| 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 | var $arrPluginInstances = array(); |
|---|
| 34 | // プラグインのアクションの配列. |
|---|
| 35 | var $arrRegistedPluginActions = array(); |
|---|
| 36 | // プラグインのIDの配列. |
|---|
| 37 | var $arrPluginIds = array(); |
|---|
| 38 | // HeadNaviブロックの配列 |
|---|
| 39 | var $arrHeadNaviBlocsByPlugin = array(); |
|---|
| 40 | |
|---|
| 41 | /** |
|---|
| 42 | * 有効なプラグインのロード. プラグインエンジンが有効になっていない場合は |
|---|
| 43 | * プラグインエンジン自身のインストール処理を起動する |
|---|
| 44 | * |
|---|
| 45 | * @return void |
|---|
| 46 | */ |
|---|
| 47 | function load($plugin_activate_flg = true) |
|---|
| 48 | { |
|---|
| 49 | |
|---|
| 50 | if (!defined('CONFIG_REALFILE') || !file_exists(CONFIG_REALFILE)) return; // インストール前 |
|---|
| 51 | if (GC_Utils_Ex::isInstallFunction()) return; // インストール中 |
|---|
| 52 | if ($plugin_activate_flg === false) return; |
|---|
| 53 | // 有効なプラグインを取得 |
|---|
| 54 | $arrPluginDataList = SC_Plugin_Util_Ex::getEnablePlugin(); |
|---|
| 55 | // pluginディレクトリを取得 |
|---|
| 56 | $arrPluginDirectory = SC_Plugin_Util_Ex::getPluginDirectory(); |
|---|
| 57 | foreach ($arrPluginDataList as $arrPluginData) { |
|---|
| 58 | // プラグイン本体ファイル名が取得したプラグインディレクトリ一覧にある事を確認 |
|---|
| 59 | if (array_search($arrPluginData['plugin_code'], $arrPluginDirectory) !== false) { |
|---|
| 60 | // プラグイン本体ファイルをrequire. |
|---|
| 61 | require_once PLUGIN_UPLOAD_REALDIR . $arrPluginData['plugin_code'] . '/' . $arrPluginData['class_name'] . '.php'; |
|---|
| 62 | |
|---|
| 63 | // プラグインのインスタンス生成. |
|---|
| 64 | $objPlugin = new $arrPluginData['class_name']($arrPluginData); |
|---|
| 65 | // メンバ変数にプラグインのインスタンスを登録. |
|---|
| 66 | $this->arrPluginInstances[$arrPluginData['plugin_id']] = $objPlugin; |
|---|
| 67 | $this->arrPluginIds[] = $arrPluginData['plugin_id']; |
|---|
| 68 | // ローカルフックポイントの登録. |
|---|
| 69 | $this->registerLocalHookPoint($objPlugin, $arrPluginData['priority']); |
|---|
| 70 | // スーパーフックポイントの登録. |
|---|
| 71 | $this->registerSuperHookPoint($objPlugin, HOOK_POINT_PREPROCESS, 'preProcess', $arrPluginData['priority']); |
|---|
| 72 | $this->registerSuperHookPoint($objPlugin, HOOK_POINT_PROCESS, 'process', $arrPluginData['priority']); |
|---|
| 73 | } |
|---|
| 74 | } |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | /** |
|---|
| 78 | * SC_Helper_Plugin オブジェクトを返す(Singletonパターン) |
|---|
| 79 | * |
|---|
| 80 | * @return object SC_Helper_Pluginオブジェクト |
|---|
| 81 | */ |
|---|
| 82 | static function getSingletonInstance($plugin_activate_flg = true) |
|---|
| 83 | { |
|---|
| 84 | if (!isset($GLOBALS['_SC_Helper_Plugin_instance'])) { |
|---|
| 85 | // プラグインのローダーがDB接続を必要とするため、 |
|---|
| 86 | // SC_Queryインスタンス生成後のみオブジェクトを生成する。 |
|---|
| 87 | require_once CLASS_EX_REALDIR . 'SC_Query_Ex.php'; |
|---|
| 88 | if (is_null(SC_Query_Ex::getPoolInstance())) { |
|---|
| 89 | return false; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | $GLOBALS['_SC_Helper_Plugin_instance'] = new SC_Helper_Plugin_Ex(); |
|---|
| 93 | $GLOBALS['_SC_Helper_Plugin_instance']->load($plugin_activate_flg); |
|---|
| 94 | } |
|---|
| 95 | return $GLOBALS['_SC_Helper_Plugin_instance']; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | /** |
|---|
| 99 | * プラグイン実行 |
|---|
| 100 | * |
|---|
| 101 | * @param string $hook_point フックポイント |
|---|
| 102 | * @param array $arrArgs コールバック関数へ渡す引数 |
|---|
| 103 | * @return void |
|---|
| 104 | */ |
|---|
| 105 | function doAction($hook_point, $arrArgs = array()) |
|---|
| 106 | { |
|---|
| 107 | if (is_array($arrArgs) === false) { |
|---|
| 108 | array(&$arrArgs); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | if ($hook_point == 'loadClassFileChange') { |
|---|
| 112 | $arrSaveArgs = $arrArgs; |
|---|
| 113 | $arrClassName = array(); |
|---|
| 114 | $arrClassPath = array(); |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | if (array_key_exists($hook_point, $this->arrRegistedPluginActions) |
|---|
| 118 | && is_array($this->arrRegistedPluginActions[$hook_point])) { |
|---|
| 119 | |
|---|
| 120 | krsort($this->arrRegistedPluginActions[$hook_point]); |
|---|
| 121 | foreach ($this->arrRegistedPluginActions[$hook_point] as $arrFuncs) { |
|---|
| 122 | |
|---|
| 123 | foreach ($arrFuncs as $func) { |
|---|
| 124 | if (!is_null($func['function'])) { |
|---|
| 125 | if ($hook_point == 'loadClassFileChange') { |
|---|
| 126 | $classname = $arrSaveArgs[0]; |
|---|
| 127 | $classpath = $arrSaveArgs[1]; |
|---|
| 128 | $arrTempArgs = array(&$classname, &$classpath); |
|---|
| 129 | |
|---|
| 130 | call_user_func_array($func['function'], $arrTempArgs); |
|---|
| 131 | |
|---|
| 132 | if ($classname !== $arrSaveArgs[0]) { |
|---|
| 133 | $arrClassName[] = $classname; |
|---|
| 134 | $arrClassPath[] = $classpath; |
|---|
| 135 | } |
|---|
| 136 | } else { |
|---|
| 137 | call_user_func_array($func['function'], $arrArgs); |
|---|
| 138 | } |
|---|
| 139 | } |
|---|
| 140 | } |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | if ($hook_point == 'loadClassFileChange') { |
|---|
| 144 | if (count($arrClassName) > 0) { |
|---|
| 145 | $arrArgs[0] = $arrClassName; |
|---|
| 146 | $arrArgs[1] = $arrClassPath; |
|---|
| 147 | } |
|---|
| 148 | } |
|---|
| 149 | } |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | /** |
|---|
| 153 | * スーパーフックポイントを登録します. |
|---|
| 154 | * |
|---|
| 155 | * @param Object $objPlugin プラグインのインスタンス |
|---|
| 156 | * @param string $hook_point スーパーフックポイント |
|---|
| 157 | * @param string $function_name 実行する関数名 |
|---|
| 158 | * @param string $priority 実行順 |
|---|
| 159 | */ |
|---|
| 160 | function registerSuperHookPoint($objPlugin, $hook_point, $function_name, $priority) |
|---|
| 161 | { |
|---|
| 162 | // スーパープラグイン関数を定義しているかを検証. |
|---|
| 163 | if (method_exists($objPlugin, $function_name) === true) { |
|---|
| 164 | // アクションの登録 |
|---|
| 165 | $this->addAction($hook_point, array($objPlugin, $function_name), $priority); |
|---|
| 166 | } |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | /** |
|---|
| 170 | * ローカルフックポイントを登録します. |
|---|
| 171 | * |
|---|
| 172 | * @param Object $objPlugin プラグインのインスタンス |
|---|
| 173 | * @param string $priority 実行順 |
|---|
| 174 | */ |
|---|
| 175 | function registerLocalHookPoint($objPlugin, $priority) |
|---|
| 176 | { |
|---|
| 177 | // ローカルプラグイン関数を定義しているかを検証. |
|---|
| 178 | if (method_exists($objPlugin, 'register') === true) { |
|---|
| 179 | // アクションの登録(プラグイン側に記述) |
|---|
| 180 | $objPluginHelper =& SC_Helper_Plugin::getSingletonInstance(); |
|---|
| 181 | $objPlugin->register($objPluginHelper, $priority); |
|---|
| 182 | } |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | /** |
|---|
| 186 | * プラグイン コールバック関数を追加する |
|---|
| 187 | * |
|---|
| 188 | * @param string $hook_point フックポイント名 |
|---|
| 189 | * @param callback $function コールバック関数名 |
|---|
| 190 | * @param string $priority 同一フックポイント内での実行優先度 |
|---|
| 191 | * @return boolean 成功すればtrue |
|---|
| 192 | */ |
|---|
| 193 | function addAction($hook_point, $function, $priority = 0) |
|---|
| 194 | { |
|---|
| 195 | if (!is_callable($function)) { |
|---|
| 196 | // TODO エラー処理; コール可能な形式ではありません |
|---|
| 197 | } |
|---|
| 198 | $idx = $this->makeActionUniqueId($hook_point, $function, $priority); |
|---|
| 199 | $this->arrRegistedPluginActions[$hook_point][$priority][$idx] = array('function' => $function); |
|---|
| 200 | return true; |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | /** |
|---|
| 204 | * コールバック関数を一意に識別するIDの生成 |
|---|
| 205 | * |
|---|
| 206 | * @param string $hook_point フックポイント名 |
|---|
| 207 | * @param callback $function コールバック関数名 |
|---|
| 208 | * @param integer $priority 同一フックポイント内での実行優先度 |
|---|
| 209 | * @return string コールバック関数を一意に識別するID |
|---|
| 210 | */ |
|---|
| 211 | function makeActionUniqueId($hook_point, $function, $priority) |
|---|
| 212 | { |
|---|
| 213 | static $filter_id_count = 0; |
|---|
| 214 | |
|---|
| 215 | if (is_string($function)) { |
|---|
| 216 | return $function; |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | if (is_object($function)) { |
|---|
| 220 | $function = array($function, ''); |
|---|
| 221 | } else { |
|---|
| 222 | $function = (array) $function; |
|---|
| 223 | } |
|---|
| 224 | |
|---|
| 225 | if (is_object($function[0])) { |
|---|
| 226 | if (function_exists('spl_object_hash')) { |
|---|
| 227 | return spl_object_hash($function[0]) . $function[1]; |
|---|
| 228 | } else { |
|---|
| 229 | $obj_idx = get_class($function[0]).$function[1]; |
|---|
| 230 | if ( false === $priority) |
|---|
| 231 | return false; |
|---|
| 232 | $obj_idx .= isset($this->arrRegistedPluginActions[$hook_point][$priority]) |
|---|
| 233 | ? count((array)$this->arrRegistedPluginActions[$hook_point][$priority]) |
|---|
| 234 | : $filter_id_count; |
|---|
| 235 | $function[0]->wp_filter_id = $filter_id_count; |
|---|
| 236 | ++$filter_id_count; |
|---|
| 237 | |
|---|
| 238 | return $obj_idx; |
|---|
| 239 | } |
|---|
| 240 | } else if (is_string($function[0])) { |
|---|
| 241 | return $function[0].$function[1]; |
|---|
| 242 | } |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | /** |
|---|
| 246 | * ブロックの配列から有効でないpluginのブロックを除外して返します. |
|---|
| 247 | * |
|---|
| 248 | * @param array $arrBlocs プラグインのインストールディレクトリ |
|---|
| 249 | * @return array $arrBlocsサイトルートからメディアディレクトリへの相対パス |
|---|
| 250 | */ |
|---|
| 251 | function getEnableBlocs($arrBlocs) |
|---|
| 252 | { |
|---|
| 253 | foreach ($arrBlocs as $key => $value) { |
|---|
| 254 | // 有効なpluginのブロック以外. |
|---|
| 255 | if (!in_array($value['plugin_id'] , $this->arrPluginIds)) { |
|---|
| 256 | // 通常ブロック以外. |
|---|
| 257 | if ($value['plugin_id'] != '') { |
|---|
| 258 | // ブロック配列から削除する |
|---|
| 259 | unset ($arrBlocs[$key]); |
|---|
| 260 | } |
|---|
| 261 | } |
|---|
| 262 | } |
|---|
| 263 | return $arrBlocs; |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | /** |
|---|
| 267 | * テンプレートのヘッダに追加するPHPのURLをセットする |
|---|
| 268 | * |
|---|
| 269 | * @param string $url PHPファイルのURL |
|---|
| 270 | * @return void |
|---|
| 271 | */ |
|---|
| 272 | function setHeadNavi($url) |
|---|
| 273 | { |
|---|
| 274 | $this->arrHeadNaviBlocsByPlugin[$url] = TARGET_ID_HEAD; |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | /** |
|---|
| 278 | * PHPのURLをテンプレートのヘッダに追加する |
|---|
| 279 | * |
|---|
| 280 | * @param array|null $arrBlocs 配置情報を含めたブロックの配列 |
|---|
| 281 | * @return void |
|---|
| 282 | */ |
|---|
| 283 | function setHeadNaviBlocs(&$arrBlocs) |
|---|
| 284 | { |
|---|
| 285 | foreach ($this->arrHeadNaviBlocsByPlugin as $key => $value) { |
|---|
| 286 | $arrBlocs[] = array( |
|---|
| 287 | 'target_id' =>$value, |
|---|
| 288 | 'php_path' => $key |
|---|
| 289 | ); |
|---|
| 290 | } |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | /** |
|---|
| 294 | * Utility function to set a hook point. |
|---|
| 295 | * |
|---|
| 296 | * @param string $hook_point hook point |
|---|
| 297 | * @param array $arrArgs argument passing to callback function |
|---|
| 298 | * @param boolean $plugin_activate_flg |
|---|
| 299 | * @return void |
|---|
| 300 | */ |
|---|
| 301 | public static function hook($hook_point, $arrArgs = array(), $plugin_activate_flg = PLUGIN_ACTIVATE_FLAG) |
|---|
| 302 | { |
|---|
| 303 | $objPlugin = SC_Helper_Plugin::getSingletonInstance($plugin_activate_flg); |
|---|
| 304 | $objPlugin->doAction($hook_point, $arrArgs); |
|---|
| 305 | } |
|---|
| 306 | } |
|---|