Subversion Repositories php-qbpwcf

Rev

Rev 226 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 1
<?php
2
 
3
/*
4
 
5
	QBPWCF, Quick Build PHP website Component base on Fedora Linux.
6
    Copyright (C) 2015~2024 Min-Jhin,Chen
7
 
8
    This file is part of QBPWCF.
9
 
10
    QBPWCF is free software: you can redistribute it and/or modify
11
    it under the terms of the GNU General Public License as published by
12
    the Free Software Foundation, either version 3 of the License, or
13
    (at your option) any later version.
14
 
15
    QBPWCF is distributed in the hope that it will be useful,
16
    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
    GNU General Public License for more details.
19
 
20
    You should have received a copy of the GNU General Public License
21
    along with QBPWCF.  If not, see <http://www.gnu.org/licenses/>.
22
 
23
*/
24
namespace qbpwcf;
25
 
26
/*
27
類別說明:
28
跟程式中止有關的類別.
29
備註:
30
無.
31
*/
32
class stop{
33
 
34
	/*
35
	#函式說明:
36
	#當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
37
	#回傳結果:
38
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
39
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
40
	#$result["function"],當前執行的函式名稱.
41
	#必填參數:
42
	#$method,物件,為物件實體或類別名稱,會自動置入該參數.
43
	#$arguments,陣列,為呼叫方法時所用的參數.
44
	#參考資料:
45
	#__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
46
	*/
47
	public function __call($method,$arguments){
48
 
49
		#取得當前執行的函式
50
		$result["function"]=__FUNCTION__;
51
 
52
		#設置執行不正常
53
		$result["status"]="false";
54
 
55
		#設置執行錯誤
56
		$result["error"][]=__NAMESPACE__ ."/".$method."() 不存在!";
57
 
58
		#設置所丟入的參數
59
		$result["error"][]=$arguments;
60
 
61
		#回傳結果
62
		return $result;
63
 
64
		}#function __call end
65
 
66
	/*
67
	#函式說明:
68
	#當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
69
	#回傳結果:
70
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
71
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
72
	#$result["function"],當前執行的函式名稱.
73
	#必填參數:
74
	#$method,物件,為物件實體或類別名稱,會自動置入該參數.
75
	#$arguments,陣列,為呼叫方法時所用的參數.
76
	#參考資料:
77
	#__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
78
	*/
79
	public static function __callStatic($method,$arguments){
80
 
81
		#取得當前執行的函式
82
		$result["function"]=__FUNCTION__;
83
 
84
		#設置執行不正常
85
		$result["status"]="false";
86
 
87
		#設置執行錯誤
88
		$result["error"][]="欲呼叫的". __NAMESPACE__ ."/".$method."() 不存在!";
89
 
90
		#設置所丟入的參數
91
		$result["error"][]=$arguments;
92
 
93
		#回傳結果
94
		return $result;
95
 
96
		}#function __callStatic end
97
 
98
	/*
99
	#函式說明:
100
	#停止執行程式,中止前一刻會印出一段內容
101
	#回傳結果:
102
	#$result["status"],執行是否正常,正常的話為"true",不正常的話為"false".
103
	#$result["error"],錯誤訊息.
104
	#$result["function"],當前執行的函數名稱.
105
	#必填參數:
106
	#$conf["comment"],字串,要顯示的錯誤訊息.
107
	$conf["comment"]="";
108
	#參考資料:
109
	#exit=>http://php.net/manual/en/function.exit.php
110
	#備註:
111
	#正常來說不會得到回傳的$result,若有則代表異常.
112
	*/
113
	function exitWithComment(&$conf){
114
 
115
		#初始化要回傳的變數
116
		$result=array();
117
 
118
		#取得當前執行的函數名稱
119
		$result["function"]=__FUNCTION__;
120
 
121
		#如果 $conf 不為陣列
122
		if(gettype($conf)!="array"){
123
 
124
			#設置執行失敗
125
			$result["status"]="false";
126
 
127
			#設置執行錯誤訊息
128
			$result["error"][]="\$conf變數須為陣列形態";
129
 
130
			#如果傳入的參數為 null
131
			if($conf==null){
132
 
133
				#設置執行錯誤訊息
134
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
135
 
136
				}#if end
137
 
138
			#回傳結果
139
			return $result;
140
 
141
			}#if end
142
 
143
		#函式說明:
144
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
145
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
146
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
147
		#$result["function"],當前執行的函式名稱.
148
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
149
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
150
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
151
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
152
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
153
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
154
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
155
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
156
		#必填寫的參數:
157
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
158
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
159
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
160
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("comment");
161
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
162
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string");
163
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
164
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
165
		#可以省略的參數:
166
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
167
		#$conf["canBeEmptyString"]="false";
168
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或集合.
169
		#$conf["skipableVariableCanNotBeEmpty"]=array();
170
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
171
		#$conf["skipableVariableName"]=array();
172
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double"); 
173
		#$conf["skipableVariableType"]=array();
174
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
175
		#$conf["skipableVarDefaultValue"]=array("");
176
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
177
		#$conf["arrayCountEqualCheck"][]=array();
178
		#參考資料來源:
179
		#array_keys=>http://php.net/manual/en/function.array-keys.php
180
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
181
		unset($conf["variableCheck::checkArguments"]);
182
 
183
		#如果檢查失敗
184
		if($checkArguments["status"]=="false"){
185
 
186
			#設置錯誤識別
187
			$result["status"]="false";
188
 
189
			#設置錯誤訊息
190
			$result["error"]=$checkArguments;
191
 
192
			#回傳結果
193
			return $result;
194
 
195
			}#if end
196
 
197
		#如果檢查不通過
198
		if($checkArguments["passed"]=="false"){
199
 
200
			#設置錯誤識別
201
			$result["status"]="false";
202
 
203
			#設置錯誤訊息
204
			$result["error"]=$checkArguments;
205
 
206
			#回傳結果
207
			return $result;
208
 
209
			}#if end
210
 
211
		#停止執行之前輸出 $conf["comment"] 的內容
212
		exit($conf["comment"]);
213
 
214
		#執行到這邊代表不正常
215
		$result["status"]="false";
216
 
217
		#設置錯誤訊息
218
		$result["error"][]="非預期的錯誤";
219
 
220
		#回傳結果
221
		return $result;
222
 
223
		}#function exitWithComment end
224
 
225
	/*
226
	#函式說明:
227
	#停止執行程式,中止前一刻會印出一段內容
228
	#回傳結果:
229
	#$result["status"],執行是否正常,正常的話為"true",不正常的話為"false".
230
	#$result["error"],錯誤訊息.
231
	#$result["function"],當前執行的函數名稱.
232
	#必填參數:
233
	#無.
234
	#可省略參數:
235
	#$conf["returnVal"],整數,要回傳給shell的數值,預設為0.
236
	#$conf["returnVal"]=0;
237
	#$conf["normalExit"],字串,是否正常結束,若為"true"則會將參數returnVal設置為0,預設為"false".
238
	#$conf["normalExit"]="false";
239
	#$conf["unexpectedEixt"],字串,是否不正常結束,若為"true"則會將參數returnVal設置為1,預設為"false".
240
	#$conf["unexpectedEixt"]="false";
241
	#參考資料:
242
	#exit=>http://php.net/manual/en/function.exit.php
243
	#備註:
244
	#正常來說不會得到回傳的$result,若有則代表異常.
245
	*/
246
	public static function exitWithReturnValueToShell(&$conf){
247
 
248
		#初始化要回傳的變數
249
		$result=array();
250
 
251
		#取得當前執行的函數名稱
252
		$result["function"]=__FUNCTION__;
253
 
254
		#如果 $conf 不為陣列
255
		if(gettype($conf)!="array"){
256
 
257
			#設置執行失敗
258
			$result["status"]="false";
259
 
260
			#設置執行錯誤訊息
261
			$result["error"][]="\$conf變數須為陣列形態";
262
 
263
			#如果傳入的參數為 null
264
			if($conf==null){
265
 
266
				#設置執行錯誤訊息
267
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
268
 
269
				}#if end
270
 
271
			#回傳結果
272
			return $result;
273
 
274
			}#if end
275
 
276
		#函式說明:
277
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容。
278
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
279
		#$reuslt["error"],執行不正常結束的錯訊息陣列.
280
		#$result["function"],當前執行的函式名稱.
281
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
282
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
283
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
284
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
285
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
286
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
287
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
288
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
289
		#必填寫的參數:
290
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
291
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
292
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
293
		#$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("comment");
294
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
295
		#$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string");
296
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
297
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
298
		#可以省略的參數:
299
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
300
		#$conf["canBeEmptyString"]="false";
301
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或集合.
302
		$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("returnVal","normalExit","unexpectedEixt");
303
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
304
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("returnVal","normalExit","unexpectedEixt");
305
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double"); 
306
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("integer","string","string");
307
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
308
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array(0,"false","false");
309
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
310
		#$conf["arrayCountEqualCheck"][]=array();
311
		#參考資料來源:
312
		#array_keys=>http://php.net/manual/en/function.array-keys.php
313
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
314
		unset($conf["variableCheck::checkArguments"]);
315
 
316
		#如果檢查失敗
317
		if($checkArguments["status"]=="false"){
318
 
319
			#設置錯誤識別
320
			$result["status"]="false";
321
 
322
			#設置錯誤訊息
323
			$result["error"]=$checkArguments;
324
 
325
			#回傳結果
326
			return $result;
327
 
328
			}#if end
329
 
330
		#如果檢查不通過
331
		if($checkArguments["passed"]=="false"){
332
 
333
			#設置錯誤識別
334
			$result["status"]="false";
335
 
336
			#設置錯誤訊息
337
			$result["error"]=$checkArguments;
338
 
339
			#回傳結果
340
			return $result;
341
 
342
			}#if end
343
 
344
		#如果是正常結束
345
		if($conf["normalExit"]==="true"){
346
 
347
			#回傳代碼設置為0
348
			$conf["returnVal"]=0;
349
 
350
			}#if end
351
 
352
		else if($conf["unexpectedEixt"]==="true"){
353
 
354
			#回傳代碼設置為1
355
			$conf["returnVal"]=1;
356
 
357
			}#if end
358
 
359
		#停止執行並回傳 $conf["returnVal"] 給 shell
360
		exit($conf["returnVal"]);
361
 
362
		#執行到這邊代表不正常
363
		$result["status"]="false";
364
 
365
		#設置錯誤訊息
366
		$result["error"][]="非預期的錯誤";
367
 
368
		#回傳結果
369
		return $result;
370
 
371
		}#function exitWithReturnValueToShell end
372
 
373
	}#class stop end
374
 
375
?>