1 | <?php |
---|
2 | /* |
---|
3 | * This file is part of EC-CUBE |
---|
4 | * |
---|
5 | * Copyright(c) 2000-2013 LOCKON CO.,LTD. All Rights Reserved. |
---|
6 | * |
---|
7 | * http://www.lockon.co.jp/ |
---|
8 | * |
---|
9 | * This program is free software; you can redistribute it and/or |
---|
10 | * modify it under the terms of the GNU General Public License |
---|
11 | * as published by the Free Software Foundation; either version 2 |
---|
12 | * of the License, or (at your option) any later version. |
---|
13 | * |
---|
14 | * This program is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this program; if not, write to the Free Software |
---|
21 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
22 | */ |
---|
23 | |
---|
24 | /** |
---|
25 | * プラグインのヘルパークラス. |
---|
26 | * |
---|
27 | * @package Helper |
---|
28 | * @version $Id$ |
---|
29 | */ |
---|
30 | class SC_Helper_Plugin |
---|
31 | { |
---|
32 | // プラグインのインスタンスの配列. |
---|
33 | public $arrPluginInstances = array(); |
---|
34 | // プラグインのアクションの配列. |
---|
35 | public $arrRegistedPluginActions = array(); |
---|
36 | // プラグインのIDの配列. |
---|
37 | public $arrPluginIds = array(); |
---|
38 | // HeadNaviブロックの配列 |
---|
39 | public $arrHeadNaviBlocsByPlugin = array(); |
---|
40 | |
---|
41 | /** |
---|
42 | * 有効なプラグインのロード. プラグインエンジンが有効になっていない場合は |
---|
43 | * プラグインエンジン自身のインストール処理を起動する |
---|
44 | * |
---|
45 | * @return void |
---|
46 | */ |
---|
47 | public function load($plugin_activate_flg = true) |
---|
48 | { |
---|
49 | if (!defined('CONFIG_REALFILE') || !file_exists(CONFIG_REALFILE)) return; // インストール前 |
---|
50 | if (GC_Utils_Ex::isInstallFunction()) return; // インストール中 |
---|
51 | if ($plugin_activate_flg === false) return; |
---|
52 | // 有効なプラグインを取得 |
---|
53 | $arrPluginDataList = SC_Plugin_Util_Ex::getEnablePlugin(); |
---|
54 | // pluginディレクトリを取得 |
---|
55 | $arrPluginDirectory = SC_Plugin_Util_Ex::getPluginDirectory(); |
---|
56 | foreach ($arrPluginDataList as $arrPluginData) { |
---|
57 | // プラグイン本体ファイル名が取得したプラグインディレクトリ一覧にある事を確認 |
---|
58 | if (array_search($arrPluginData['plugin_code'], $arrPluginDirectory) !== false) { |
---|
59 | $plugin_file_path = PLUGIN_UPLOAD_REALDIR . $arrPluginData['plugin_code'] . '/' . $arrPluginData['class_name'] . '.php'; |
---|
60 | // プラグイン本体ファイルが存在しない場合 |
---|
61 | if (!file_exists($plugin_file_path)) { |
---|
62 | // エラー出力 |
---|
63 | $msg = 'プラグイン本体ファイルが存在しない。当該プラグインを無視して続行する。'; |
---|
64 | $msg .= 'ファイル=' . var_export($plugin_file_path, true) . '; '; |
---|
65 | trigger_error($msg, E_USER_WARNING); |
---|
66 | // 次のプラグインへ続行 |
---|
67 | continue 1; |
---|
68 | } |
---|
69 | // プラグイン本体ファイルをrequire. |
---|
70 | require_once $plugin_file_path; |
---|
71 | |
---|
72 | // プラグインのインスタンス生成. |
---|
73 | $objPlugin = new $arrPluginData['class_name']($arrPluginData); |
---|
74 | // メンバ変数にプラグインのインスタンスを登録. |
---|
75 | $this->arrPluginInstances[$arrPluginData['plugin_id']] = $objPlugin; |
---|
76 | $this->arrPluginIds[] = $arrPluginData['plugin_id']; |
---|
77 | // ローカルフックポイントの登録. |
---|
78 | $this->registerLocalHookPoint($objPlugin, $arrPluginData['priority']); |
---|
79 | // スーパーフックポイントの登録. |
---|
80 | $this->registerSuperHookPoint($objPlugin, HOOK_POINT_PREPROCESS, 'preProcess', $arrPluginData['priority']); |
---|
81 | $this->registerSuperHookPoint($objPlugin, HOOK_POINT_PROCESS, 'process', $arrPluginData['priority']); |
---|
82 | } |
---|
83 | } |
---|
84 | } |
---|
85 | |
---|
86 | /** |
---|
87 | * SC_Helper_Plugin オブジェクトを返す(Singletonパターン) |
---|
88 | * |
---|
89 | * @return object SC_Helper_Pluginオブジェクト |
---|
90 | */ |
---|
91 | public static function getSingletonInstance($plugin_activate_flg = true) |
---|
92 | { |
---|
93 | if (!isset($GLOBALS['_SC_Helper_Plugin_instance'])) { |
---|
94 | // プラグインのローダーがDB接続を必要とするため、 |
---|
95 | // SC_Queryインスタンス生成後のみオブジェクトを生成する。 |
---|
96 | require_once CLASS_EX_REALDIR . 'SC_Query_Ex.php'; |
---|
97 | if (is_null(SC_Query_Ex::getPoolInstance())) { |
---|
98 | return false; |
---|
99 | } |
---|
100 | |
---|
101 | $GLOBALS['_SC_Helper_Plugin_instance'] = new SC_Helper_Plugin_Ex(); |
---|
102 | $GLOBALS['_SC_Helper_Plugin_instance']->load($plugin_activate_flg); |
---|
103 | } |
---|
104 | |
---|
105 | return $GLOBALS['_SC_Helper_Plugin_instance']; |
---|
106 | } |
---|
107 | |
---|
108 | /** |
---|
109 | * プラグイン実行 |
---|
110 | * |
---|
111 | * @param string $hook_point フックポイント |
---|
112 | * @param array $arrArgs コールバック関数へ渡す引数 |
---|
113 | * @return void |
---|
114 | */ |
---|
115 | public function doAction($hook_point, $arrArgs = array()) |
---|
116 | { |
---|
117 | if (is_array($arrArgs) === false) { |
---|
118 | array(&$arrArgs); |
---|
119 | } |
---|
120 | |
---|
121 | if ($hook_point == 'loadClassFileChange') { |
---|
122 | $arrSaveArgs = $arrArgs; |
---|
123 | $arrClassName = array(); |
---|
124 | $arrClassPath = array(); |
---|
125 | } |
---|
126 | |
---|
127 | if (array_key_exists($hook_point, $this->arrRegistedPluginActions) |
---|
128 | && is_array($this->arrRegistedPluginActions[$hook_point])) { |
---|
129 | krsort($this->arrRegistedPluginActions[$hook_point]); |
---|
130 | foreach ($this->arrRegistedPluginActions[$hook_point] as $arrFuncs) { |
---|
131 | foreach ($arrFuncs as $func) { |
---|
132 | if (!is_null($func['function'])) { |
---|
133 | if ($hook_point == 'loadClassFileChange') { |
---|
134 | $classname = $arrSaveArgs[0]; |
---|
135 | $classpath = $arrSaveArgs[1]; |
---|
136 | $arrTempArgs = array(&$classname, &$classpath); |
---|
137 | |
---|
138 | call_user_func_array($func['function'], $arrTempArgs); |
---|
139 | |
---|
140 | if ($classname !== $arrSaveArgs[0]) { |
---|
141 | $arrClassName[] = $classname; |
---|
142 | $arrClassPath[] = $classpath; |
---|
143 | } |
---|
144 | } else { |
---|
145 | call_user_func_array($func['function'], $arrArgs); |
---|
146 | } |
---|
147 | } |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | if ($hook_point == 'loadClassFileChange') { |
---|
152 | if (count($arrClassName) > 0) { |
---|
153 | $arrArgs[0] = $arrClassName; |
---|
154 | $arrArgs[1] = $arrClassPath; |
---|
155 | } |
---|
156 | } |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | /** |
---|
161 | * スーパーフックポイントを登録します. |
---|
162 | * |
---|
163 | * @param Object $objPlugin プラグインのインスタンス |
---|
164 | * @param string $hook_point スーパーフックポイント |
---|
165 | * @param string $function_name 実行する関数名 |
---|
166 | * @param string $priority 実行順 |
---|
167 | */ |
---|
168 | public function registerSuperHookPoint($objPlugin, $hook_point, $function_name, $priority) |
---|
169 | { |
---|
170 | // スーパープラグイン関数を定義しているかを検証. |
---|
171 | if (method_exists($objPlugin, $function_name) === true) { |
---|
172 | // アクションの登録 |
---|
173 | $this->addAction($hook_point, array($objPlugin, $function_name), $priority); |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | /** |
---|
178 | * ローカルフックポイントを登録します. |
---|
179 | * |
---|
180 | * @param Object $objPlugin プラグインのインスタンス |
---|
181 | * @param string $priority 実行順 |
---|
182 | */ |
---|
183 | public function registerLocalHookPoint($objPlugin, $priority) |
---|
184 | { |
---|
185 | // ローカルプラグイン関数を定義しているかを検証. |
---|
186 | if (method_exists($objPlugin, 'register') === true) { |
---|
187 | // アクションの登録(プラグイン側に記述) |
---|
188 | $objPluginHelper =& SC_Helper_Plugin::getSingletonInstance(); |
---|
189 | $objPlugin->register($objPluginHelper, $priority); |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | /** |
---|
194 | * プラグイン コールバック関数を追加する |
---|
195 | * |
---|
196 | * @param string $hook_point フックポイント名 |
---|
197 | * @param callback $function コールバック関数名 |
---|
198 | * @param string $priority 同一フックポイント内での実行優先度 |
---|
199 | * @return boolean 成功すればtrue |
---|
200 | */ |
---|
201 | public function addAction($hook_point, $function, $priority = 0) |
---|
202 | { |
---|
203 | if (!is_callable($function)) { |
---|
204 | // TODO エラー処理; コール可能な形式ではありません |
---|
205 | } |
---|
206 | $idx = $this->makeActionUniqueId($hook_point, $function, $priority); |
---|
207 | $this->arrRegistedPluginActions[$hook_point][$priority][$idx] = array('function' => $function); |
---|
208 | |
---|
209 | return true; |
---|
210 | } |
---|
211 | |
---|
212 | /** |
---|
213 | * コールバック関数を一意に識別するIDの生成 |
---|
214 | * |
---|
215 | * @param string $hook_point フックポイント名 |
---|
216 | * @param callback $function コールバック関数名 |
---|
217 | * @param integer $priority 同一フックポイント内での実行優先度 |
---|
218 | * @return string コールバック関数を一意に識別するID |
---|
219 | */ |
---|
220 | public function makeActionUniqueId($hook_point, $function, $priority) |
---|
221 | { |
---|
222 | static $filter_id_count = 0; |
---|
223 | |
---|
224 | if (is_string($function)) { |
---|
225 | return $function; |
---|
226 | } |
---|
227 | |
---|
228 | if (is_object($function)) { |
---|
229 | $function = array($function, ''); |
---|
230 | } else { |
---|
231 | $function = (array) $function; |
---|
232 | } |
---|
233 | |
---|
234 | if (is_object($function[0])) { |
---|
235 | if (function_exists('spl_object_hash')) { |
---|
236 | return spl_object_hash($function[0]) . $function[1]; |
---|
237 | } else { |
---|
238 | $obj_idx = get_class($function[0]).$function[1]; |
---|
239 | if ( false === $priority) |
---|
240 | return false; |
---|
241 | $obj_idx .= isset($this->arrRegistedPluginActions[$hook_point][$priority]) |
---|
242 | ? count((array) $this->arrRegistedPluginActions[$hook_point][$priority]) |
---|
243 | : $filter_id_count; |
---|
244 | $function[0]->wp_filter_id = $filter_id_count; |
---|
245 | ++$filter_id_count; |
---|
246 | |
---|
247 | return $obj_idx; |
---|
248 | } |
---|
249 | } elseif (is_string($function[0])) { |
---|
250 | return $function[0].$function[1]; |
---|
251 | } |
---|
252 | } |
---|
253 | |
---|
254 | /** |
---|
255 | * ブロックの配列から有効でないpluginのブロックを除外して返します. |
---|
256 | * |
---|
257 | * @param array $arrBlocs プラグインのインストールディレクトリ |
---|
258 | * @return array $arrBlocsサイトルートからメディアディレクトリへの相対パス |
---|
259 | */ |
---|
260 | public function getEnableBlocs($arrBlocs) |
---|
261 | { |
---|
262 | foreach ($arrBlocs as $key => $value) { |
---|
263 | // 有効なpluginのブロック以外. |
---|
264 | if (!in_array($value['plugin_id'] , $this->arrPluginIds)) { |
---|
265 | // 通常ブロック以外. |
---|
266 | if ($value['plugin_id'] != '') { |
---|
267 | // ブロック配列から削除する |
---|
268 | unset ($arrBlocs[$key]); |
---|
269 | } |
---|
270 | } |
---|
271 | } |
---|
272 | |
---|
273 | return $arrBlocs; |
---|
274 | } |
---|
275 | |
---|
276 | /** |
---|
277 | * テンプレートのヘッダに追加するPHPのURLをセットする |
---|
278 | * |
---|
279 | * @param string $url PHPファイルのURL |
---|
280 | * @return void |
---|
281 | */ |
---|
282 | public function setHeadNavi($url) |
---|
283 | { |
---|
284 | $this->arrHeadNaviBlocsByPlugin[$url] = TARGET_ID_HEAD; |
---|
285 | } |
---|
286 | |
---|
287 | /** |
---|
288 | * PHPのURLをテンプレートのヘッダに追加する |
---|
289 | * |
---|
290 | * @param array|null $arrBlocs 配置情報を含めたブロックの配列 |
---|
291 | * @return void |
---|
292 | */ |
---|
293 | public function setHeadNaviBlocs(&$arrBlocs) |
---|
294 | { |
---|
295 | foreach ($this->arrHeadNaviBlocsByPlugin as $key => $value) { |
---|
296 | $arrBlocs[] = array( |
---|
297 | 'target_id' =>$value, |
---|
298 | 'php_path' => $key |
---|
299 | ); |
---|
300 | } |
---|
301 | } |
---|
302 | |
---|
303 | /** |
---|
304 | * Utility function to set a hook point. |
---|
305 | * |
---|
306 | * @param string $hook_point hook point |
---|
307 | * @param array $arrArgs argument passing to callback function |
---|
308 | * @param boolean $plugin_activate_flg |
---|
309 | * @return void |
---|
310 | */ |
---|
311 | public static function hook($hook_point, $arrArgs = array(), $plugin_activate_flg = PLUGIN_ACTIVATE_FLAG) |
---|
312 | { |
---|
313 | $objPlugin = SC_Helper_Plugin::getSingletonInstance($plugin_activate_flg); |
---|
314 | $objPlugin->doAction($hook_point, $arrArgs); |
---|
315 | } |
---|
316 | } |
---|