Subversion Repositories php-qbpwcf

Rev

Rev 239 | Go to most recent revision | Details | Compare with Previous | 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.
239 liveuser 6
    Copyright (C) 2014~2026 MIN ZHI, CHEN
3 liveuser 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
https://www.php.net/manual/en/book.parallel.php
31
備註:
32
建構中
33
*/
34
class threads{
35
 
36
	/*
37
	#函式說明:
38
	#當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
39
	#回傳結果:
40
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
41
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
42
	#$result["function"],當前執行的函式名稱.
43
	#必填參數:
44
	#$method,物件,為物件實體或類別名稱,會自動置入該參數.
45
	#$arguments,陣列,為呼叫方法時所用的參數.
46
	#可省略參數:
47
	#無.
48
	#參考資料:
49
	#__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
50
	#備註:
51
	#無.
52
	*/
53
	public function __call($method,$arguments){
54
 
55
		#取得當前執行的函式
56
		$result["function"]=__FUNCTION__;
57
 
58
		#設置執行不正常
59
		$result["status"]="false";
60
 
61
		#設置執行錯誤
62
		$result["error"][]=__NAMESPACE__ ."/".$method."() 不存在!";
63
 
64
		#設置所丟入的參數
65
		$result["error"][]=$arguments;
66
 
67
		#回傳結果
68
		return $result;
69
 
70
		}#function __call end
71
 
72
	/*
73
	#函式說明:
74
	#當前類別被呼叫的靜態方法不存在時,將會執行該函數,回報該方法不存在.
75
	#回傳結果:
76
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
77
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
78
	#$result["function"],當前執行的函式名稱.
79
	#必填參數:
80
	#$method,物件,為物件實體或類別名稱,會自動置入該參數.
81
	#$arguments,陣列,為呼叫方法時所用的參數.
82
	#可省略參數:
83
	#無.
84
	#參考資料:
85
	#__call=>http://php.net/manual/en/language.oop5.overloading.php#object.callstatic
86
	#備註:
87
	#無.
88
	*/
89
	public static function __callStatic($method,$arguments){
90
 
91
		#取得當前執行的函式
92
		$result["function"]=__FUNCTION__;
93
 
94
		#設置執行不正常
95
		$result["status"]="false";
96
 
97
		#設置執行錯誤
98
		$result["error"][]="欲呼叫的". __NAMESPACE__ ."/".$method."() 不存在!";
99
 
100
		#設置所丟入的參數
101
		$result["error"][]=$arguments;
102
 
103
		#回傳結果
104
		return $result;
105
 
106
		}#function __callStatic end
107
 
108
	/*
109
	#函式說明:
110
	#展示多執行序運作.
111
	#回傳結果:
112
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
113
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
114
	#$result["function"],當前執行的函式名稱.
115
	#必填參數:
116
	#無.
117
	#可省略參數:
118
	#無.
119
	#參考資料:
120
	#無.
121
	#備註:
122
	#無.
123
	改用 proc 試試 https://www.php.net/manual/en/function.proc-open.php
124
	*/
125
	public static function demo(){
226 liveuser 126
 
3 liveuser 127
		#執行demo
128
		var_dump(self::proc_demo());
226 liveuser 129
 
3 liveuser 130
		}#function demo end
131
 
132
	/*
133
	#函式說明:
134
	#透過proc展示多執行序運作.
135
	#回傳結果:
136
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
137
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
138
	#$result["function"],當前執行的函式名稱.
139
	#必填參數:
140
	#無.
141
	#可省略參數:
142
	#無.
143
	#參考資料:
144
	#https://www.php.net/manual/en/function.proc-open.php
145
	#備註:
146
	#無.
147
	*/
148
	public static function proc_demo(){
226 liveuser 149
 
3 liveuser 150
		#初始化 descriptorspec
151
		$descriptorspec = array();
226 liveuser 152
 
3 liveuser 153
		#stdin is a pipe that the child will read from
154
		$descriptorspec[]=array("pipe", "r");
226 liveuser 155
 
3 liveuser 156
		#stdout is a pipe that the child will write to
157
		$descriptorspec[]=array("pipe", "w");
226 liveuser 158
 
3 liveuser 159
		#stderr is a pipe that the child will write to
160
		$descriptorspec[]=array("pipe", "w");
226 liveuser 161
 
162
		#The initial working dir for the command. This must be an absolute directory path, or null if you want to use the default value (the working dir of the current PHP process)
3 liveuser 163
		$cwd = null;
226 liveuser 164
 
165
		#An array with the environment variables for the command that will be run, or null to use the same environment as the current PHP process
3 liveuser 166
		$env = array("QBPWCF" => "Quick Build PHP Website Componment base on Fedora Linux");
226 liveuser 167
 
3 liveuser 168
		#初始化儲存要多執行序執行的程式
169
		$codes=array();
226 liveuser 170
 
3 liveuser 171
		#初始化要demo用的url
172
		$demoUrl="https://qbpwc.sourceforge.io/";
226 liveuser 173
 
3 liveuser 174
		#要多執行序執行的程式1
175
		$codes[]=array("bash","curl -v -L --ssl-reqd ".$demoUrl);
226 liveuser 176
 
3 liveuser 177
		#要多執行序執行的程式2
178
		$codes[]=array("bash","curl -v -L --ssl-reqd ".$demoUrl);
226 liveuser 179
 
3 liveuser 180
		#要多執行序執行的程式3
181
		$codes[]=array("bash","curl -v -L --ssl-reqd ".$demoUrl);
226 liveuser 182
 
3 liveuser 183
		#debug
184
		#var_dump(__LINE__,$codes);
226 liveuser 185
 
3 liveuser 186
		#儲存每個要執行的程式資訊
187
		$process=array();
226 liveuser 188
 
3 liveuser 189
		#針對每個要執行的程式內容
190
		foreach($codes as $index=>$code){
226 liveuser 191
 
3 liveuser 192
			#取得要用什麼程式執行
193
			$executeBy=$code[0];
226 liveuser 194
 
3 liveuser 195
			#取得要執行的程式內容
196
			$code=$code[1];
226 liveuser 197
 
3 liveuser 198
			#運行 php 程式,並儲存 resource
199
			$process[$index]["resource"] = proc_open($executeBy, $descriptorspec, $pipes, $cwd, $env);
226 liveuser 200
 
3 liveuser 201
			#儲存對應的 pipes
202
			$process[$index]["pipes"]=$pipes;
226 liveuser 203
 
3 liveuser 204
			#debug
205
			#var_dump(__LINE__,$process[$index]);
226 liveuser 206
 
3 liveuser 207
			#如果有成功執行
208
			if(is_resource($process[$index]["resource"])){
226 liveuser 209
 
3 liveuser 210
				#$pipes now looks like this:
211
				#0 => writeable handle connected to child stdin
212
				#1 => readable handle connected to child stdout
213
				#2 => readable handle connected to child stdin
226 liveuser 214
 
3 liveuser 215
				#寫入內容作為 $executeBy 的輸入
216
				$bytes2write=fwrite($process[$index]["pipes"][0], $code);
226 liveuser 217
 
3 liveuser 218
				#debug
219
				#var_dump(__LINE__,$bytes2write);
226 liveuser 220
 
3 liveuser 221
				#結束輸入
222
				fclose($process[$index]["pipes"][0]);
226 liveuser 223
 
3 liveuser 224
				}#if end
226 liveuser 225
 
3 liveuser 226
			#反之,代表運行 php 失敗
227
			else{
226 liveuser 228
 
3 liveuser 229
				#初始化暫存執行結果的變數
230
				$cmdRes=array();
226 liveuser 231
 
3 liveuser 232
				#設置執行失敗
233
				$cmdRes["status"]="false";
226 liveuser 234
 
3 liveuser 235
				#設置執行錯誤訊息
236
				$cmdRes["error"]="execute program by \"".$executeBy."\" failed!";
226 liveuser 237
 
3 liveuser 238
				#設置執行結果
239
				$result["content"][]=$cmdRes;
226 liveuser 240
 
3 liveuser 241
				}#else end
226 liveuser 242
 
3 liveuser 243
			}#foreach end
226 liveuser 244
 
3 liveuser 245
		#debug
246
		#var_dump(__LINE__,$process);
226 liveuser 247
 
3 liveuser 248
		#針對每個回饋
249
		foreach($process as $index => $ps){
226 liveuser 250
 
3 liveuser 251
			#取得 proc resource
252
			$resource=$ps["resource"];
226 liveuser 253
 
3 liveuser 254
			#取得標準輸出
255
			$stdout=stream_get_contents($process[$index]["pipes"][1]);
226 liveuser 256
 
3 liveuser 257
			#取得錯誤輸出
258
			$stderr=stream_get_contents($process[$index]["pipes"][2]);
226 liveuser 259
 
3 liveuser 260
			#debug
261
			#var_dump($stdout);
226 liveuser 262
 
3 liveuser 263
			#初始化暫存執行狀況的變數
264
			$cmdRes=array();
226 liveuser 265
 
3 liveuser 266
			#設置執行失敗訊息
267
			$cmdRes["error"]=$stderr;
226 liveuser 268
 
3 liveuser 269
			#設置執行結果
270
			$cmdRes["content"]=$stdout;
226 liveuser 271
 
3 liveuser 272
			#debug
273
			#var_dump($cmdRes);
226 liveuser 274
 
3 liveuser 275
			#結束讀取標準輸出
276
			fclose($process[$index]["pipes"][1]);
226 liveuser 277
 
3 liveuser 278
			#結束讀取錯誤輸出
279
			fclose($process[$index]["pipes"][2]);
226 liveuser 280
 
3 liveuser 281
			#It is important that you close any pipes before calling proc_close in order to avoid a deadlock
282
			$cmdRes["statusCode"] = proc_close($resource);
226 liveuser 283
 
3 liveuser 284
			#debug
285
			#var_dump($cmdRes["statusCode"]);
226 liveuser 286
 
3 liveuser 287
			#預設執行不正常
288
			$cmdRes["status"]="false";
226 liveuser 289
 
3 liveuser 290
			#如果執行正常
291
			if($cmdRes["statusCode"]===0){
226 liveuser 292
 
3 liveuser 293
				#設置執行正常
294
				$cmdRes["status"]="true";
226 liveuser 295
 
3 liveuser 296
				}#if end
226 liveuser 297
 
3 liveuser 298
			#保存結果
299
			$result["content"][]=$cmdRes;
226 liveuser 300
 
3 liveuser 301
			#debug
302
			#var_dump($result);
226 liveuser 303
 
3 liveuser 304
			}#foreach end
226 liveuser 305
 
3 liveuser 306
		#設置執行正常
307
		$result["status"]="true";
226 liveuser 308
 
3 liveuser 309
		#debug
310
		#var_dump($result);
226 liveuser 311
 
3 liveuser 312
		#回傳結果
313
		return $result;
226 liveuser 314
 
3 liveuser 315
		}#function proc_demo end
316
 
317
	/*
318
	#函式說明:
319
	#透過proc來多執行序運作.
320
	#回傳結果:
321
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
322
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
323
	#$result["function"],當前執行的函式名稱.
324
	#$result["argu"],使用的參數.
162 liveuser 325
	#$result["content"],陣列,每個元素為其指令執行的結果訊息陣列,key為"status"代表執行是否正常的識別;key為"statusCode"代表程式結束後回傳給對應executeBy程式的數值,若為"?"則代表程序尚未結束,若為整數0,則代表順利執行結束,可透過proc_update函式進行資訊的更新與取得;key為"output"代表標準輸出,若為resource,則代表為pipe;key為"error"代表非標準輸出,若為resource,則代表為pipe;key為"input"代表成功輸入的指令;key為"process"代表該程序經proc_open後的process source;key為"proc_get_status"代表程序的資訊.
3 liveuser 326
	#必填參數:
66 liveuser 327
	#$conf["cmds"],字串陣列,每個元素代表單一程序要執行的指令與參數.
3 liveuser 328
	#$conf["cmds"]=array();
329
	#可省略參數:
330
	#$conf["wait"],字串,是否需要等待所有程序結束,預設為"true"要等待;反之為"false"不要等待.
331
	#$conf["wait"]="false";
140 liveuser 332
	#$conf["timeout"],字串陣列,每個元素代表單一程序執行的最大等待秒數,超過後將會強迫停止執行,僅當wait參數為"true"時生效.
333
	#$conf["timeout"]=array("10");
3 liveuser 334
	#$conf["workingDir"],字串陣列,個別程式執行時的家目錄,預設不指定.
335
	#$conf["workingDir"]=array("path");
336
	#$conf["envs"],2維字串陣列,每個元素代表個別程式執行時的指定環境變數,key變數名稱;value為變數內容.預設為array("QBPWCF" => "Quick Build PHP Website Componment base on Fedora Linux");
337
	#$conf["envs"]=array(array("key"=>"value"));
338
	#$conf["executeBy"],字串陣列,每個元素代表個別指令要用什麼程式執行,預設為"bash".
339
	#$conf["executeBy"]=array("bash");
340
	#參考資料:
341
	#https://www.php.net/manual/en/function.proc-open.php
342
	#https://www.php.net/manual/en/function.proc-get-status.php
140 liveuser 343
	#https://www.php.net/manual/en/function.proc-terminate.php
3 liveuser 344
	#備註:
249 liveuser 345
	#當wait參數為"true"時,會自動解析stdout與stderr,因此不用再透過 self::proc_update 來更新.
346
	#當wait參數不為"true"時,若需要取得當下的執行狀況,請使用 self::proc_update 來更新.
3 liveuser 347
	*/
348
	public static function proc(&$conf){
226 liveuser 349
 
3 liveuser 350
		#初始化要回傳的變數
351
		$result=array();
352
 
353
		#初始化當前執行的函數名稱
354
		$result["function"]=__FUNCTION__;
355
 
356
		#如果 $conf 不為陣列
66 liveuser 357
		if(gettype($conf)!=="array"){
226 liveuser 358
 
3 liveuser 359
			#設置執行失敗
360
			$result["status"]="false";
226 liveuser 361
 
3 liveuser 362
			#設置執行錯誤訊息
363
			$result["error"][]="\$conf變數須為陣列形態";
364
 
365
			#如果傳入的參數為 null
66 liveuser 366
			if($conf===null){
226 liveuser 367
 
3 liveuser 368
				#設置執行錯誤訊息
369
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 370
 
3 liveuser 371
				}#if end
372
 
373
			#回傳結果
374
			return $result;
226 liveuser 375
 
3 liveuser 376
			}#if end
226 liveuser 377
 
3 liveuser 378
		#取得使用的參數
379
		$result["argu"]=$conf;
226 liveuser 380
 
3 liveuser 381
		#函式說明:
382
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
383
		#回傳結果:
384
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
385
		#$result["error"],執行不正常結束的錯訊息陣列.
386
		#$result["simpleError"],簡單表示的錯誤訊息.
387
		#$result["function"],當前執行的函式名稱.
388
		#$result["argu"],設置給予的參數.
389
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
390
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
391
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
392
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
393
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
394
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
395
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
396
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
397
		#必填參數:
398
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
399
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
400
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
401
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
402
		#可省略參數:
403
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
404
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("cmds");
405
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
406
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("array");
407
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
408
		#$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
409
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
410
		#$conf["canNotBeEmpty"]=array();
411
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
412
		#$conf["canBeEmpty"]=array();
413
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
140 liveuser 414
		$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("wait","workingDir","envs","executeBy","timeout");
3 liveuser 415
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
140 liveuser 416
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("wait","workingDir","envs","executeBy","timeout");
3 liveuser 417
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
140 liveuser 418
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","array","array","array","array");
3 liveuser 419
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
140 liveuser 420
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("true",null,null,null,null);
3 liveuser 421
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
422
		#$conf["disallowAllSkipableVarIsEmpty"]="";
423
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
424
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
425
		#$conf["disallowAllSkipableVarNotExist"],字串,是否不允許每個可省略參數都不存在,預設為"false"代表允許,反之為"true".
426
		#$conf["disallowAllSkipableVarNotExist"]="";
427
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
428
		#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"][]=array("");
429
		#參考資料:
430
		#array_keys=>http://php.net/manual/en/function.array-keys.php
431
		#備註:
432
		#無.
433
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
434
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 435
 
3 liveuser 436
		#如果 $checkArguments["status"] 等於 "false"
437
		if($checkArguments["status"]==="false"){
226 liveuser 438
 
3 liveuser 439
			#設置錯誤識別
440
			$result["status"]="false";
226 liveuser 441
 
3 liveuser 442
			#設置錯誤訊息
443
			$result["error"]=$checkArguments;
226 liveuser 444
 
3 liveuser 445
			#回傳結果
446
			return $result;
226 liveuser 447
 
3 liveuser 448
			}#if end
226 liveuser 449
 
3 liveuser 450
		#如果 $checkArguments["passed"] 等於 "false"
451
		if($checkArguments["passed"]==="false"){
226 liveuser 452
 
3 liveuser 453
			#設置錯誤識別
454
			$result["status"]="false";
226 liveuser 455
 
3 liveuser 456
			#設置錯誤訊息
457
			$result["error"]=$checkArguments;
226 liveuser 458
 
3 liveuser 459
			#回傳結果
460
			return $result;
226 liveuser 461
 
3 liveuser 462
			}#if end
226 liveuser 463
 
3 liveuser 464
		#初始化 descriptorspec
465
		$descriptorspec = array();
226 liveuser 466
 
3 liveuser 467
		#stdin is a pipe that the child will read from
468
		$descriptorspec[]=array("pipe", "r");
226 liveuser 469
 
3 liveuser 470
		#stdout is a pipe that the child will write to
471
		$descriptorspec[]=array("pipe", "w");
226 liveuser 472
 
3 liveuser 473
		#stderr is a pipe that the child will write to
474
		$descriptorspec[]=array("pipe", "w");
226 liveuser 475
 
476
		#The initial working dir for the command. This must be an absolute directory path, or null if you want to use the default value (the working dir of the current PHP process)
3 liveuser 477
		$defaultCWD = null;
226 liveuser 478
 
3 liveuser 479
		#初始化儲存每個指令的working dir.
480
		$cwds=array();
226 liveuser 481
 
3 liveuser 482
		#有幾個指令就執行幾次
483
		for($i=0;$i<count($conf["cmds"]);$i++){
226 liveuser 484
 
3 liveuser 485
			#設置預設working dir
486
			$cwds[]=$defaultCWD;
226 liveuser 487
 
3 liveuser 488
			}#for end
226 liveuser 489
 
3 liveuser 490
		#如果有設置 workingDir
491
		if(isset($conf["workingDir"])){
226 liveuser 492
 
3 liveuser 493
			#如果數量符合
494
			if(count($conf["cmds"])===count($conf["workingDir"])){
226 liveuser 495
 
3 liveuser 496
				#有幾個指令就執行幾次
497
				for($i=0;$i<count($conf["cmds"]);$i++){
226 liveuser 498
 
3 liveuser 499
					#預設為指定數值
500
					$cwds[]=$conf["workingDir"][$i];
226 liveuser 501
 
3 liveuser 502
					}#for end
226 liveuser 503
 
3 liveuser 504
				}#if end
226 liveuser 505
 
3 liveuser 506
			}#if end
226 liveuser 507
 
508
		#An array with the environment variables for the command that will be run, or null to use the same environment as the current PHP process
3 liveuser 509
		$defaultENV = array("QBPWCF" => "Quick Build PHP Website Componment base on Fedora Linux");
226 liveuser 510
 
3 liveuser 511
		#初始化儲存每個指令的環境變數.
512
		$envs=array();
226 liveuser 513
 
3 liveuser 514
		#有幾個指令就執行幾次
515
		for($i=0;$i<count($conf["cmds"]);$i++){
226 liveuser 516
 
3 liveuser 517
			#設置預設環境變數
518
			$envs[]=$defaultENV;
226 liveuser 519
 
3 liveuser 520
			}#for end
226 liveuser 521
 
3 liveuser 522
		#如果有設置 envs
523
		if(isset($conf["envs"])){
226 liveuser 524
 
3 liveuser 525
			#如果數量符合
526
			if(count($conf["cmds"])===count($conf["envs"])){
226 liveuser 527
 
3 liveuser 528
				#有幾個指令就執行幾次
529
				for($i=0;$i<count($conf["cmds"]);$i++){
226 liveuser 530
 
3 liveuser 531
					#預設為指定數值
532
					$envs[]=$conf["envs"][$i];
226 liveuser 533
 
3 liveuser 534
					}#for end
226 liveuser 535
 
3 liveuser 536
				}#if end
226 liveuser 537
 
3 liveuser 538
			}#if end
226 liveuser 539
 
3 liveuser 540
		#預設程式都透過 bash 來執行
541
		$defaultExecBy="bash";
226 liveuser 542
 
3 liveuser 543
		#初始化儲存每個指令要透過什麼程式執行
544
		$executeBys=array();
226 liveuser 545
 
3 liveuser 546
		#有幾個指令就執行幾次
547
		for($i=0;$i<count($conf["cmds"]);$i++){
226 liveuser 548
 
3 liveuser 549
			#設置預設環境變數
550
			$executeBys[]=$defaultExecBy;
226 liveuser 551
 
3 liveuser 552
			}#for end
226 liveuser 553
 
3 liveuser 554
		#如果有設置 executeBy
555
		if(isset($conf["executeBy"])){
226 liveuser 556
 
3 liveuser 557
			#如果數量符合
558
			if(count($conf["cmds"])===count($conf["executeBy"])){
226 liveuser 559
 
3 liveuser 560
				#初始化儲存每個指令要透過什麼程式執行
561
				$executeBys=array();
226 liveuser 562
 
3 liveuser 563
				#有幾個指令就執行幾次
564
				for($i=0;$i<count($conf["cmds"]);$i++){
226 liveuser 565
 
3 liveuser 566
					#預設為指定數值
567
					$executeBys[]=$conf["executeBy"][$i];
226 liveuser 568
 
3 liveuser 569
					}#for end
226 liveuser 570
 
3 liveuser 571
				}#if end
226 liveuser 572
 
3 liveuser 573
			}#if end
226 liveuser 574
 
3 liveuser 575
		#儲存每個要執行的程式資訊
576
		$process=array();
226 liveuser 577
 
3 liveuser 578
		#debug
579
		#var_dump(__LINE__,$executeBys);
226 liveuser 580
 
3 liveuser 581
		#針對每個指令與參數字串
582
		foreach($conf["cmds"] as $index=>$cmdAndArguStr){
226 liveuser 583
 
3 liveuser 584
			#debug
585
			#var_dump(__LINE__,$executeBys[$index]);
226 liveuser 586
 
3 liveuser 587
			#運行 php 程式,並儲存 resource
588
			$process[$index]["resource"] = proc_open($executeBys[$index], $descriptorspec, $pipes, $cwds[$index], $envs[$index]);
226 liveuser 589
 
3 liveuser 590
			#儲存對應的 pipes
591
			$process[$index]["pipes"]=$pipes;
226 liveuser 592
 
3 liveuser 593
			#如果有成功執行
594
			if(is_resource($process[$index]["resource"])){
226 liveuser 595
 
3 liveuser 596
				#$pipes now looks like this:
597
				#0 => writeable handle connected to child stdin
598
				#1 => readable handle connected to child stdout
599
				#2 => readable handle connected to child stdin
226 liveuser 600
 
3 liveuser 601
				#寫入內容作為 $executeBy 的輸入
602
				$bytes2write=fwrite($process[$index]["pipes"][0], $cmdAndArguStr);
226 liveuser 603
 
3 liveuser 604
				#結束輸入
605
				fclose($process[$index]["pipes"][0]);
226 liveuser 606
 
3 liveuser 607
				#如果有完整寫入指令
608
				if($bytes2write===strlen($cmdAndArguStr)){
226 liveuser 609
 
3 liveuser 610
					#記錄輸入的指令
611
					$process[$index]["input"]=$cmdAndArguStr;
226 liveuser 612
 
3 liveuser 613
					}#if end
226 liveuser 614
 
3 liveuser 615
				#反之
616
				else{
226 liveuser 617
 
3 liveuser 618
					#設置錯誤識別
619
					$result["status"]="false";
226 liveuser 620
 
3 liveuser 621
					#設置錯誤訊息
622
					$result["error"][]="將指令(".$cmdAndArguStr.")輸入(".$executeBys[$index].")失敗";
226 liveuser 623
 
3 liveuser 624
					#回傳結果
625
					return $result;
226 liveuser 626
 
3 liveuser 627
					}#else end
226 liveuser 628
 
3 liveuser 629
				}#if end
226 liveuser 630
 
3 liveuser 631
			#反之,代表運行 $executeBys[$index] 失敗
632
			else{
226 liveuser 633
 
3 liveuser 634
				#初始化暫存執行結果的變數
635
				$cmdRes=array();
226 liveuser 636
 
3 liveuser 637
				#設置執行失敗
638
				$cmdRes["status"]="false";
226 liveuser 639
 
3 liveuser 640
				#設置執行錯誤訊息
641
				$cmdRes["error"]="execute program by \"".$executeBys[$index]."\" failed!";
226 liveuser 642
 
3 liveuser 643
				#設置執行結果
644
				$result["content"][]=$cmdRes;
226 liveuser 645
 
3 liveuser 646
				}#else end
226 liveuser 647
 
3 liveuser 648
			}#foreach end
226 liveuser 649
 
3 liveuser 650
		#針對每個回饋
651
		foreach($process as $index => $ps){
226 liveuser 652
 
3 liveuser 653
			#取得 proc resource
654
			$resource=&$ps["resource"];
226 liveuser 655
 
3 liveuser 656
			#取得標準輸出的pipe
657
			$stdout=$process[$index]["pipes"][1];
226 liveuser 658
 
3 liveuser 659
			#取得非標準輸出的pipe
660
			$stderr=$process[$index]["pipes"][2];
226 liveuser 661
 
3 liveuser 662
			#如果需要等待程序執行完
663
			if($conf["wait"]==="true"){
226 liveuser 664
 
140 liveuser 665
				#如果有設置  timeout
666
				if(isset($conf["timeout"])){
226 liveuser 667
 
140 liveuser 668
					#如果有設置該程序的 timeout
669
					if(isset($conf["timeout"][$index])){
226 liveuser 670
 
140 liveuser 671
						#避免 timeout 仍然會等待程序結束 - start
226 liveuser 672
 
140 liveuser 673
						#不讓標準輸出會阻擋程式執行
674
						stream_set_blocking($stdout,false);
226 liveuser 675
 
140 liveuser 676
						#不讓錯誤輸出會阻擋程式執行
677
						stream_set_blocking($stderr,false);
226 liveuser 678
 
140 liveuser 679
						#避免 timeout 仍然會等待程序結束 - end
226 liveuser 680
 
140 liveuser 681
						}#if end
226 liveuser 682
 
140 liveuser 683
					}#if end
226 liveuser 684
 
3 liveuser 685
				}#if end
226 liveuser 686
 
3 liveuser 687
			#反之
688
			else{
226 liveuser 689
 
3 liveuser 690
				#不讓標準輸出會阻擋程式執行
691
				stream_set_blocking($stdout,false);
226 liveuser 692
 
3 liveuser 693
				#不讓錯誤輸出會阻擋程式執行
694
				stream_set_blocking($stderr,false);
226 liveuser 695
 
3 liveuser 696
				}#else end
226 liveuser 697
 
3 liveuser 698
			#初始化暫存執行狀況的變數
699
			$cmdRes=array();
226 liveuser 700
 
3 liveuser 701
			#取得 proc_open 後的 process resource
702
			$cmdRes["process"]=$resource;
226 liveuser 703
 
3 liveuser 704
			#取得 process 的狀態,包含 pid 等資訊,若程式已經結束,則會自動將pipes關閉,再proc_close.
705
			$cmdRes["proc_get_status"]=proc_get_status($resource);
226 liveuser 706
 
3 liveuser 707
			#預設 statusCode 為 "?" 代表尚未 proc_close
708
			$cmdRes["statusCode"]="?";
226 liveuser 709
 
3 liveuser 710
			#如果執行已經結束,有回傳exitcode
711
			if($cmdRes["proc_get_status"]["exitcode"]!==-1){
226 liveuser 712
 
3 liveuser 713
				#設置 statusCode
714
				$cmdRes["statusCode"]=&$cmdRes["proc_get_status"]["exitcode"];
226 liveuser 715
 
3 liveuser 716
				}#if end
226 liveuser 717
 
3 liveuser 718
			#反之未執行完畢,且需要等待程序執行完
719
			else if($conf["wait"]==="true"){
226 liveuser 720
 
140 liveuser 721
				#預設已經執行0秒
722
				$runtime=0;
226 liveuser 723
 
3 liveuser 724
				#無窮迴圈
725
				while(true){
226 liveuser 726
 
140 liveuser 727
					#debug
728
					#var_dump(__LINE__,$runtime,$conf["timeout"]);
226 liveuser 729
 
140 liveuser 730
					#如果有設置  timeout
731
					if(isset($conf["timeout"])){
226 liveuser 732
 
140 liveuser 733
						#如果有設置該程序的 timeout
734
						if(isset($conf["timeout"][$index])){
226 liveuser 735
 
140 liveuser 736
							#如果執行秒數逾時了
737
							if( $runtime > (int)($conf["timeout"][$index]) ){
226 liveuser 738
 
140 liveuser 739
								#debug
740
								#var_dump(__LINE__,"timeout!");
226 liveuser 741
 
140 liveuser 742
								#關閉標準輸出的pipe
743
								fclose($stdout);
226 liveuser 744
 
140 liveuser 745
								#關閉標準錯誤的pipe
746
								fclose($stderr);
226 liveuser 747
 
140 liveuser 748
								#強制結束程序
749
								proc_terminate($resource);
226 liveuser 750
 
140 liveuser 751
								#結束proc.
752
								proc_close($resource);
226 liveuser 753
 
159 liveuser 754
								#結束無窮迴圈
755
								break;
226 liveuser 756
 
140 liveuser 757
								}#if end
226 liveuser 758
 
140 liveuser 759
							}#if end
226 liveuser 760
 
140 liveuser 761
						}#if end
226 liveuser 762
 
3 liveuser 763
					#取得 process 的狀態,包含 pid 等資訊,若程式已經結束,則會自動將pipes關閉,再proc_close.
764
					$cmdRes["proc_get_status"]=proc_get_status($resource);
226 liveuser 765
 
3 liveuser 766
					#如果執行已經結束,有回傳exitcode
767
					if($cmdRes["proc_get_status"]["exitcode"]!==-1){
226 liveuser 768
 
3 liveuser 769
						#設置 statusCode
770
						$cmdRes["statusCode"]=&$cmdRes["proc_get_status"]["exitcode"];
249 liveuser 771
 
159 liveuser 772
						#結束無窮迴圈
3 liveuser 773
						break;
226 liveuser 774
 
3 liveuser 775
						}#if end
226 liveuser 776
 
3 liveuser 777
					#等待1秒鐘
778
					sleep(1);
226 liveuser 779
 
140 liveuser 780
					#執行秒數+1
781
					$runtime++;
226 liveuser 782
 
3 liveuser 783
					}#while end
226 liveuser 784
 
3 liveuser 785
				}#if end
226 liveuser 786
 
3 liveuser 787
			#設置執行失敗訊息
788
			$cmdRes["error"]=$stderr;
226 liveuser 789
 
3 liveuser 790
			#設置執行結果
791
			$cmdRes["content"]=$stdout;
226 liveuser 792
 
3 liveuser 793
			#如果有成功輸入指令
794
			if(isset($process[$index]["input"])){
226 liveuser 795
 
3 liveuser 796
				#設置有輸入的指令
797
				$cmdRes["input"]=$process[$index]["input"];
226 liveuser 798
 
3 liveuser 799
				}#if end
226 liveuser 800
 
3 liveuser 801
			#預設執行不正常
802
			$cmdRes["status"]="false";
226 liveuser 803
 
3 liveuser 804
			#如果執行正常 或 等於"?"(代表尚未 proc_close)
805
			if($cmdRes["statusCode"]===0 || $cmdRes["statusCode"]==="?"){
226 liveuser 806
 
3 liveuser 807
				#設置執行正常
808
				$cmdRes["status"]="true";
226 liveuser 809
 
3 liveuser 810
				}#if end
226 liveuser 811
 
3 liveuser 812
			#保存結果
813
			$result["content"][]=$cmdRes;
226 liveuser 814
 
3 liveuser 815
			}#foreach end
226 liveuser 816
 
249 liveuser 817
		#如果要等待程序執行結束
818
		if($conf["wait"]==="true"){
819
 
820
			#函式說明:
821
			#更新透過proc執行的多程序資訊.
822
			#回傳結果:
823
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
824
			#$reuslt["error"],執行不正常結束的錯訊息陣列.
825
			#$result["function"],當前執行的函式名稱.
826
			#$result["argu"],使用的參數.
827
			#$result["content"],陣列,每個元素為其指令執行的結果訊息陣列,key為"status"代表執行是否正常的識別;key為"statusCode"代表程式結束後回傳給對應executeBy程式的數值,若為"?"則代表程序尚未結束,可透過proc_update函式進行資訊的更新與取得;key為"output"代表標準輸出,若為resource,則代表為pipe;key為"error"代表非標準輸出,若為resource,則代表為pipe;key為"input"代表成功輸入的指令;key為"process"代表該程序經proc_open後的process source;key為"proc_get_status"代表程序的資訊.
828
			#必填參數:
829
			#$conf["procs"],陣列,運行self::proc後回傳的content.
830
			$conf["threads::proc_update"]["procs"]=$result["content"];
831
			#可省略參數:
832
			#無.
833
			#參考資料:
834
			#無.
835
			#備註:
836
			#無.
837
			$proc_update=threads::proc_update($conf["threads::proc_update"]);
838
			unset($conf["threads::proc_update"]);
839
 
840
			#如果執行異常
841
			if($proc_update["status"]==="false"){
842
 
843
				#設置錯誤識別
844
				$result["status"]="false";
845
 
846
				#設置錯誤訊息
847
				$result["error"]=$proc_update;
848
 
849
				#回傳結果
850
				return $result;
851
 
852
				}#if end
853
 
854
			#debug
855
			#var_dump(__LINE__,$proc_update);exit;
856
 
857
			#取得執行後的標準輸出
858
			$result["content"]=$proc_update["content"];
859
 
860
			}#if end
861
 
3 liveuser 862
		#設置執行正常
863
		$result["status"]="true";
226 liveuser 864
 
3 liveuser 865
		#回傳結果
866
		return $result;
226 liveuser 867
 
3 liveuser 868
		}#function proc end
869
 
870
	/*
871
	#函式說明:
872
	#更新透過proc執行的多程序資訊.
873
	#回傳結果:
874
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
875
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
876
	#$result["function"],當前執行的函式名稱.
877
	#$result["argu"],使用的參數.
66 liveuser 878
	#$result["content"],陣列,每個元素為其指令執行的結果訊息陣列,key為"status"代表執行是否正常的識別;key為"statusCode"代表程式結束後回傳給對應executeBy程式的數值,若為"?"則代表程序尚未結束,可透過proc_update函式進行資訊的更新與取得;key為"output"代表標準輸出,若為resource,則代表為pipe;key為"error"代表非標準輸出,若為resource,則代表為pipe;key為"input"代表成功輸入的指令;key為"process"代表該程序經proc_open後的process source;key為"proc_get_status"代表程序的資訊.
3 liveuser 879
	#必填參數:
880
	#$conf["procs"],陣列,運行self::proc後回傳的content.
881
	$conf["procs"]=$procs;
882
	#可省略參數:
883
	#無.
884
	#參考資料:
885
	#無.
886
	#備註:
887
	#無.
888
	*/
889
	public static function proc_update(&$conf){
226 liveuser 890
 
3 liveuser 891
		#初始化要回傳的變數
892
		$result=array();
893
 
894
		#初始化當前執行的函數名稱
895
		$result["function"]=__FUNCTION__;
896
 
897
		#如果 $conf 不為陣列
898
		if(gettype($conf)!="array"){
226 liveuser 899
 
3 liveuser 900
			#設置執行失敗
901
			$result["status"]="false";
226 liveuser 902
 
3 liveuser 903
			#設置執行錯誤訊息
904
			$result["error"][]="\$conf變數須為陣列形態";
905
 
906
			#如果傳入的參數為 null
907
			if($conf==null){
226 liveuser 908
 
3 liveuser 909
				#設置執行錯誤訊息
910
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 911
 
3 liveuser 912
				}#if end
913
 
914
			#回傳結果
915
			return $result;
226 liveuser 916
 
3 liveuser 917
			}#if end
226 liveuser 918
 
3 liveuser 919
		#取得使用的參數
920
		$result["argu"]=$conf;
226 liveuser 921
 
3 liveuser 922
		#函式說明:
923
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
924
		#回傳結果:
925
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
926
		#$result["error"],執行不正常結束的錯訊息陣列.
927
		#$result["simpleError"],簡單表示的錯誤訊息.
928
		#$result["function"],當前執行的函式名稱.
929
		#$result["argu"],設置給予的參數.
930
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
931
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
932
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
933
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
934
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
935
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
936
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
937
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
938
		#必填參數:
939
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
940
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
941
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
942
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
943
		#可省略參數:
944
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
945
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("procs");
946
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
947
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("array");
948
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
949
		#$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
950
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
951
		$conf["variableCheck::checkArguments"]["canNotBeEmpty"]=array("procs");
952
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
953
		#$conf["canBeEmpty"]=array();
954
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
955
		#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("wait","workingDir","envs","executeBy");
956
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
957
		#$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("wait","workingDir","envs","executeBy");
958
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
959
		#$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string","array","array","array");
960
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
961
		#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("true",null,null,null);
962
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
963
		#$conf["disallowAllSkipableVarIsEmpty"]="";
964
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
965
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
966
		#$conf["disallowAllSkipableVarNotExist"],字串,是否不允許每個可省略參數都不存在,預設為"false"代表允許,反之為"true".
967
		#$conf["disallowAllSkipableVarNotExist"]="";
968
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
969
		#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"][]=array("");
970
		#參考資料:
971
		#array_keys=>http://php.net/manual/en/function.array-keys.php
972
		#備註:
973
		#無.
974
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
975
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 976
 
3 liveuser 977
		#如果 $checkArguments["status"] 等於 "false"
978
		if($checkArguments["status"]==="false"){
226 liveuser 979
 
3 liveuser 980
			#設置錯誤識別
981
			$result["status"]="false";
226 liveuser 982
 
3 liveuser 983
			#設置錯誤訊息
984
			$result["error"]=$checkArguments;
226 liveuser 985
 
3 liveuser 986
			#回傳結果
987
			return $result;
226 liveuser 988
 
3 liveuser 989
			}#if end
226 liveuser 990
 
3 liveuser 991
		#如果 $checkArguments["passed"] 等於 "false"
992
		if($checkArguments["passed"]==="false"){
226 liveuser 993
 
3 liveuser 994
			#設置錯誤識別
995
			$result["status"]="false";
226 liveuser 996
 
3 liveuser 997
			#設置錯誤訊息
998
			$result["error"]=$checkArguments;
226 liveuser 999
 
3 liveuser 1000
			#回傳結果
1001
			return $result;
226 liveuser 1002
 
3 liveuser 1003
			}#if end
226 liveuser 1004
 
3 liveuser 1005
		#檢查 content
1006
		/*
1007
		#$result["content"],陣列,每個元素為其指令執行的結果訊息陣列,key為"status"代表執行是否正常的識別;key為"statusCode"代表程式結束後回傳給對應executeBy程式的數值;key為"output"代表標準輸出,若為resource,則代表為pipe;key為"error"代表非標準輸出,若為resource,則代表為pipe;key為"input"代表成功輸入的指令;key為"process"代表該程序經proc_open後的process source;key為"proc_get_status"代表程序的資訊.
1008
		*/
226 liveuser 1009
 
3 liveuser 1010
		#針對每個程序
1011
		foreach($conf["procs"] as $index => $proc){
226 liveuser 1012
 
3 liveuser 1013
			#函式說明:
1014
			#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
1015
			#回傳結果:
1016
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1017
			#$result["error"],執行不正常結束的錯訊息陣列.
1018
			#$result["simpleError"],簡單表示的錯誤訊息.
1019
			#$result["function"],當前執行的函式名稱.
1020
			#$result["argu"],設置給予的參數.
1021
			#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
1022
			#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
1023
			#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
1024
			#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
1025
			#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
1026
			#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
1027
			#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
1028
			#$result["notNeedVar"],字串陣列,多餘的參數名稱.
1029
			#必填參數:
1030
			#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
1031
			$conf["variableCheck::checkArguments"]["varInput"]=&$proc;
1032
			#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
1033
			$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
1034
			#可省略參數:
1035
			#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
1036
			$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("status","statusCode","content","error","input","process","proc_get_status");
1037
			#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
1038
			$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("string",null,null,null,"string","resource","array");
1039
			#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
1040
			#$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
1041
			#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
1042
			#$conf["variableCheck::checkArguments"]["canNotBeEmpty"]=array("procs");
1043
			#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
1044
			#$conf["variableCheck::checkArguments"]["canBeEmpty"]=array();
1045
			#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
1046
			#$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("wait","workingDir","envs","executeBy");
1047
			#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
1048
			#$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("error");
1049
			#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
1050
			#$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("array");
1051
			#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
1052
			#$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array("true",null,null,null);
1053
			#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
1054
			#$conf["disallowAllSkipableVarIsEmpty"]="";
1055
			#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
1056
			#$conf["disallowAllSkipableVarIsEmptyArray"]="";
1057
			#$conf["disallowAllSkipableVarNotExist"],字串,是否不允許每個可省略參數都不存在,預設為"false"代表允許,反之為"true".
1058
			#$conf["disallowAllSkipableVarNotExist"]="";
1059
			#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
1060
			#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"][]=array("");
1061
			#參考資料:
1062
			#array_keys=>http://php.net/manual/en/function.array-keys.php
1063
			#備註:
1064
			#無.
1065
			$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
1066
			unset($conf["variableCheck::checkArguments"]);
226 liveuser 1067
 
3 liveuser 1068
			#如果 $checkArguments["status"] 等於 "false"
1069
			if($checkArguments["status"]==="false"){
226 liveuser 1070
 
3 liveuser 1071
				#設置錯誤識別
1072
				$result["status"]="false";
226 liveuser 1073
 
3 liveuser 1074
				#設置錯誤訊息
1075
				$result["error"]=$checkArguments;
226 liveuser 1076
 
3 liveuser 1077
				#回傳結果
1078
				return $result;
226 liveuser 1079
 
3 liveuser 1080
				}#if end
226 liveuser 1081
 
3 liveuser 1082
			#如果 $checkArguments["passed"] 等於 "false"
1083
			if($checkArguments["passed"]==="false"){
226 liveuser 1084
 
3 liveuser 1085
				#設置錯誤識別
1086
				$result["status"]="false";
226 liveuser 1087
 
3 liveuser 1088
				#設置錯誤訊息
1089
				$result["error"]=$checkArguments;
226 liveuser 1090
 
3 liveuser 1091
				#回傳結果
1092
				return $result;
226 liveuser 1093
 
3 liveuser 1094
				}#if end
226 liveuser 1095
 
3 liveuser 1096
			#如果可能尚在執行中
1097
			if($proc["statusCode"]==="?"){
226 liveuser 1098
 
3 liveuser 1099
				#取得當下的狀態
1100
				$proc_get_status=proc_get_status($proc["process"]);
226 liveuser 1101
 
3 liveuser 1102
				#如果執行已經結束,有回傳exitcode
1103
				if($proc_get_status["exitcode"]!==-1){
226 liveuser 1104
 
3 liveuser 1105
					#設置 statusCode
1106
					$conf["procs"][$index]["statusCode"]=$proc_get_status["exitcode"];
226 liveuser 1107
 
3 liveuser 1108
					#取得標準輸出
1109
					$stdout=stream_get_contents($proc["content"]);
226 liveuser 1110
 
3 liveuser 1111
					#取得錯誤輸出
1112
					$stderr=stream_get_contents($proc["error"]);
226 liveuser 1113
 
3 liveuser 1114
					#設置執行失敗訊息
1115
					$conf["procs"][$index]["error"]=$stderr;
226 liveuser 1116
 
3 liveuser 1117
					#設置執行結果
1118
					$conf["procs"][$index]["content"]=$stdout;
226 liveuser 1119
 
3 liveuser 1120
					}#if end
226 liveuser 1121
 
3 liveuser 1122
				}#if end
249 liveuser 1123
 
1124
			#反之已經執行完畢
1125
			else{
1126
 
1127
				#如果標準輸出依然為 resource
1128
				if(gettype($proc["content"])==="resource"){
1129
 
1130
					#取得標準輸出
1131
					$stdout=stream_get_contents($proc["content"]);
226 liveuser 1132
 
249 liveuser 1133
					#設置執行結果
1134
					$conf["procs"][$index]["content"]=$stdout;
1135
 
1136
					}#if end
1137
 
1138
				#如果錯誤輸出依然為 resource
1139
				if(gettype($proc["error"])==="resource"){
1140
 
1141
					#取得錯誤輸出
1142
					$stderr=stream_get_contents($proc["error"]);
1143
 
1144
					#設置執行失敗訊息
1145
					$conf["procs"][$index]["error"]=$stderr;
1146
 
1147
					}#if end
1148
 
1149
				}#else end
1150
 
3 liveuser 1151
			}#foreach end
226 liveuser 1152
 
3 liveuser 1153
		#設置要回傳的結果
1154
		$result["content"]=$conf["procs"];
226 liveuser 1155
 
3 liveuser 1156
		#設置執行正常
1157
		$result["status"]="true";
226 liveuser 1158
 
3 liveuser 1159
		#回傳結果
1160
		return $result;
226 liveuser 1161
 
3 liveuser 1162
		}#function proc_update end
1163
 
1164
	/*
1165
	#函式說明:
1166
	#透過parallel展示多執行序運作.
1167
	#回傳結果:
1168
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1169
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
1170
	#$result["function"],當前執行的函式名稱.
1171
	#必填參數:
1172
	#無.
1173
	#可省略參數:
1174
	#無.
1175
	#參考資料:
1176
	#https://www.php.net/manual/en/class.parallel-runtime.php
1177
	#備註:
226 liveuser 1178
	#需要於編譯PHP時啟用該套件
3 liveuser 1179
	*/
1180
	public static function parallel_demo(){
226 liveuser 1181
 
3 liveuser 1182
		#初始化要回傳的結果
1183
		$result=array();
1184
 
1185
		#取得當前執行的函數名稱
1186
		$result["function"]=__FUNCTION__;
226 liveuser 1187
 
3 liveuser 1188
		#新增程序r1
1189
		$r1=new \parallel\Runtime();
226 liveuser 1190
 
3 liveuser 1191
		#r1要執行的函式
1192
		$thread_r1_function=function($conf){
226 liveuser 1193
 
3 liveuser 1194
			#提示開始
1195
			echo $conf["id"]." start".PHP_EOL;
226 liveuser 1196
 
3 liveuser 1197
			#跑 $conf["i"] 次
1198
			for($i=0;$i<$conf["i"];$i++){
226 liveuser 1199
 
3 liveuser 1200
				#題書$i數值
1201
				echo "\$i=".$i.PHP_EOL;
226 liveuser 1202
 
3 liveuser 1203
				}#for end
226 liveuser 1204
 
3 liveuser 1205
			#提示結束
1206
			echo $conf["id"]." end".PHP_EOL;
226 liveuser 1207
 
3 liveuser 1208
			};#function end
1209
 
1210
		#r1的參數
1211
		$args_r1=array();
1212
		$args_r1["id"]="r1";
1213
		$args_r1["i"]=10;
1214
 
1215
		#運行程序r1
1216
		#程序函式為 $thread_r1_function
1217
		#參數陣列為 $args_r1
1218
		$future_r1=$r1->run($thread_r1_function, $args_r1);
226 liveuser 1219
 
3 liveuser 1220
		#新增程序r2
1221
		$r2=new \parallel\Runtime();
226 liveuser 1222
 
3 liveuser 1223
		#r2要執行的函式
1224
		$thread_r2_function=function($conf){
226 liveuser 1225
 
3 liveuser 1226
			#提示開始
1227
			echo $conf["id"]." start".PHP_EOL;
226 liveuser 1228
 
3 liveuser 1229
			#跑 $conf["i"] 次
1230
			for($i=$conf["i"];$i>0;$i--){
226 liveuser 1231
 
3 liveuser 1232
				#題書$i數值
1233
				echo "\$i=".$i.PHP_EOL;
226 liveuser 1234
 
3 liveuser 1235
				}#for end
226 liveuser 1236
 
3 liveuser 1237
			#提示結束
1238
			echo $conf["id"]." end".PHP_EOL;
226 liveuser 1239
 
3 liveuser 1240
			};#function end
1241
 
1242
		#r2的參數
1243
		$args_r2=array();
1244
		$args_r2["id"]="r2";
1245
		$args_r2["i"]=10;
226 liveuser 1246
 
3 liveuser 1247
		#運行程序r2
1248
		#程序函式為 $thread__r2_function
1249
		#參數陣列為 $args_r2
1250
		$future_r2=$r2->run($thread_r2_function, $args_r2);
226 liveuser 1251
 
3 liveuser 1252
		#存放有多少在執行的程序
1253
		$futureArray=array();
1254
		$futureArray[]=$future_r1;
1255
		$futureArray[]=$future_r2;
226 liveuser 1256
 
3 liveuser 1257
		#當有程序尚未結束時
1258
		while(!empty($futureArray)){
226 liveuser 1259
 
3 liveuser 1260
			#針對每個 Future
1261
			foreach($futureArray as $index => $Future){
226 liveuser 1262
 
3 liveuser 1263
				#如果執行完成了
1264
				if($Future->done()){
226 liveuser 1265
 
3 liveuser 1266
					#移除之
1267
					unset($futureArray[$index]);
226 liveuser 1268
 
3 liveuser 1269
					}#if end
226 liveuser 1270
 
3 liveuser 1271
				}#foreach end
226 liveuser 1272
 
3 liveuser 1273
			}#while end
226 liveuser 1274
 
3 liveuser 1275
		#提示所有程序都執行結束了
1276
		echo "all done!".PHP_EOL;
226 liveuser 1277
 
3 liveuser 1278
		#設置執行正常
1279
		$result["status"]="true";
226 liveuser 1280
 
3 liveuser 1281
		#回傳結果
1282
		return $result;
226 liveuser 1283
 
3 liveuser 1284
		}#function parallel_demo end
226 liveuser 1285
 
3 liveuser 1286
	/*
1287
	#函式說明:
1288
	#透過 unix socket 來多執行序運作.
1289
	#回傳結果:
1290
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1291
	#$reuslt["error"],執行不正常結束的錯訊息陣列.
1292
	#$result["function"],當前執行的函式名稱.
1293
	#$result["argu"],使用的參數.
1294
	#$result["content"],陣列,每個元素為cmds參數中個別元素其指令傳送後得到的uuid,可透過sock::getProcInfo來查詢結果.
1295
	#必填參數:
1296
	#$conf["cmds"],字串陣列,每個元素代表要執行的指令與參數.
1297
	#$conf["cmds"]=array();
1298
	#可省略參數:
1299
	#$conf["sock"],字串,要連線的 usr/bin/qbpwcf-sock.php 所產生的 unix domain socket 路徑與名稱,預設為 qbpwcf_usock_path.
1300
	#$conf["sock"]=qbpwcf_usock_path;
1301
	#參考資料:
1302
	#無
1303
	#備註:
1304
	#需要透過sock::unixDomainSockServer來運行.
1305
	*/
1306
	public static function socket(&$conf){
226 liveuser 1307
 
3 liveuser 1308
		#初始化要回傳的變數
1309
		$result=array();
1310
 
1311
		#初始化當前執行的函數名稱
1312
		$result["function"]=__FUNCTION__;
1313
 
1314
		#如果 $conf 不為陣列
1315
		if(gettype($conf)!="array"){
226 liveuser 1316
 
3 liveuser 1317
			#設置執行失敗
1318
			$result["status"]="false";
226 liveuser 1319
 
3 liveuser 1320
			#設置執行錯誤訊息
1321
			$result["error"][]="\$conf變數須為陣列形態";
1322
 
1323
			#如果傳入的參數為 null
1324
			if($conf==null){
226 liveuser 1325
 
3 liveuser 1326
				#設置執行錯誤訊息
1327
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
226 liveuser 1328
 
3 liveuser 1329
				}#if end
1330
 
1331
			#回傳結果
1332
			return $result;
226 liveuser 1333
 
3 liveuser 1334
			}#if end
226 liveuser 1335
 
3 liveuser 1336
		#取得使用的參數
1337
		$result["argu"]=$conf;
226 liveuser 1338
 
3 liveuser 1339
		#函式說明:
1340
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
1341
		#回傳結果:
1342
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1343
		#$result["error"],執行不正常結束的錯訊息陣列.
1344
		#$result["simpleError"],簡單表示的錯誤訊息.
1345
		#$result["function"],當前執行的函式名稱.
1346
		#$result["argu"],設置給予的參數.
1347
		#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
1348
		#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
1349
		#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
1350
		#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
1351
		#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
1352
		#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
1353
		#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
1354
		#$result["notNeedVar"],字串陣列,多餘的參數名稱.
1355
		#必填參數:
1356
		#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
1357
		$conf["variableCheck::checkArguments"]["varInput"]=&$conf;
1358
		#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
1359
		$conf["variableCheck::checkArguments"]["referenceVarKey"]="variableCheck::checkArguments";
1360
		#可省略參數:
1361
		#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
1362
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableName"]=array("cmds");
1363
		#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
1364
		$conf["variableCheck::checkArguments"]["mustBeFilledVariableType"]=array("array");
1365
		#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
1366
		$conf["variableCheck::checkArguments"]["canBeEmptyString"]="false";
1367
		#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
1368
		#$conf["canNotBeEmpty"]=array();
1369
		#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
1370
		#$conf["canBeEmpty"]=array();
1371
		#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
1372
		$conf["variableCheck::checkArguments"]["skipableVariableCanNotBeEmpty"]=array("sock");
1373
		#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
1374
		$conf["variableCheck::checkArguments"]["skipableVariableName"]=array("sock");
1375
		#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
1376
		$conf["variableCheck::checkArguments"]["skipableVariableType"]=array("string");
1377
		#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
1378
		$conf["variableCheck::checkArguments"]["skipableVarDefaultValue"]=array(null);
1379
		#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
1380
		#$conf["disallowAllSkipableVarIsEmpty"]="";
1381
		#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
1382
		#$conf["disallowAllSkipableVarIsEmptyArray"]="";
1383
		#$conf["disallowAllSkipableVarNotExist"],字串,是否不允許每個可省略參數都不存在,預設為"false"代表允許,反之為"true".
1384
		#$conf["disallowAllSkipableVarNotExist"]="";
1385
		#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
1386
		#$conf["variableCheck::checkArguments"]["arrayCountEqualCheck"][]=array("");
1387
		#參考資料:
1388
		#array_keys=>http://php.net/manual/en/function.array-keys.php
1389
		#備註:
1390
		#無.
1391
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
1392
		unset($conf["variableCheck::checkArguments"]);
226 liveuser 1393
 
3 liveuser 1394
		#如果 $checkArguments["status"] 等於 "false"
1395
		if($checkArguments["status"]==="false"){
226 liveuser 1396
 
3 liveuser 1397
			#設置錯誤識別
1398
			$result["status"]="false";
226 liveuser 1399
 
3 liveuser 1400
			#設置錯誤訊息
1401
			$result["error"]=$checkArguments;
226 liveuser 1402
 
3 liveuser 1403
			#回傳結果
1404
			return $result;
226 liveuser 1405
 
3 liveuser 1406
			}#if end
226 liveuser 1407
 
3 liveuser 1408
		#如果 $checkArguments["passed"] 等於 "false"
1409
		if($checkArguments["passed"]==="false"){
226 liveuser 1410
 
3 liveuser 1411
			#設置錯誤識別
1412
			$result["status"]="false";
226 liveuser 1413
 
3 liveuser 1414
			#設置錯誤訊息
1415
			$result["error"]=$checkArguments;
226 liveuser 1416
 
3 liveuser 1417
			#回傳結果
1418
			return $result;
226 liveuser 1419
 
3 liveuser 1420
			}#if end
226 liveuser 1421
 
3 liveuser 1422
		#針對每個指令
1423
		foreach($conf["cmds"] as $cmd){
226 liveuser 1424
 
3 liveuser 1425
			#連線到 usr/bin/qbpwcf-usock.php 產生的  unix domain socket,運行指定的指令.
1426
			#回傳結果:
1427
			#$result["status"],"true"代表執行正常;"false"代表執行不正常.
1428
			#$result["error"],錯誤訊息陣列.
1429
			#$result["function"],當前執行的函式名稱.
1430
			#$result["argu"],使用的參數.
1431
			#$result["content"],執行完指令的結果.
1432
			#必填參數:
1433
			#$conf["fileArgu"],字串,變數__FILE__的內容.
1434
			$conf["sock::execAnyCmdByQBPWCFunixSocket"]["fileArgu"]=__FILE__;
1435
			#$conf["command"],字串,要執行的指令名稱.
1436
			$conf["sock::execAnyCmdByQBPWCFunixSocket"]["command"]=$cmd;
1437
			#可省略參數:
226 liveuser 1438
 
3 liveuser 1439
			#如果有設置 sock 參數
1440
			if(isset($conf["sock"])){
226 liveuser 1441
 
3 liveuser 1442
				#$conf["sock"],字串,要連線的 usr/bin/qbpwcf-sock.php 所產生的 unix domain socket 路徑與名稱,預設為 qbpwcf_usock_path.
1443
				$conf["sock::execAnyCmdByQBPWCFunixSocket"]["sock"]=$conf["sock"];
226 liveuser 1444
 
3 liveuser 1445
				}#if end
226 liveuser 1446
 
3 liveuser 1447
			#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
1448
			#$conf["sock::execAnyCmdByQBPWCFunixSocket"]["argu"]=$conf["argu"];
1449
			#$conf["commandIncludeArgu"],字串,是否command含有參數,預設為"false"代表沒有;反之為"true".
1450
			$conf["sock::execAnyCmdByQBPWCFunixSocket"]["commandIncludeArgu"]="true";
1451
			#$conf["commandInBg"],字串,是否要將程序放在背景執行,再取得相關資訊,預設為"false"代表不要;反之為"true".
1452
			$conf["sock::execAnyCmdByQBPWCFunixSocket"]["commandInBg"]="true";
1453
			#參考資料:
1454
			#無.
1455
			#備註:
1456
			#無.
1457
			$execAnyCmdByQBPWCFunixSocket=sock::execAnyCmdByQBPWCFunixSocket($conf["sock::execAnyCmdByQBPWCFunixSocket"]);
1458
			unset($conf["sock::execAnyCmdByQBPWCFunixSocket"]);
226 liveuser 1459
 
3 liveuser 1460
			#如果執行失敗
1461
			if($execAnyCmdByQBPWCFunixSocket["status"]==="false"){
226 liveuser 1462
 
3 liveuser 1463
				#設置錯誤識別
1464
				$result["status"]="false";
226 liveuser 1465
 
3 liveuser 1466
				#設置錯誤訊息
1467
				$result["error"]=$execAnyCmdByQBPWCFunixSocket;
226 liveuser 1468
 
3 liveuser 1469
				#回傳結果
1470
				return $result;
226 liveuser 1471
 
3 liveuser 1472
				}#if end
226 liveuser 1473
 
3 liveuser 1474
			#debug
1475
			#var_dump(__LINE__,$execAnyCmdByQBPWCFunixSocket);
226 liveuser 1476
 
3 liveuser 1477
			#儲存收到的回應
1478
			$result["content"][]=$execAnyCmdByQBPWCFunixSocket["content"];
226 liveuser 1479
 
3 liveuser 1480
			}#foreach end
226 liveuser 1481
 
3 liveuser 1482
		#設置執行正常
1483
		$result["status"]="true";
226 liveuser 1484
 
3 liveuser 1485
		#回傳結果
1486
		return $result;
226 liveuser 1487
 
3 liveuser 1488
		}#function socket end
1489
 
226 liveuser 1490
	}#class threads end