Subversion Repositories php-qbpwcf

Rev

Rev 162 | Rev 239 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 162 Rev 226
Line 1... Line 1...
1
<?php
1
<?php
2
 
2
 
3
/*
3
/*
4
 
4
 
5
	QBPWCF, Quick Build PHP website Component base on Fedora Linux.
5
	QBPWCF, Quick Build PHP website Component base on Fedora Linux.
6
    Copyright (C) 2015~2024 Min-Jhin,Chen
6
    Copyright (C) 2014~2025 MIN ZHI, CHEN
7
 
7
 
8
    This file is part of QBPWCF.
8
    This file is part of QBPWCF.
9
 
9
 
10
    QBPWCF is free software: you can redistribute it and/or modify
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
11
    it under the terms of the GNU General Public License as published by
Line 121... Line 121...
121
	#備註:
121
	#備註:
122
	#無.
122
	#無.
123
	改用 proc 試試 https://www.php.net/manual/en/function.proc-open.php
123
	改用 proc 試試 https://www.php.net/manual/en/function.proc-open.php
124
	*/
124
	*/
125
	public static function demo(){
125
	public static function demo(){
126
	
126
 
127
		#執行demo
127
		#執行demo
128
		var_dump(self::proc_demo());
128
		var_dump(self::proc_demo());
129
	
129
 
130
		}#function demo end
130
		}#function demo end
131
 
131
 
132
	/*
132
	/*
133
	#函式說明:
133
	#函式說明:
134
	#透過proc展示多執行序運作.
134
	#透過proc展示多執行序運作.
Line 144... Line 144...
144
	#https://www.php.net/manual/en/function.proc-open.php
144
	#https://www.php.net/manual/en/function.proc-open.php
145
	#備註:
145
	#備註:
146
	#無.
146
	#無.
147
	*/
147
	*/
148
	public static function proc_demo(){
148
	public static function proc_demo(){
149
	
149
 
150
		#初始化 descriptorspec
150
		#初始化 descriptorspec
151
		$descriptorspec = array();
151
		$descriptorspec = array();
152
		
152
 
153
		#stdin is a pipe that the child will read from
153
		#stdin is a pipe that the child will read from
154
		$descriptorspec[]=array("pipe", "r");
154
		$descriptorspec[]=array("pipe", "r");
155
		
155
 
156
		#stdout is a pipe that the child will write to
156
		#stdout is a pipe that the child will write to
157
		$descriptorspec[]=array("pipe", "w");
157
		$descriptorspec[]=array("pipe", "w");
158
		
158
 
159
		#stderr is a pipe that the child will write to
159
		#stderr is a pipe that the child will write to
160
		$descriptorspec[]=array("pipe", "w");
160
		$descriptorspec[]=array("pipe", "w");
161
		
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) 
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)
163
		$cwd = null;
163
		$cwd = null;
164
		
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 
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
166
		$env = array("QBPWCF" => "Quick Build PHP Website Componment base on Fedora Linux");
166
		$env = array("QBPWCF" => "Quick Build PHP Website Componment base on Fedora Linux");
167
		
167
 
168
		#初始化儲存要多執行序執行的程式
168
		#初始化儲存要多執行序執行的程式
169
		$codes=array();
169
		$codes=array();
170
		
170
 
171
		#初始化要demo用的url
171
		#初始化要demo用的url
172
		$demoUrl="https://qbpwc.sourceforge.io/";
172
		$demoUrl="https://qbpwc.sourceforge.io/";
173
		
173
 
174
		#要多執行序執行的程式1
174
		#要多執行序執行的程式1
175
		$codes[]=array("bash","curl -v -L --ssl-reqd ".$demoUrl);
175
		$codes[]=array("bash","curl -v -L --ssl-reqd ".$demoUrl);
176
		
176
 
177
		#要多執行序執行的程式2
177
		#要多執行序執行的程式2
178
		$codes[]=array("bash","curl -v -L --ssl-reqd ".$demoUrl);
178
		$codes[]=array("bash","curl -v -L --ssl-reqd ".$demoUrl);
179
		
179
 
180
		#要多執行序執行的程式3
180
		#要多執行序執行的程式3
181
		$codes[]=array("bash","curl -v -L --ssl-reqd ".$demoUrl);
181
		$codes[]=array("bash","curl -v -L --ssl-reqd ".$demoUrl);
182
		
182
 
183
		#debug
183
		#debug
184
		#var_dump(__LINE__,$codes);
184
		#var_dump(__LINE__,$codes);
185
		
185
 
186
		#儲存每個要執行的程式資訊
186
		#儲存每個要執行的程式資訊
187
		$process=array();
187
		$process=array();
188
		
188
 
189
		#針對每個要執行的程式內容
189
		#針對每個要執行的程式內容
190
		foreach($codes as $index=>$code){
190
		foreach($codes as $index=>$code){
191
		
191
 
192
			#取得要用什麼程式執行
192
			#取得要用什麼程式執行
193
			$executeBy=$code[0];
193
			$executeBy=$code[0];
194
		
194
 
195
			#取得要執行的程式內容
195
			#取得要執行的程式內容
196
			$code=$code[1];
196
			$code=$code[1];
197
			
197
 
198
			#運行 php 程式,並儲存 resource
198
			#運行 php 程式,並儲存 resource
199
			$process[$index]["resource"] = proc_open($executeBy, $descriptorspec, $pipes, $cwd, $env);
199
			$process[$index]["resource"] = proc_open($executeBy, $descriptorspec, $pipes, $cwd, $env);
200
			
200
 
201
			#儲存對應的 pipes
201
			#儲存對應的 pipes
202
			$process[$index]["pipes"]=$pipes;
202
			$process[$index]["pipes"]=$pipes;
203
			
203
 
204
			#debug
204
			#debug
205
			#var_dump(__LINE__,$process[$index]);
205
			#var_dump(__LINE__,$process[$index]);
206
			
206
 
207
			#如果有成功執行
207
			#如果有成功執行
208
			if(is_resource($process[$index]["resource"])){
208
			if(is_resource($process[$index]["resource"])){
209
			
209
 
210
				#$pipes now looks like this:
210
				#$pipes now looks like this:
211
				#0 => writeable handle connected to child stdin
211
				#0 => writeable handle connected to child stdin
212
				#1 => readable handle connected to child stdout
212
				#1 => readable handle connected to child stdout
213
				#2 => readable handle connected to child stdin
213
				#2 => readable handle connected to child stdin
214
				
214
 
215
				#寫入內容作為 $executeBy 的輸入
215
				#寫入內容作為 $executeBy 的輸入
216
				$bytes2write=fwrite($process[$index]["pipes"][0], $code);
216
				$bytes2write=fwrite($process[$index]["pipes"][0], $code);
217
				
217
 
218
				#debug
218
				#debug
219
				#var_dump(__LINE__,$bytes2write);
219
				#var_dump(__LINE__,$bytes2write);
220
				
220
 
221
				#結束輸入
221
				#結束輸入
222
				fclose($process[$index]["pipes"][0]);
222
				fclose($process[$index]["pipes"][0]);
223
			
223
 
224
				}#if end
224
				}#if end
225
				
225
 
226
			#反之,代表運行 php 失敗
226
			#反之,代表運行 php 失敗
227
			else{
227
			else{
228
			
228
 
229
				#初始化暫存執行結果的變數
229
				#初始化暫存執行結果的變數
230
				$cmdRes=array();
230
				$cmdRes=array();
231
				
231
 
232
				#設置執行失敗
232
				#設置執行失敗
233
				$cmdRes["status"]="false";
233
				$cmdRes["status"]="false";
234
				
234
 
235
				#設置執行錯誤訊息
235
				#設置執行錯誤訊息
236
				$cmdRes["error"]="execute program by \"".$executeBy."\" failed!";
236
				$cmdRes["error"]="execute program by \"".$executeBy."\" failed!";
237
				
237
 
238
				#設置執行結果
238
				#設置執行結果
239
				$result["content"][]=$cmdRes;
239
				$result["content"][]=$cmdRes;
240
				
240
 
241
				}#else end
241
				}#else end
242
		
242
 
243
			}#foreach end
243
			}#foreach end
244
		
244
 
245
		#debug
245
		#debug
246
		#var_dump(__LINE__,$process);
246
		#var_dump(__LINE__,$process);
247
		
247
 
248
		#針對每個回饋
248
		#針對每個回饋
249
		foreach($process as $index => $ps){
249
		foreach($process as $index => $ps){
250
		
250
 
251
			#取得 proc resource
251
			#取得 proc resource
252
			$resource=$ps["resource"];
252
			$resource=$ps["resource"];
253
		
253
 
254
			#取得標準輸出
254
			#取得標準輸出
255
			$stdout=stream_get_contents($process[$index]["pipes"][1]);
255
			$stdout=stream_get_contents($process[$index]["pipes"][1]);
256
		
256
 
257
			#取得錯誤輸出
257
			#取得錯誤輸出
258
			$stderr=stream_get_contents($process[$index]["pipes"][2]);
258
			$stderr=stream_get_contents($process[$index]["pipes"][2]);
259
		
259
 
260
			#debug
260
			#debug
261
			#var_dump($stdout);
261
			#var_dump($stdout);
262
		
262
 
263
			#初始化暫存執行狀況的變數
263
			#初始化暫存執行狀況的變數
264
			$cmdRes=array();
264
			$cmdRes=array();
265
			
265
 
266
			#設置執行失敗訊息
266
			#設置執行失敗訊息
267
			$cmdRes["error"]=$stderr;
267
			$cmdRes["error"]=$stderr;
268
			
268
 
269
			#設置執行結果
269
			#設置執行結果
270
			$cmdRes["content"]=$stdout;
270
			$cmdRes["content"]=$stdout;
271
			
271
 
272
			#debug
272
			#debug
273
			#var_dump($cmdRes);
273
			#var_dump($cmdRes);
274
			
274
 
275
			#結束讀取標準輸出
275
			#結束讀取標準輸出
276
			fclose($process[$index]["pipes"][1]);
276
			fclose($process[$index]["pipes"][1]);
277
			
277
 
278
			#結束讀取錯誤輸出
278
			#結束讀取錯誤輸出
279
			fclose($process[$index]["pipes"][2]);
279
			fclose($process[$index]["pipes"][2]);
280
			
280
 
281
			#It is important that you close any pipes before calling proc_close in order to avoid a deadlock
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);
282
			$cmdRes["statusCode"] = proc_close($resource);
283
			
283
 
284
			#debug
284
			#debug
285
			#var_dump($cmdRes["statusCode"]);
285
			#var_dump($cmdRes["statusCode"]);
286
			
286
 
287
			#預設執行不正常
287
			#預設執行不正常
288
			$cmdRes["status"]="false";
288
			$cmdRes["status"]="false";
289
			
289
 
290
			#如果執行正常
290
			#如果執行正常
291
			if($cmdRes["statusCode"]===0){
291
			if($cmdRes["statusCode"]===0){
292
			
292
 
293
				#設置執行正常
293
				#設置執行正常
294
				$cmdRes["status"]="true";
294
				$cmdRes["status"]="true";
295
			
295
 
296
				}#if end
296
				}#if end
297
			
297
 
298
			#保存結果
298
			#保存結果
299
			$result["content"][]=$cmdRes;
299
			$result["content"][]=$cmdRes;
300
		
300
 
301
			#debug
301
			#debug
302
			#var_dump($result);
302
			#var_dump($result);
303
		
303
 
304
			}#foreach end
304
			}#foreach end
305
			
305
 
306
		#設置執行正常
306
		#設置執行正常
307
		$result["status"]="true";
307
		$result["status"]="true";
308
		
308
 
309
		#debug
309
		#debug
310
		#var_dump($result);
310
		#var_dump($result);
311
		
311
 
312
		#回傳結果
312
		#回傳結果
313
		return $result;
313
		return $result;
314
		
314
 
315
		}#function proc_demo end
315
		}#function proc_demo end
316
 
316
 
317
	/*
317
	/*
318
	#函式說明:
318
	#函式說明:
319
	#透過proc來多執行序運作.
319
	#透過proc來多執行序運作.
Line 343... Line 343...
343
	#https://www.php.net/manual/en/function.proc-terminate.php
343
	#https://www.php.net/manual/en/function.proc-terminate.php
344
	#備註:
344
	#備註:
345
	#若需要取得當下的執行狀況,請使用 self::proc_update 來更新.
345
	#若需要取得當下的執行狀況,請使用 self::proc_update 來更新.
346
	*/
346
	*/
347
	public static function proc(&$conf){
347
	public static function proc(&$conf){
348
	
348
 
349
		#初始化要回傳的變數
349
		#初始化要回傳的變數
350
		$result=array();
350
		$result=array();
351
 
351
 
352
		#初始化當前執行的函數名稱
352
		#初始化當前執行的函數名稱
353
		$result["function"]=__FUNCTION__;
353
		$result["function"]=__FUNCTION__;
354
 
354
 
355
		#如果 $conf 不為陣列
355
		#如果 $conf 不為陣列
356
		if(gettype($conf)!=="array"){
356
		if(gettype($conf)!=="array"){
357
			
357
 
358
			#設置執行失敗
358
			#設置執行失敗
359
			$result["status"]="false";
359
			$result["status"]="false";
360
			
360
 
361
			#設置執行錯誤訊息
361
			#設置執行錯誤訊息
362
			$result["error"][]="\$conf變數須為陣列形態";
362
			$result["error"][]="\$conf變數須為陣列形態";
363
 
363
 
364
			#如果傳入的參數為 null
364
			#如果傳入的參數為 null
365
			if($conf===null){
365
			if($conf===null){
366
				
366
 
367
				#設置執行錯誤訊息
367
				#設置執行錯誤訊息
368
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
368
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
369
				
369
 
370
				}#if end
370
				}#if end
371
 
371
 
372
			#回傳結果
372
			#回傳結果
373
			return $result;
373
			return $result;
374
			
374
 
375
			}#if end
375
			}#if end
376
	
376
 
377
		#取得使用的參數
377
		#取得使用的參數
378
		$result["argu"]=$conf;
378
		$result["argu"]=$conf;
379
		
379
 
380
		#函式說明:
380
		#函式說明:
381
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
381
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
382
		#回傳結果:
382
		#回傳結果:
383
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
383
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
384
		#$result["error"],執行不正常結束的錯訊息陣列.
384
		#$result["error"],執行不正常結束的錯訊息陣列.
Line 429... Line 429...
429
		#array_keys=>http://php.net/manual/en/function.array-keys.php
429
		#array_keys=>http://php.net/manual/en/function.array-keys.php
430
		#備註:
430
		#備註:
431
		#無.
431
		#無.
432
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
432
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
433
		unset($conf["variableCheck::checkArguments"]);
433
		unset($conf["variableCheck::checkArguments"]);
434
	
434
 
435
		#如果 $checkArguments["status"] 等於 "false"
435
		#如果 $checkArguments["status"] 等於 "false"
436
		if($checkArguments["status"]==="false"){
436
		if($checkArguments["status"]==="false"){
437
			
437
 
438
			#設置錯誤識別
438
			#設置錯誤識別
439
			$result["status"]="false";
439
			$result["status"]="false";
440
			
440
 
441
			#設置錯誤訊息
441
			#設置錯誤訊息
442
			$result["error"]=$checkArguments;
442
			$result["error"]=$checkArguments;
443
			
443
 
444
			#回傳結果
444
			#回傳結果
445
			return $result;
445
			return $result;
446
			
446
 
447
			}#if end
447
			}#if end
448
			
448
 
449
		#如果 $checkArguments["passed"] 等於 "false"
449
		#如果 $checkArguments["passed"] 等於 "false"
450
		if($checkArguments["passed"]==="false"){
450
		if($checkArguments["passed"]==="false"){
451
			
451
 
452
			#設置錯誤識別
452
			#設置錯誤識別
453
			$result["status"]="false";
453
			$result["status"]="false";
454
			
454
 
455
			#設置錯誤訊息
455
			#設置錯誤訊息
456
			$result["error"]=$checkArguments;
456
			$result["error"]=$checkArguments;
457
			
457
 
458
			#回傳結果
458
			#回傳結果
459
			return $result;
459
			return $result;
460
			
460
 
461
			}#if end
461
			}#if end
462
		
462
 
463
		#初始化 descriptorspec
463
		#初始化 descriptorspec
464
		$descriptorspec = array();
464
		$descriptorspec = array();
465
		
465
 
466
		#stdin is a pipe that the child will read from
466
		#stdin is a pipe that the child will read from
467
		$descriptorspec[]=array("pipe", "r");
467
		$descriptorspec[]=array("pipe", "r");
468
		
468
 
469
		#stdout is a pipe that the child will write to
469
		#stdout is a pipe that the child will write to
470
		$descriptorspec[]=array("pipe", "w");
470
		$descriptorspec[]=array("pipe", "w");
471
		
471
 
472
		#stderr is a pipe that the child will write to
472
		#stderr is a pipe that the child will write to
473
		$descriptorspec[]=array("pipe", "w");
473
		$descriptorspec[]=array("pipe", "w");
474
		
474
 
475
		#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) 
475
		#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)
476
		$defaultCWD = null;
476
		$defaultCWD = null;
477
		
477
 
478
		#初始化儲存每個指令的working dir.
478
		#初始化儲存每個指令的working dir.
479
		$cwds=array();
479
		$cwds=array();
480
		
480
 
481
		#有幾個指令就執行幾次
481
		#有幾個指令就執行幾次
482
		for($i=0;$i<count($conf["cmds"]);$i++){
482
		for($i=0;$i<count($conf["cmds"]);$i++){
483
		
483
 
484
			#設置預設working dir
484
			#設置預設working dir
485
			$cwds[]=$defaultCWD;
485
			$cwds[]=$defaultCWD;
486
		
486
 
487
			}#for end
487
			}#for end
488
		
488
 
489
		#如果有設置 workingDir
489
		#如果有設置 workingDir
490
		if(isset($conf["workingDir"])){
490
		if(isset($conf["workingDir"])){
491
		
491
 
492
			#如果數量符合
492
			#如果數量符合
493
			if(count($conf["cmds"])===count($conf["workingDir"])){
493
			if(count($conf["cmds"])===count($conf["workingDir"])){
494
			
494
 
495
				#有幾個指令就執行幾次
495
				#有幾個指令就執行幾次
496
				for($i=0;$i<count($conf["cmds"]);$i++){
496
				for($i=0;$i<count($conf["cmds"]);$i++){
497
				
497
 
498
					#預設為指定數值
498
					#預設為指定數值
499
					$cwds[]=$conf["workingDir"][$i];
499
					$cwds[]=$conf["workingDir"][$i];
500
				
500
 
501
					}#for end
501
					}#for end
502
			
502
 
503
				}#if end
503
				}#if end
504
		
504
 
505
			}#if end
505
			}#if end
506
		
506
 
507
		#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 
507
		#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
508
		$defaultENV = array("QBPWCF" => "Quick Build PHP Website Componment base on Fedora Linux");
508
		$defaultENV = array("QBPWCF" => "Quick Build PHP Website Componment base on Fedora Linux");
509
		
509
 
510
		#初始化儲存每個指令的環境變數.
510
		#初始化儲存每個指令的環境變數.
511
		$envs=array();
511
		$envs=array();
512
		
512
 
513
		#有幾個指令就執行幾次
513
		#有幾個指令就執行幾次
514
		for($i=0;$i<count($conf["cmds"]);$i++){
514
		for($i=0;$i<count($conf["cmds"]);$i++){
515
		
515
 
516
			#設置預設環境變數
516
			#設置預設環境變數
517
			$envs[]=$defaultENV;
517
			$envs[]=$defaultENV;
518
		
518
 
519
			}#for end
519
			}#for end
520
			
520
 
521
		#如果有設置 envs
521
		#如果有設置 envs
522
		if(isset($conf["envs"])){
522
		if(isset($conf["envs"])){
523
		
523
 
524
			#如果數量符合
524
			#如果數量符合
525
			if(count($conf["cmds"])===count($conf["envs"])){
525
			if(count($conf["cmds"])===count($conf["envs"])){
526
			
526
 
527
				#有幾個指令就執行幾次
527
				#有幾個指令就執行幾次
528
				for($i=0;$i<count($conf["cmds"]);$i++){
528
				for($i=0;$i<count($conf["cmds"]);$i++){
529
				
529
 
530
					#預設為指定數值
530
					#預設為指定數值
531
					$envs[]=$conf["envs"][$i];
531
					$envs[]=$conf["envs"][$i];
532
				
532
 
533
					}#for end
533
					}#for end
534
			
534
 
535
				}#if end
535
				}#if end
536
		
536
 
537
			}#if end
537
			}#if end
538
		
538
 
539
		#預設程式都透過 bash 來執行
539
		#預設程式都透過 bash 來執行
540
		$defaultExecBy="bash";
540
		$defaultExecBy="bash";
541
		
541
 
542
		#初始化儲存每個指令要透過什麼程式執行
542
		#初始化儲存每個指令要透過什麼程式執行
543
		$executeBys=array();
543
		$executeBys=array();
544
		
544
 
545
		#有幾個指令就執行幾次
545
		#有幾個指令就執行幾次
546
		for($i=0;$i<count($conf["cmds"]);$i++){
546
		for($i=0;$i<count($conf["cmds"]);$i++){
547
		
547
 
548
			#設置預設環境變數
548
			#設置預設環境變數
549
			$executeBys[]=$defaultExecBy;
549
			$executeBys[]=$defaultExecBy;
550
		
550
 
551
			}#for end
551
			}#for end
552
		
552
 
553
		#如果有設置 executeBy
553
		#如果有設置 executeBy
554
		if(isset($conf["executeBy"])){
554
		if(isset($conf["executeBy"])){
555
		
555
 
556
			#如果數量符合
556
			#如果數量符合
557
			if(count($conf["cmds"])===count($conf["executeBy"])){
557
			if(count($conf["cmds"])===count($conf["executeBy"])){
558
			
558
 
559
				#初始化儲存每個指令要透過什麼程式執行
559
				#初始化儲存每個指令要透過什麼程式執行
560
				$executeBys=array();
560
				$executeBys=array();
561
			
561
 
562
				#有幾個指令就執行幾次
562
				#有幾個指令就執行幾次
563
				for($i=0;$i<count($conf["cmds"]);$i++){
563
				for($i=0;$i<count($conf["cmds"]);$i++){
564
				
564
 
565
					#預設為指定數值
565
					#預設為指定數值
566
					$executeBys[]=$conf["executeBy"][$i];
566
					$executeBys[]=$conf["executeBy"][$i];
567
				
567
 
568
					}#for end
568
					}#for end
569
			
569
 
570
				}#if end
570
				}#if end
571
		
571
 
572
			}#if end
572
			}#if end
573
		
573
 
574
		#儲存每個要執行的程式資訊
574
		#儲存每個要執行的程式資訊
575
		$process=array();
575
		$process=array();
576
		
576
 
577
		#debug
577
		#debug
578
		#var_dump(__LINE__,$executeBys);
578
		#var_dump(__LINE__,$executeBys);
579
		
579
 
580
		#針對每個指令與參數字串
580
		#針對每個指令與參數字串
581
		foreach($conf["cmds"] as $index=>$cmdAndArguStr){
581
		foreach($conf["cmds"] as $index=>$cmdAndArguStr){
582
		
582
 
583
			#debug
583
			#debug
584
			#var_dump(__LINE__,$executeBys[$index]);
584
			#var_dump(__LINE__,$executeBys[$index]);
585
		
585
 
586
			#運行 php 程式,並儲存 resource
586
			#運行 php 程式,並儲存 resource
587
			$process[$index]["resource"] = proc_open($executeBys[$index], $descriptorspec, $pipes, $cwds[$index], $envs[$index]);
587
			$process[$index]["resource"] = proc_open($executeBys[$index], $descriptorspec, $pipes, $cwds[$index], $envs[$index]);
588
			
588
 
589
			#儲存對應的 pipes
589
			#儲存對應的 pipes
590
			$process[$index]["pipes"]=$pipes;
590
			$process[$index]["pipes"]=$pipes;
591
			
591
 
592
			#如果有成功執行
592
			#如果有成功執行
593
			if(is_resource($process[$index]["resource"])){
593
			if(is_resource($process[$index]["resource"])){
594
			
594
 
595
				#$pipes now looks like this:
595
				#$pipes now looks like this:
596
				#0 => writeable handle connected to child stdin
596
				#0 => writeable handle connected to child stdin
597
				#1 => readable handle connected to child stdout
597
				#1 => readable handle connected to child stdout
598
				#2 => readable handle connected to child stdin
598
				#2 => readable handle connected to child stdin
599
				
599
 
600
				#寫入內容作為 $executeBy 的輸入
600
				#寫入內容作為 $executeBy 的輸入
601
				$bytes2write=fwrite($process[$index]["pipes"][0], $cmdAndArguStr);
601
				$bytes2write=fwrite($process[$index]["pipes"][0], $cmdAndArguStr);
602
				
602
 
603
				#結束輸入
603
				#結束輸入
604
				fclose($process[$index]["pipes"][0]);
604
				fclose($process[$index]["pipes"][0]);
605
			
605
 
606
				#如果有完整寫入指令
606
				#如果有完整寫入指令
607
				if($bytes2write===strlen($cmdAndArguStr)){
607
				if($bytes2write===strlen($cmdAndArguStr)){
608
				
608
 
609
					#記錄輸入的指令
609
					#記錄輸入的指令
610
					$process[$index]["input"]=$cmdAndArguStr;
610
					$process[$index]["input"]=$cmdAndArguStr;
611
				
611
 
612
					}#if end
612
					}#if end
613
					
613
 
614
				#反之
614
				#反之
615
				else{
615
				else{
616
				
616
 
617
					#設置錯誤識別
617
					#設置錯誤識別
618
					$result["status"]="false";
618
					$result["status"]="false";
619
					
619
 
620
					#設置錯誤訊息
620
					#設置錯誤訊息
621
					$result["error"][]="將指令(".$cmdAndArguStr.")輸入(".$executeBys[$index].")失敗";
621
					$result["error"][]="將指令(".$cmdAndArguStr.")輸入(".$executeBys[$index].")失敗";
622
					
622
 
623
					#回傳結果
623
					#回傳結果
624
					return $result;
624
					return $result;
625
				
625
 
626
					}#else end
626
					}#else end
627
			
627
 
628
				}#if end
628
				}#if end
629
				
629
 
630
			#反之,代表運行 $executeBys[$index] 失敗
630
			#反之,代表運行 $executeBys[$index] 失敗
631
			else{
631
			else{
632
			
632
 
633
				#初始化暫存執行結果的變數
633
				#初始化暫存執行結果的變數
634
				$cmdRes=array();
634
				$cmdRes=array();
635
				
635
 
636
				#設置執行失敗
636
				#設置執行失敗
637
				$cmdRes["status"]="false";
637
				$cmdRes["status"]="false";
638
				
638
 
639
				#設置執行錯誤訊息
639
				#設置執行錯誤訊息
640
				$cmdRes["error"]="execute program by \"".$executeBys[$index]."\" failed!";
640
				$cmdRes["error"]="execute program by \"".$executeBys[$index]."\" failed!";
641
				
641
 
642
				#設置執行結果
642
				#設置執行結果
643
				$result["content"][]=$cmdRes;
643
				$result["content"][]=$cmdRes;
644
				
644
 
645
				}#else end
645
				}#else end
646
				
646
 
647
			}#foreach end
647
			}#foreach end
648
		
648
 
649
		#針對每個回饋
649
		#針對每個回饋
650
		foreach($process as $index => $ps){
650
		foreach($process as $index => $ps){
651
		
651
 
652
			#取得 proc resource
652
			#取得 proc resource
653
			$resource=&$ps["resource"];
653
			$resource=&$ps["resource"];
654
		
654
 
655
			#取得標準輸出的pipe
655
			#取得標準輸出的pipe
656
			$stdout=$process[$index]["pipes"][1];
656
			$stdout=$process[$index]["pipes"][1];
657
			
657
 
658
			#取得非標準輸出的pipe
658
			#取得非標準輸出的pipe
659
			$stderr=$process[$index]["pipes"][2];
659
			$stderr=$process[$index]["pipes"][2];
660
		
660
 
661
			#如果需要等待程序執行完
661
			#如果需要等待程序執行完
662
			if($conf["wait"]==="true"){
662
			if($conf["wait"]==="true"){
663
			
663
 
664
				#如果有設置  timeout
664
				#如果有設置  timeout
665
				if(isset($conf["timeout"])){
665
				if(isset($conf["timeout"])){
666
				
666
 
667
					#如果有設置該程序的 timeout
667
					#如果有設置該程序的 timeout
668
					if(isset($conf["timeout"][$index])){
668
					if(isset($conf["timeout"][$index])){
669
						
669
 
670
						#避免 timeout 仍然會等待程序結束 - start
670
						#避免 timeout 仍然會等待程序結束 - start
671
					
671
 
672
						#不讓標準輸出會阻擋程式執行
672
						#不讓標準輸出會阻擋程式執行
673
						stream_set_blocking($stdout,false);
673
						stream_set_blocking($stdout,false);
674
						
674
 
675
						#不讓錯誤輸出會阻擋程式執行
675
						#不讓錯誤輸出會阻擋程式執行
676
						stream_set_blocking($stderr,false);
676
						stream_set_blocking($stderr,false);
677
						
677
 
678
						#避免 timeout 仍然會等待程序結束 - end
678
						#避免 timeout 仍然會等待程序結束 - end
679
						
679
 
680
						}#if end
680
						}#if end
681
					
681
 
682
					}#if end
682
					}#if end
683
				
683
 
684
				}#if end
684
				}#if end
685
			
685
 
686
			#反之
686
			#反之
687
			else{
687
			else{
688
			
688
 
689
				#不讓標準輸出會阻擋程式執行
689
				#不讓標準輸出會阻擋程式執行
690
				stream_set_blocking($stdout,false);
690
				stream_set_blocking($stdout,false);
691
				
691
 
692
				#不讓錯誤輸出會阻擋程式執行
692
				#不讓錯誤輸出會阻擋程式執行
693
				stream_set_blocking($stderr,false);
693
				stream_set_blocking($stderr,false);
694
			
694
 
695
				}#else end
695
				}#else end
696
			
696
 
697
			#初始化暫存執行狀況的變數
697
			#初始化暫存執行狀況的變數
698
			$cmdRes=array();
698
			$cmdRes=array();
699
			
699
 
700
			#取得 proc_open 後的 process resource
700
			#取得 proc_open 後的 process resource
701
			$cmdRes["process"]=$resource;
701
			$cmdRes["process"]=$resource;
702
			
702
 
703
			#取得 process 的狀態,包含 pid 等資訊,若程式已經結束,則會自動將pipes關閉,再proc_close.
703
			#取得 process 的狀態,包含 pid 等資訊,若程式已經結束,則會自動將pipes關閉,再proc_close.
704
			$cmdRes["proc_get_status"]=proc_get_status($resource);
704
			$cmdRes["proc_get_status"]=proc_get_status($resource);
705
			
705
 
706
			#預設 statusCode 為 "?" 代表尚未 proc_close
706
			#預設 statusCode 為 "?" 代表尚未 proc_close
707
			$cmdRes["statusCode"]="?";
707
			$cmdRes["statusCode"]="?";
708
			
708
 
709
			#如果執行已經結束,有回傳exitcode
709
			#如果執行已經結束,有回傳exitcode
710
			if($cmdRes["proc_get_status"]["exitcode"]!==-1){
710
			if($cmdRes["proc_get_status"]["exitcode"]!==-1){
711
			
711
 
712
				#設置 statusCode
712
				#設置 statusCode
713
				$cmdRes["statusCode"]=&$cmdRes["proc_get_status"]["exitcode"];
713
				$cmdRes["statusCode"]=&$cmdRes["proc_get_status"]["exitcode"];
714
			
714
 
715
				}#if end
715
				}#if end
716
			
716
 
717
			#反之未執行完畢,且需要等待程序執行完
717
			#反之未執行完畢,且需要等待程序執行完
718
			else if($conf["wait"]==="true"){
718
			else if($conf["wait"]==="true"){
719
			
719
 
720
				#預設已經執行0秒
720
				#預設已經執行0秒
721
				$runtime=0;
721
				$runtime=0;
722
			
722
 
723
				#無窮迴圈
723
				#無窮迴圈
724
				while(true){
724
				while(true){
725
				
725
 
726
					#debug
726
					#debug
727
					#var_dump(__LINE__,$runtime,$conf["timeout"]);
727
					#var_dump(__LINE__,$runtime,$conf["timeout"]);
728
				
728
 
729
					#如果有設置  timeout
729
					#如果有設置  timeout
730
					if(isset($conf["timeout"])){
730
					if(isset($conf["timeout"])){
731
					
731
 
732
						#如果有設置該程序的 timeout
732
						#如果有設置該程序的 timeout
733
						if(isset($conf["timeout"][$index])){
733
						if(isset($conf["timeout"][$index])){
734
						
734
 
735
							#如果執行秒數逾時了
735
							#如果執行秒數逾時了
736
							if( $runtime > (int)($conf["timeout"][$index]) ){
736
							if( $runtime > (int)($conf["timeout"][$index]) ){
737
							
737
 
738
								#debug
738
								#debug
739
								#var_dump(__LINE__,"timeout!");
739
								#var_dump(__LINE__,"timeout!");
740
							
740
 
741
								#關閉標準輸出的pipe
741
								#關閉標準輸出的pipe
742
								fclose($stdout);
742
								fclose($stdout);
743
								
743
 
744
								#關閉標準錯誤的pipe
744
								#關閉標準錯誤的pipe
745
								fclose($stderr);
745
								fclose($stderr);
746
							
746
 
747
								#強制結束程序
747
								#強制結束程序
748
								proc_terminate($resource);
748
								proc_terminate($resource);
749
							
749
 
750
								#結束proc.
750
								#結束proc.
751
								proc_close($resource);
751
								proc_close($resource);
752
								
752
 
753
								#結束無窮迴圈
753
								#結束無窮迴圈
754
								break;
754
								break;
755
							
755
 
756
								}#if end
756
								}#if end
757
						
757
 
758
							}#if end
758
							}#if end
759
					
759
 
760
						}#if end
760
						}#if end
761
				
761
 
762
					#取得 process 的狀態,包含 pid 等資訊,若程式已經結束,則會自動將pipes關閉,再proc_close.
762
					#取得 process 的狀態,包含 pid 等資訊,若程式已經結束,則會自動將pipes關閉,再proc_close.
763
					$cmdRes["proc_get_status"]=proc_get_status($resource);
763
					$cmdRes["proc_get_status"]=proc_get_status($resource);
764
					
764
 
765
					#如果執行已經結束,有回傳exitcode
765
					#如果執行已經結束,有回傳exitcode
766
					if($cmdRes["proc_get_status"]["exitcode"]!==-1){
766
					if($cmdRes["proc_get_status"]["exitcode"]!==-1){
767
					
767
 
768
						#設置 statusCode
768
						#設置 statusCode
769
						$cmdRes["statusCode"]=&$cmdRes["proc_get_status"]["exitcode"];
769
						$cmdRes["statusCode"]=&$cmdRes["proc_get_status"]["exitcode"];
770
					
770
 
771
						#結束無窮迴圈
771
						#結束無窮迴圈
772
						break;
772
						break;
773
					
773
 
774
						}#if end
774
						}#if end
775
						
775
 
776
					#等待1秒鐘
776
					#等待1秒鐘
777
					sleep(1);
777
					sleep(1);
778
					
778
 
779
					#執行秒數+1
779
					#執行秒數+1
780
					$runtime++;
780
					$runtime++;
781
				
781
 
782
					}#while end
782
					}#while end
783
					
783
 
784
				}#if end
784
				}#if end
785
			
785
 
786
			#設置執行失敗訊息
786
			#設置執行失敗訊息
787
			$cmdRes["error"]=$stderr;
787
			$cmdRes["error"]=$stderr;
788
			
788
 
789
			#設置執行結果
789
			#設置執行結果
790
			$cmdRes["content"]=$stdout;
790
			$cmdRes["content"]=$stdout;
791
			
791
 
792
			#如果有成功輸入指令
792
			#如果有成功輸入指令
793
			if(isset($process[$index]["input"])){
793
			if(isset($process[$index]["input"])){
794
			
794
 
795
				#設置有輸入的指令
795
				#設置有輸入的指令
796
				$cmdRes["input"]=$process[$index]["input"];
796
				$cmdRes["input"]=$process[$index]["input"];
797
			
797
 
798
				}#if end
798
				}#if end
799
			
799
 
800
			#預設執行不正常
800
			#預設執行不正常
801
			$cmdRes["status"]="false";
801
			$cmdRes["status"]="false";
802
			
802
 
803
			#如果執行正常 或 等於"?"(代表尚未 proc_close)
803
			#如果執行正常 或 等於"?"(代表尚未 proc_close)
804
			if($cmdRes["statusCode"]===0 || $cmdRes["statusCode"]==="?"){
804
			if($cmdRes["statusCode"]===0 || $cmdRes["statusCode"]==="?"){
805
			
805
 
806
				#設置執行正常
806
				#設置執行正常
807
				$cmdRes["status"]="true";
807
				$cmdRes["status"]="true";
808
			
808
 
809
				}#if end
809
				}#if end
810
			
810
 
811
			#保存結果
811
			#保存結果
812
			$result["content"][]=$cmdRes;
812
			$result["content"][]=$cmdRes;
813
		
813
 
814
			}#foreach end
814
			}#foreach end
815
		
815
 
816
		#設置執行正常
816
		#設置執行正常
817
		$result["status"]="true";
817
		$result["status"]="true";
818
		
818
 
819
		#回傳結果
819
		#回傳結果
820
		return $result;
820
		return $result;
821
	
821
 
822
		}#function proc end
822
		}#function proc end
823
 
823
 
824
	/*
824
	/*
825
	#函式說明:
825
	#函式說明:
826
	#更新透過proc執行的多程序資訊.
826
	#更新透過proc執行的多程序資訊.
Line 839... Line 839...
839
	#無.
839
	#無.
840
	#備註:
840
	#備註:
841
	#無.
841
	#無.
842
	*/
842
	*/
843
	public static function proc_update(&$conf){
843
	public static function proc_update(&$conf){
844
	
844
 
845
		#初始化要回傳的變數
845
		#初始化要回傳的變數
846
		$result=array();
846
		$result=array();
847
 
847
 
848
		#初始化當前執行的函數名稱
848
		#初始化當前執行的函數名稱
849
		$result["function"]=__FUNCTION__;
849
		$result["function"]=__FUNCTION__;
850
 
850
 
851
		#如果 $conf 不為陣列
851
		#如果 $conf 不為陣列
852
		if(gettype($conf)!="array"){
852
		if(gettype($conf)!="array"){
853
			
853
 
854
			#設置執行失敗
854
			#設置執行失敗
855
			$result["status"]="false";
855
			$result["status"]="false";
856
			
856
 
857
			#設置執行錯誤訊息
857
			#設置執行錯誤訊息
858
			$result["error"][]="\$conf變數須為陣列形態";
858
			$result["error"][]="\$conf變數須為陣列形態";
859
 
859
 
860
			#如果傳入的參數為 null
860
			#如果傳入的參數為 null
861
			if($conf==null){
861
			if($conf==null){
862
				
862
 
863
				#設置執行錯誤訊息
863
				#設置執行錯誤訊息
864
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
864
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
865
				
865
 
866
				}#if end
866
				}#if end
867
 
867
 
868
			#回傳結果
868
			#回傳結果
869
			return $result;
869
			return $result;
870
			
870
 
871
			}#if end
871
			}#if end
872
	
872
 
873
		#取得使用的參數
873
		#取得使用的參數
874
		$result["argu"]=$conf;
874
		$result["argu"]=$conf;
875
		
875
 
876
		#函式說明:
876
		#函式說明:
877
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
877
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
878
		#回傳結果:
878
		#回傳結果:
879
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
879
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
880
		#$result["error"],執行不正常結束的錯訊息陣列.
880
		#$result["error"],執行不正常結束的錯訊息陣列.
Line 925... Line 925...
925
		#array_keys=>http://php.net/manual/en/function.array-keys.php
925
		#array_keys=>http://php.net/manual/en/function.array-keys.php
926
		#備註:
926
		#備註:
927
		#無.
927
		#無.
928
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
928
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
929
		unset($conf["variableCheck::checkArguments"]);
929
		unset($conf["variableCheck::checkArguments"]);
930
	
930
 
931
		#如果 $checkArguments["status"] 等於 "false"
931
		#如果 $checkArguments["status"] 等於 "false"
932
		if($checkArguments["status"]==="false"){
932
		if($checkArguments["status"]==="false"){
933
			
933
 
934
			#設置錯誤識別
934
			#設置錯誤識別
935
			$result["status"]="false";
935
			$result["status"]="false";
936
			
936
 
937
			#設置錯誤訊息
937
			#設置錯誤訊息
938
			$result["error"]=$checkArguments;
938
			$result["error"]=$checkArguments;
939
			
939
 
940
			#回傳結果
940
			#回傳結果
941
			return $result;
941
			return $result;
942
			
942
 
943
			}#if end
943
			}#if end
944
			
944
 
945
		#如果 $checkArguments["passed"] 等於 "false"
945
		#如果 $checkArguments["passed"] 等於 "false"
946
		if($checkArguments["passed"]==="false"){
946
		if($checkArguments["passed"]==="false"){
947
			
947
 
948
			#設置錯誤識別
948
			#設置錯誤識別
949
			$result["status"]="false";
949
			$result["status"]="false";
950
			
950
 
951
			#設置錯誤訊息
951
			#設置錯誤訊息
952
			$result["error"]=$checkArguments;
952
			$result["error"]=$checkArguments;
953
			
953
 
954
			#回傳結果
954
			#回傳結果
955
			return $result;
955
			return $result;
956
			
956
 
957
			}#if end
957
			}#if end
958
				
958
 
959
		#檢查 content
959
		#檢查 content
960
		/*
960
		/*
961
		#$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"代表程序的資訊.
961
		#$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"代表程序的資訊.
962
		*/
962
		*/
963
		
963
 
964
		#針對每個程序
964
		#針對每個程序
965
		foreach($conf["procs"] as $index => $proc){
965
		foreach($conf["procs"] as $index => $proc){
966
		
966
 
967
			#函式說明:
967
			#函式說明:
968
			#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
968
			#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
969
			#回傳結果:
969
			#回傳結果:
970
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
970
			#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
971
			#$result["error"],執行不正常結束的錯訊息陣列.
971
			#$result["error"],執行不正常結束的錯訊息陣列.
Line 1016... Line 1016...
1016
			#array_keys=>http://php.net/manual/en/function.array-keys.php
1016
			#array_keys=>http://php.net/manual/en/function.array-keys.php
1017
			#備註:
1017
			#備註:
1018
			#無.
1018
			#無.
1019
			$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
1019
			$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
1020
			unset($conf["variableCheck::checkArguments"]);
1020
			unset($conf["variableCheck::checkArguments"]);
1021
		
1021
 
1022
			#如果 $checkArguments["status"] 等於 "false"
1022
			#如果 $checkArguments["status"] 等於 "false"
1023
			if($checkArguments["status"]==="false"){
1023
			if($checkArguments["status"]==="false"){
1024
				
1024
 
1025
				#設置錯誤識別
1025
				#設置錯誤識別
1026
				$result["status"]="false";
1026
				$result["status"]="false";
1027
				
1027
 
1028
				#設置錯誤訊息
1028
				#設置錯誤訊息
1029
				$result["error"]=$checkArguments;
1029
				$result["error"]=$checkArguments;
1030
				
1030
 
1031
				#回傳結果
1031
				#回傳結果
1032
				return $result;
1032
				return $result;
1033
				
1033
 
1034
				}#if end
1034
				}#if end
1035
				
1035
 
1036
			#如果 $checkArguments["passed"] 等於 "false"
1036
			#如果 $checkArguments["passed"] 等於 "false"
1037
			if($checkArguments["passed"]==="false"){
1037
			if($checkArguments["passed"]==="false"){
1038
				
1038
 
1039
				#設置錯誤識別
1039
				#設置錯誤識別
1040
				$result["status"]="false";
1040
				$result["status"]="false";
1041
				
1041
 
1042
				#設置錯誤訊息
1042
				#設置錯誤訊息
1043
				$result["error"]=$checkArguments;
1043
				$result["error"]=$checkArguments;
1044
				
1044
 
1045
				#回傳結果
1045
				#回傳結果
1046
				return $result;
1046
				return $result;
1047
				
1047
 
1048
				}#if end
1048
				}#if end
1049
				
1049
 
1050
			#如果可能尚在執行中
1050
			#如果可能尚在執行中
1051
			if($proc["statusCode"]==="?"){
1051
			if($proc["statusCode"]==="?"){
1052
			
1052
 
1053
				#取得當下的狀態
1053
				#取得當下的狀態
1054
				$proc_get_status=proc_get_status($proc["process"]);
1054
				$proc_get_status=proc_get_status($proc["process"]);
1055
			
1055
 
1056
				#如果執行已經結束,有回傳exitcode
1056
				#如果執行已經結束,有回傳exitcode
1057
				if($proc_get_status["exitcode"]!==-1){
1057
				if($proc_get_status["exitcode"]!==-1){
1058
				
1058
 
1059
					#設置 statusCode
1059
					#設置 statusCode
1060
					$conf["procs"][$index]["statusCode"]=$proc_get_status["exitcode"];
1060
					$conf["procs"][$index]["statusCode"]=$proc_get_status["exitcode"];
1061
				
1061
 
1062
					#取得標準輸出
1062
					#取得標準輸出
1063
					$stdout=stream_get_contents($proc["content"]);
1063
					$stdout=stream_get_contents($proc["content"]);
1064
				
1064
 
1065
					#取得錯誤輸出
1065
					#取得錯誤輸出
1066
					$stderr=stream_get_contents($proc["error"]);
1066
					$stderr=stream_get_contents($proc["error"]);
1067
				
1067
 
1068
					#設置執行失敗訊息
1068
					#設置執行失敗訊息
1069
					$conf["procs"][$index]["error"]=$stderr;
1069
					$conf["procs"][$index]["error"]=$stderr;
1070
					
1070
 
1071
					#設置執行結果
1071
					#設置執行結果
1072
					$conf["procs"][$index]["content"]=$stdout;
1072
					$conf["procs"][$index]["content"]=$stdout;
1073
				
1073
 
1074
					}#if end
1074
					}#if end
1075
					
1075
 
1076
				}#if end
1076
				}#if end
1077
		
1077
 
1078
			}#foreach end
1078
			}#foreach end
1079
			
1079
 
1080
		#設置要回傳的結果
1080
		#設置要回傳的結果
1081
		$result["content"]=$conf["procs"];
1081
		$result["content"]=$conf["procs"];
1082
	
1082
 
1083
		#設置執行正常
1083
		#設置執行正常
1084
		$result["status"]="true";
1084
		$result["status"]="true";
1085
		
1085
 
1086
		#回傳結果
1086
		#回傳結果
1087
		return $result;
1087
		return $result;
1088
	
1088
 
1089
		}#function proc_update end
1089
		}#function proc_update end
1090
 
1090
 
1091
	/*
1091
	/*
1092
	#函式說明:
1092
	#函式說明:
1093
	#透過parallel展示多執行序運作.
1093
	#透過parallel展示多執行序運作.
Line 1100... Line 1100...
1100
	#可省略參數:
1100
	#可省略參數:
1101
	#無.
1101
	#無.
1102
	#參考資料:
1102
	#參考資料:
1103
	#https://www.php.net/manual/en/class.parallel-runtime.php
1103
	#https://www.php.net/manual/en/class.parallel-runtime.php
1104
	#備註:
1104
	#備註:
1105
	#需要於編譯PHP時啟用該套件	
1105
	#需要於編譯PHP時啟用該套件
1106
	*/
1106
	*/
1107
	public static function parallel_demo(){
1107
	public static function parallel_demo(){
1108
	
1108
 
1109
		#初始化要回傳的結果
1109
		#初始化要回傳的結果
1110
		$result=array();
1110
		$result=array();
1111
 
1111
 
1112
		#取得當前執行的函數名稱
1112
		#取得當前執行的函數名稱
1113
		$result["function"]=__FUNCTION__;
1113
		$result["function"]=__FUNCTION__;
1114
	
1114
 
1115
		#新增程序r1
1115
		#新增程序r1
1116
		$r1=new \parallel\Runtime();
1116
		$r1=new \parallel\Runtime();
1117
	
1117
 
1118
		#r1要執行的函式
1118
		#r1要執行的函式
1119
		$thread_r1_function=function($conf){
1119
		$thread_r1_function=function($conf){
1120
		
1120
 
1121
			#提示開始
1121
			#提示開始
1122
			echo $conf["id"]." start".PHP_EOL;
1122
			echo $conf["id"]." start".PHP_EOL;
1123
		
1123
 
1124
			#跑 $conf["i"] 次
1124
			#跑 $conf["i"] 次
1125
			for($i=0;$i<$conf["i"];$i++){
1125
			for($i=0;$i<$conf["i"];$i++){
1126
			
1126
 
1127
				#題書$i數值
1127
				#題書$i數值
1128
				echo "\$i=".$i.PHP_EOL;
1128
				echo "\$i=".$i.PHP_EOL;
1129
			
1129
 
1130
				}#for end
1130
				}#for end
1131
		
1131
 
1132
			#提示結束
1132
			#提示結束
1133
			echo $conf["id"]." end".PHP_EOL;
1133
			echo $conf["id"]." end".PHP_EOL;
1134
		
1134
 
1135
			};#function end
1135
			};#function end
1136
 
1136
 
1137
		#r1的參數
1137
		#r1的參數
1138
		$args_r1=array();
1138
		$args_r1=array();
1139
		$args_r1["id"]="r1";
1139
		$args_r1["id"]="r1";
Line 1141... Line 1141...
1141
 
1141
 
1142
		#運行程序r1
1142
		#運行程序r1
1143
		#程序函式為 $thread_r1_function
1143
		#程序函式為 $thread_r1_function
1144
		#參數陣列為 $args_r1
1144
		#參數陣列為 $args_r1
1145
		$future_r1=$r1->run($thread_r1_function, $args_r1);
1145
		$future_r1=$r1->run($thread_r1_function, $args_r1);
1146
	
1146
 
1147
		#新增程序r2
1147
		#新增程序r2
1148
		$r2=new \parallel\Runtime();
1148
		$r2=new \parallel\Runtime();
1149
	
1149
 
1150
		#r2要執行的函式
1150
		#r2要執行的函式
1151
		$thread_r2_function=function($conf){
1151
		$thread_r2_function=function($conf){
1152
		
1152
 
1153
			#提示開始
1153
			#提示開始
1154
			echo $conf["id"]." start".PHP_EOL;
1154
			echo $conf["id"]." start".PHP_EOL;
1155
		
1155
 
1156
			#跑 $conf["i"] 次
1156
			#跑 $conf["i"] 次
1157
			for($i=$conf["i"];$i>0;$i--){
1157
			for($i=$conf["i"];$i>0;$i--){
1158
			
1158
 
1159
				#題書$i數值
1159
				#題書$i數值
1160
				echo "\$i=".$i.PHP_EOL;
1160
				echo "\$i=".$i.PHP_EOL;
1161
			
1161
 
1162
				}#for end
1162
				}#for end
1163
		
1163
 
1164
			#提示結束
1164
			#提示結束
1165
			echo $conf["id"]." end".PHP_EOL;
1165
			echo $conf["id"]." end".PHP_EOL;
1166
		
1166
 
1167
			};#function end
1167
			};#function end
1168
 
1168
 
1169
		#r2的參數
1169
		#r2的參數
1170
		$args_r2=array();
1170
		$args_r2=array();
1171
		$args_r2["id"]="r2";
1171
		$args_r2["id"]="r2";
1172
		$args_r2["i"]=10;
1172
		$args_r2["i"]=10;
1173
	
1173
 
1174
		#運行程序r2
1174
		#運行程序r2
1175
		#程序函式為 $thread__r2_function
1175
		#程序函式為 $thread__r2_function
1176
		#參數陣列為 $args_r2
1176
		#參數陣列為 $args_r2
1177
		$future_r2=$r2->run($thread_r2_function, $args_r2);
1177
		$future_r2=$r2->run($thread_r2_function, $args_r2);
1178
		
1178
 
1179
		#存放有多少在執行的程序
1179
		#存放有多少在執行的程序
1180
		$futureArray=array();
1180
		$futureArray=array();
1181
		$futureArray[]=$future_r1;
1181
		$futureArray[]=$future_r1;
1182
		$futureArray[]=$future_r2;
1182
		$futureArray[]=$future_r2;
1183
		
1183
 
1184
		#當有程序尚未結束時
1184
		#當有程序尚未結束時
1185
		while(!empty($futureArray)){
1185
		while(!empty($futureArray)){
1186
		
1186
 
1187
			#針對每個 Future
1187
			#針對每個 Future
1188
			foreach($futureArray as $index => $Future){
1188
			foreach($futureArray as $index => $Future){
1189
			
1189
 
1190
				#如果執行完成了
1190
				#如果執行完成了
1191
				if($Future->done()){
1191
				if($Future->done()){
1192
				
1192
 
1193
					#移除之
1193
					#移除之
1194
					unset($futureArray[$index]);
1194
					unset($futureArray[$index]);
1195
				
1195
 
1196
					}#if end
1196
					}#if end
1197
			
1197
 
1198
				}#foreach end
1198
				}#foreach end
1199
		
1199
 
1200
			}#while end
1200
			}#while end
1201
		
1201
 
1202
		#提示所有程序都執行結束了
1202
		#提示所有程序都執行結束了
1203
		echo "all done!".PHP_EOL;
1203
		echo "all done!".PHP_EOL;
1204
		
1204
 
1205
		#設置執行正常
1205
		#設置執行正常
1206
		$result["status"]="true";
1206
		$result["status"]="true";
1207
		
1207
 
1208
		#回傳結果
1208
		#回傳結果
1209
		return $result;
1209
		return $result;
1210
		
1210
 
1211
		}#function parallel_demo end
1211
		}#function parallel_demo end
1212
		
1212
 
1213
	/*
1213
	/*
1214
	#函式說明:
1214
	#函式說明:
1215
	#透過 unix socket 來多執行序運作.
1215
	#透過 unix socket 來多執行序運作.
1216
	#回傳結果:
1216
	#回傳結果:
1217
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1217
	#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
Line 1229... Line 1229...
1229
	#無
1229
	#無
1230
	#備註:
1230
	#備註:
1231
	#需要透過sock::unixDomainSockServer來運行.
1231
	#需要透過sock::unixDomainSockServer來運行.
1232
	*/
1232
	*/
1233
	public static function socket(&$conf){
1233
	public static function socket(&$conf){
1234
	
1234
 
1235
		#初始化要回傳的變數
1235
		#初始化要回傳的變數
1236
		$result=array();
1236
		$result=array();
1237
 
1237
 
1238
		#初始化當前執行的函數名稱
1238
		#初始化當前執行的函數名稱
1239
		$result["function"]=__FUNCTION__;
1239
		$result["function"]=__FUNCTION__;
1240
 
1240
 
1241
		#如果 $conf 不為陣列
1241
		#如果 $conf 不為陣列
1242
		if(gettype($conf)!="array"){
1242
		if(gettype($conf)!="array"){
1243
			
1243
 
1244
			#設置執行失敗
1244
			#設置執行失敗
1245
			$result["status"]="false";
1245
			$result["status"]="false";
1246
			
1246
 
1247
			#設置執行錯誤訊息
1247
			#設置執行錯誤訊息
1248
			$result["error"][]="\$conf變數須為陣列形態";
1248
			$result["error"][]="\$conf變數須為陣列形態";
1249
 
1249
 
1250
			#如果傳入的參數為 null
1250
			#如果傳入的參數為 null
1251
			if($conf==null){
1251
			if($conf==null){
1252
				
1252
 
1253
				#設置執行錯誤訊息
1253
				#設置執行錯誤訊息
1254
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
1254
				$result["error"][]="\$conf變數不得為null,請檢查函數「".$result["function"]."」的參數設置有無正確!";
1255
				
1255
 
1256
				}#if end
1256
				}#if end
1257
 
1257
 
1258
			#回傳結果
1258
			#回傳結果
1259
			return $result;
1259
			return $result;
1260
			
1260
 
1261
			}#if end
1261
			}#if end
1262
	
1262
 
1263
		#取得使用的參數
1263
		#取得使用的參數
1264
		$result["argu"]=$conf;
1264
		$result["argu"]=$conf;
1265
		
1265
 
1266
		#函式說明:
1266
		#函式說明:
1267
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
1267
		#檢查必填與可省略的參數,可省略參數可指定預設要給與什麼數值內容.
1268
		#回傳結果:
1268
		#回傳結果:
1269
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1269
		#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
1270
		#$result["error"],執行不正常結束的錯訊息陣列.
1270
		#$result["error"],執行不正常結束的錯訊息陣列.
Line 1315... Line 1315...
1315
		#array_keys=>http://php.net/manual/en/function.array-keys.php
1315
		#array_keys=>http://php.net/manual/en/function.array-keys.php
1316
		#備註:
1316
		#備註:
1317
		#無.
1317
		#無.
1318
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
1318
		$checkArguments=variableCheck::checkArguments($conf["variableCheck::checkArguments"]);
1319
		unset($conf["variableCheck::checkArguments"]);
1319
		unset($conf["variableCheck::checkArguments"]);
1320
	
1320
 
1321
		#如果 $checkArguments["status"] 等於 "false"
1321
		#如果 $checkArguments["status"] 等於 "false"
1322
		if($checkArguments["status"]==="false"){
1322
		if($checkArguments["status"]==="false"){
1323
			
1323
 
1324
			#設置錯誤識別
1324
			#設置錯誤識別
1325
			$result["status"]="false";
1325
			$result["status"]="false";
1326
			
1326
 
1327
			#設置錯誤訊息
1327
			#設置錯誤訊息
1328
			$result["error"]=$checkArguments;
1328
			$result["error"]=$checkArguments;
1329
			
1329
 
1330
			#回傳結果
1330
			#回傳結果
1331
			return $result;
1331
			return $result;
1332
			
1332
 
1333
			}#if end
1333
			}#if end
1334
			
1334
 
1335
		#如果 $checkArguments["passed"] 等於 "false"
1335
		#如果 $checkArguments["passed"] 等於 "false"
1336
		if($checkArguments["passed"]==="false"){
1336
		if($checkArguments["passed"]==="false"){
1337
			
1337
 
1338
			#設置錯誤識別
1338
			#設置錯誤識別
1339
			$result["status"]="false";
1339
			$result["status"]="false";
1340
			
1340
 
1341
			#設置錯誤訊息
1341
			#設置錯誤訊息
1342
			$result["error"]=$checkArguments;
1342
			$result["error"]=$checkArguments;
1343
			
1343
 
1344
			#回傳結果
1344
			#回傳結果
1345
			return $result;
1345
			return $result;
1346
			
1346
 
1347
			}#if end
1347
			}#if end
1348
			
1348
 
1349
		#針對每個指令
1349
		#針對每個指令
1350
		foreach($conf["cmds"] as $cmd){
1350
		foreach($conf["cmds"] as $cmd){
1351
		
1351
 
1352
			#連線到 usr/bin/qbpwcf-usock.php 產生的  unix domain socket,運行指定的指令.
1352
			#連線到 usr/bin/qbpwcf-usock.php 產生的  unix domain socket,運行指定的指令.
1353
			#回傳結果:
1353
			#回傳結果:
1354
			#$result["status"],"true"代表執行正常;"false"代表執行不正常.
1354
			#$result["status"],"true"代表執行正常;"false"代表執行不正常.
1355
			#$result["error"],錯誤訊息陣列.
1355
			#$result["error"],錯誤訊息陣列.
1356
			#$result["function"],當前執行的函式名稱.
1356
			#$result["function"],當前執行的函式名稱.
Line 1360... Line 1360...
1360
			#$conf["fileArgu"],字串,變數__FILE__的內容.
1360
			#$conf["fileArgu"],字串,變數__FILE__的內容.
1361
			$conf["sock::execAnyCmdByQBPWCFunixSocket"]["fileArgu"]=__FILE__;
1361
			$conf["sock::execAnyCmdByQBPWCFunixSocket"]["fileArgu"]=__FILE__;
1362
			#$conf["command"],字串,要執行的指令名稱.
1362
			#$conf["command"],字串,要執行的指令名稱.
1363
			$conf["sock::execAnyCmdByQBPWCFunixSocket"]["command"]=$cmd;
1363
			$conf["sock::execAnyCmdByQBPWCFunixSocket"]["command"]=$cmd;
1364
			#可省略參數:
1364
			#可省略參數:
1365
			
1365
 
1366
			#如果有設置 sock 參數
1366
			#如果有設置 sock 參數
1367
			if(isset($conf["sock"])){
1367
			if(isset($conf["sock"])){
1368
			
1368
 
1369
				#$conf["sock"],字串,要連線的 usr/bin/qbpwcf-sock.php 所產生的 unix domain socket 路徑與名稱,預設為 qbpwcf_usock_path.
1369
				#$conf["sock"],字串,要連線的 usr/bin/qbpwcf-sock.php 所產生的 unix domain socket 路徑與名稱,預設為 qbpwcf_usock_path.
1370
				$conf["sock::execAnyCmdByQBPWCFunixSocket"]["sock"]=$conf["sock"];
1370
				$conf["sock::execAnyCmdByQBPWCFunixSocket"]["sock"]=$conf["sock"];
1371
			
1371
 
1372
				}#if end
1372
				}#if end
1373
			
1373
 
1374
			#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
1374
			#$conf["argu"],陣列字串,指令搭配的參數,預設為空陣列.
1375
			#$conf["sock::execAnyCmdByQBPWCFunixSocket"]["argu"]=$conf["argu"];
1375
			#$conf["sock::execAnyCmdByQBPWCFunixSocket"]["argu"]=$conf["argu"];
1376
			#$conf["commandIncludeArgu"],字串,是否command含有參數,預設為"false"代表沒有;反之為"true".
1376
			#$conf["commandIncludeArgu"],字串,是否command含有參數,預設為"false"代表沒有;反之為"true".
1377
			$conf["sock::execAnyCmdByQBPWCFunixSocket"]["commandIncludeArgu"]="true";
1377
			$conf["sock::execAnyCmdByQBPWCFunixSocket"]["commandIncludeArgu"]="true";
1378
			#$conf["commandInBg"],字串,是否要將程序放在背景執行,再取得相關資訊,預設為"false"代表不要;反之為"true".
1378
			#$conf["commandInBg"],字串,是否要將程序放在背景執行,再取得相關資訊,預設為"false"代表不要;反之為"true".
Line 1381... Line 1381...
1381
			#無.
1381
			#無.
1382
			#備註:
1382
			#備註:
1383
			#無.
1383
			#無.
1384
			$execAnyCmdByQBPWCFunixSocket=sock::execAnyCmdByQBPWCFunixSocket($conf["sock::execAnyCmdByQBPWCFunixSocket"]);
1384
			$execAnyCmdByQBPWCFunixSocket=sock::execAnyCmdByQBPWCFunixSocket($conf["sock::execAnyCmdByQBPWCFunixSocket"]);
1385
			unset($conf["sock::execAnyCmdByQBPWCFunixSocket"]);
1385
			unset($conf["sock::execAnyCmdByQBPWCFunixSocket"]);
1386
		
1386
 
1387
			#如果執行失敗
1387
			#如果執行失敗
1388
			if($execAnyCmdByQBPWCFunixSocket["status"]==="false"){
1388
			if($execAnyCmdByQBPWCFunixSocket["status"]==="false"){
1389
				
1389
 
1390
				#設置錯誤識別
1390
				#設置錯誤識別
1391
				$result["status"]="false";
1391
				$result["status"]="false";
1392
				
1392
 
1393
				#設置錯誤訊息
1393
				#設置錯誤訊息
1394
				$result["error"]=$execAnyCmdByQBPWCFunixSocket;
1394
				$result["error"]=$execAnyCmdByQBPWCFunixSocket;
1395
				
1395
 
1396
				#回傳結果
1396
				#回傳結果
1397
				return $result;
1397
				return $result;
1398
				
1398
 
1399
				}#if end
1399
				}#if end
1400
			
1400
 
1401
			#debug
1401
			#debug
1402
			#var_dump(__LINE__,$execAnyCmdByQBPWCFunixSocket);
1402
			#var_dump(__LINE__,$execAnyCmdByQBPWCFunixSocket);
1403
			
1403
 
1404
			#儲存收到的回應
1404
			#儲存收到的回應
1405
			$result["content"][]=$execAnyCmdByQBPWCFunixSocket["content"];
1405
			$result["content"][]=$execAnyCmdByQBPWCFunixSocket["content"];
1406
			
1406
 
1407
			}#foreach end
1407
			}#foreach end
1408
			
1408
 
1409
		#設置執行正常
1409
		#設置執行正常
1410
		$result["status"]="true";
1410
		$result["status"]="true";
1411
	
1411
 
1412
		#回傳結果
1412
		#回傳結果
1413
		return $result;
1413
		return $result;
1414
	
1414
 
1415
		}#function socket end
1415
		}#function socket end
1416
 
1416
 
1417
	}#class threads end
-
 
1418
1417
	}#class threads end
-
 
1418