Subversion Repositories qbpwcf-lib(archive)

Rev

Rev 925 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
706 liveuser 1
#!/bin/php
2
<?php
3
 
4
/*
5
	QBPWCF, Quick Build PHP website Component base on Fedora Linux.
6
    Copyright (C) 2015~2024 Min-Jhin,Chen
7
 
8
    This file is part of QBPWCF.
9
 
10
    QBPWCF is free software: you can redistribute it and/or modify
11
    it under the terms of the GNU General Public License as published by
12
    the Free Software Foundation, either version 3 of the License, or
13
    (at your option) any later version.
14
 
15
    QBPWCF is distributed in the hope that it will be useful,
16
    but WITHOUT ANY WARRANTY; without even the implied warranty of
17
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
    GNU General Public License for more details.
19
 
20
    You should have received a copy of the GNU General Public License
21
    along with QBPWCF.  If not, see <http://www.gnu.org/licenses/>.
22
 
23
*/
24
 
25
/*
26
說明:
27
建立暫存從config server取得的內容.
28
 
29
範例:
30
configCache.php --sock /tmp/configCache.php.sock --cacheFilePath /tmp/ --checkVersionInterval 60
31
*/
32
 
33
#使用命名空間qbpwcf
34
namespace qbpwcf;
35
 
955 liveuser 36
#取得 lib path
37
exec("php -f ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/libexec/folderOfUsrLib.php"),$output,$status);
38
 
39
#如果執行失敗
40
if($status!==0){
41
 
42
	#debug
43
	var_dump(__LINE__,$output);
44
 
45
	#結束執行,回傳shell 1.
46
	exit(1);
47
 
48
	}#if end
49
 
50
#儲存lib path
51
$folderOfUsrLib=$output[0];
52
 
706 liveuser 53
#以該檔案的實際位置的 lib path 為 include path 首位
955 liveuser 54
$output=array();
55
exec("cd ".escapeshellarg(pathinfo(__FILE__)["dirname"]."/../".$folderOfUsrLib."/qbpwcf").";pwd;",$output,$status);
56
 
57
#如果執行失敗
58
if($status!==0){
59
 
60
	#debug
61
	var_dump(__LINE__,$output);
62
 
63
	#結束執行,回傳shell 1.
64
	exit(1);
65
 
66
	}#if end
67
 
68
#設置 include path 
706 liveuser 69
set_include_path($output[0].PATH_SEPARATOR.get_include_path());
70
 
71
#匯入外部套件
72
include("allInOne.php");
73
 
74
#說明函式
75
function help(){
76
 
77
	#印出指令說明
78
	echo "Usage of ".basename(__FILE__).":".PHP_EOL; 
79
	echo "--sock socket unix doamin socket 的位置,預設為套件根目錄底下的'var/config::client/clientCacheDaemon.sock'".PHP_EOL;
80
	echo "--cacheFilePath path cache的檔案位置,預設為套件根目錄底下的'var/config::client/'".PHP_EOL;
81
	echo "--checkVersionInterval second(s) 距離最後一次取得資料的時間多久就要檢查是否有新資料,預設為60秒.".PHP_EOL;
82
 
83
	#結束執行
84
	exit;
85
 
86
	}#function help end
87
 
88
#函式說明:
89
#解析參數.
90
#回傳結果:
91
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
92
#$reuslt["error"],執行不正常結束的錯訊息陣列.
93
#$result["function"],當前執行的函式名稱.
94
#$result["content"],解析好的參數陣列.
95
#$result["content"][$key][$i],參數 $key 的 $i+1 個參數數值內容.
96
#$result["program"],字串,執行的程式名稱.
97
#必填參數:
98
#無
99
#可省略參數:
100
#$conf["helpFunc"],如果解析的參數不成對,則要執行的函式名稱.
101
$conf["helpFunc"]="help";
102
#備註:
103
#僅能在命令列底下執行.
104
#建議:
105
#以後可將參數 --a--b 的名稱與後面的數值 $value 存成 $result["a"]["b"][$i]=$value .
106
$parseArgu=cmd::parseArgu($conf);
107
unset($conf);
108
 
109
#如果解析參數失敗
110
if($parseArgu["status"]==="false"){
111
 
112
	#印出結果
113
	var_dump($parseArgu);
114
 
115
	#結束執行
116
	exit;
117
 
118
	}#if end
119
 
120
#檢查參數
121
#函式說明:
122
#檢查必填與可省略參數,可省略參數可指定預設要給與什麼數值內容。
123
#$result["status"],執行是否正常,"true"代表正常,"false"代表不正常.
124
#$reuslt["error"],執行不正常結束的錯訊息陣列.
125
#$result["function"],當前執行的函式名稱.
126
#$result["passed"],識別要檢查的全體變數是否存在以及型態是否正確的變數,"true"代表檢查全部通過;"false"代表檢查不通過
127
#$result[$shouldBeCheckedVarName]["varExist"],所檢查的變數是否存在,"false"代表不存在;"true"代表存在
128
#$result[$shouldBeCheckedVarName]["varType"],所檢查的變數型態是否正確,"false"代表錯誤;"true"代表正確
129
#$result[$shouldBeCheckedVarName]["error"],每個參數設定的錯誤訊息
130
#$result["shouldNotBeEmpty"],不應該為空字串或控陣列的變數.
131
#$result["argu"],字串陣列,目前輸入的參數名稱陣列.
132
#$result["legalVarName"],字串陣列,合法可用的參數名稱陣列.
133
#$result["notNeedVar"],字串陣列,多餘的參數名稱.
134
#必填寫的參數:
135
#$conf["varInput"],陣列變數,要檢查的陣列變數,請在要檢查的參數前面加上&,這樣變動的結果才能被套用。
136
$conf["varInput"]=&$parseArgu["content"];
137
#$conf["referenceVarKey"],字串,$conf參數後面的key值,用於移除不要的參考陣列.
138
$conf["referenceVarKey"]="variableCheck::checkArguments";
139
#可以省略的參數:
140
#$conf["mustBeFilledVariableName"],爲必填參數的變數名稱陣列,形態爲陣列變數,元素數量需要跟"mustBeFilledVariableType"參數的元素數量一致,例如: $conf["mustBeFilledVariableName"] = array("id","account","password");
141
#$conf["mustBeFilledVariableName"]=array("sock","cacheFilePath","checkVersionInterval","dbVarName","dbAddrVarName","dbPortVarName","backupAddr");
142
#$conf["mustBeFilledVariableType"],爲必填參數的變數陣列應該爲何種變數形態,形態爲陣列,元素數量需要跟"mustBeFilledVariableName"參數的元素數量一致,例如: $conf["mustBeFilledVariableType"] = array("string",integer,"double","resource","object"); , null代表不指定變數形態.
143
#$conf["mustBeFilledVariableType"]=array("array","array","array","array","array","array","array");
144
#$conf["canBeEmptyString"],字串,必填變數內容如果是空字串就不能算是有設置的話,請設為"false",預設爲"true",可以為空字串.
145
#$conf["canBeEmptyString"]="false";
146
#$conf["canNotBeEmpty"],字串陣列,哪些必填參數的內容不得為空字串或空陣列,僅當$conf["canBeEmptyString"]為"true"時會生效.
147
#$conf["canNotBeEmpty"]=array();
148
#$conf["canBeEmpty"],字串陣列,哪些必填參數的內容可為空字串或空陣列,僅當$conf["canBeEmptyString"]為"false"時會生效.
149
#$conf["canBeEmpty"]=array();
150
#$conf["skipableVariableCanNotBeEmpty"],字串陣列,哪些可省略參數不可以為空字串或空陣列.
151
#$conf["skipableVariableCanNotBeEmpty"]=array("backTime");
152
#$conf["skipableVariableName"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableName"] = array("id","account","password");
153
$conf["skipableVariableName"]=array("sock","cacheFilePath","checkVersionInterval");
154
#$conf["skipableVariableType"],陣列字串,爲可省略參數的變數名稱陣列,形態爲陣列變數,例如: $conf["skipableVariableType"] = array("string",integer,"double");
155
$conf["skipableVariableType"]=array("array","array","array");
156
#$conf["skipableVarDefaultValue"],字串陣列,每個不存在的可省略變數要初始化為什麼,null與代表不指定,若預設值是參數之一,請將$conf["mustBeFilledVar"]改成"\$conf["\mustBeFilledVar\"]".
157
$conf["skipableVarDefaultValue"]=array(null,null,null);
158
#$conf["disallowAllSkipableVarIsEmpty"],字串,是否允許每個可省略參數都為空字串,預設為"true"允許,反之為"false".
159
#$conf["variableCheck::checkArguments"]["disallowAllSkipableVarIsEmpty"]="false";
160
#$conf["disallowAllSkipableVarIsEmptyArray"],字串,是否允許每個可省略參數都為空陣列,預設為"true"允許,反之為"false".
161
#$conf["disallowAllSkipableVarIsEmptyArray"]="";
162
#$conf["arrayCountEqualCheck"],字串陣列,為檢查哪些陣列參數的元素數量要一樣,$conf["arrayCountEqualCheck"][$i]=array()為第$i組key為哪些的變數其元素數量要相等.
163
#$conf["arrayCountEqualCheck"][]=array();
164
#參考資料來源:
165
#array_keys=>http://php.net/manual/en/function.array-keys.php
166
$checkArguments=variableCheck::checkArguments($conf);
167
unset($conf);
168
 
169
#若執行失敗
170
if($checkArguments["status"]==="false"){
171
 
172
	#印出結果
173
	var_dump($checkArguments);
174
 
175
	#結束執行
176
	exit;
177
 
178
	}#if end
179
 
180
#若檢查不通過
181
if($checkArguments["passed"]==="false"){
182
 
183
	#印出結果
184
	var_dump($checkArguments);
185
 
186
	#結束執行
187
	exit;
188
 
189
	}#if end
190
 
191
#函式說明:
192
#處理來自config::client有使用cache的要求,其為常駐的unix domain socket
193
#回傳結果:
194
#$result["status"],執行是否正常,"true"為正常,"false"為不正常.
195
#$result["error"],錯誤訊息陣列.
196
#$result["function"],當前執行的函數名稱.
197
#必填參數:
198
#無.
199
#可省略參數:
200
 
201
#初始化參數
202
$conf=array();
203
 
204
#如果有設置 sock
205
if(isset($parseArgu["content"]["sock"][0])){
206
 
207
	#$conf["sock"],字串,unix doamin socket的位置,預設為套件根目錄底下的"var/config::client/clientCacheDaemon.sock".
208
	$conf["sock"]=$parseArgu["content"]["sock"][0];
209
 
210
	}#if end
211
 
212
#如果有設置 cacheFilePath
213
if(isset($parseArgu["content"]["cacheFilePath"][0])){
214
 
215
	#$conf["cacheFilePath"],字串,cache的檔案位置,預設為套件根目錄底下的"var/config::client/".
216
	$conf["cacheFilePath"]=$parseArgu["content"]["cacheFilePath"][0];
217
 
218
	}#if end
219
 
220
#如果有設置 checkVersionInterval
221
if(isset($parseArgu["content"]["checkVersionInterval"][0])){
222
 
223
	#$conf["checkVersionInterval"],字串,距離最後一次取得資料的時間多久就要檢查是否有新資料,預設為60秒.
224
	$conf["checkVersionInterval"]=$parseArgu["content"]["checkVersionInterval"][0];
225
 
226
	}#if end
227
 
228
#參考資料:
229
#無.
230
#備註:
231
#無,
232
$clientCacheDaemon=config::clientCacheDaemon($conf);
233
unset($conf);
234
 
235
#debug
236
var_dump($clientCacheDaemon);