source: branches/version-2_12-dev/data/class/helper/SC_Helper_Plugin.php @ 21563

Revision 21563, 12.8 KB checked in by Seasoft, 12 years ago (diff)

#1669 (変数の初期化漏れ)
#1613 (typo修正・ソース整形・ソースコメントの改善)

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-httpd-php; charset=UTF-8
Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2012 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 */
30class SC_Helper_Plugin {
31    // プラグインのインスタンスの配列.
32    var $arrPluginInstances = array();
33    // プラグインのアクションの配列.
34    var $arrRegistedPluginActions = array();
35    // プラグインのIDの配列.
36    var $arrPluginIds = array();
37    // HeadNaviブロックの配列
38    var $arrHeadNaviBlocsByPlugin = array();
39
40    /**
41     * 有効なプラグインのロード. プラグインエンジンが有効になっていない場合は
42     * プラグインエンジン自身のインストール処理を起動する
43     *
44     * @return void
45     */
46    function load($plugin_activate_flg = true) {
47
48        if (!defined('CONFIG_REALFILE') || !file_exists(CONFIG_REALFILE)) return; // インストール前
49        if (SC_Utils_Ex::sfIsInstallFunction()) return; // インストール中
50        if ($plugin_activate_flg === false) return;
51        // 有効なプラグインを取得
52        $arrPluginDataList = $this->getEnablePlugin();
53        // pluginディレクトリを取得
54        $arrPluginDirectory = $this->getPluginDirectory();
55
56        foreach ($arrPluginDataList as $arrPluginData) {
57            // プラグイン本体ファイル名が取得したプラグインディレクトリ一覧にある事を確認
58            if (array_search($arrPluginData['plugin_code'], $arrPluginDirectory) !== false) {
59                // プラグイン本体ファイルをrequire.
60                require_once PLUGIN_UPLOAD_REALDIR . $arrPluginData['plugin_code'] . '/' . $arrPluginData['class_name'] . '.php';
61
62                // プラグインのインスタンス生成.
63                $objPlugin = new $arrPluginData['class_name']($arrPluginData);
64                // メンバ変数にプラグインのインスタンスを登録.
65                $this->arrPluginInstances[$arrPluginData['plugin_id']] = $objPlugin;
66                $this->arrPluginIds[] = $arrPluginData['plugin_id'];
67                // ローカルフックポイントの登録.
68                $this->registLocalHookPoint($objPlugin, $arrPluginData['priority']);
69                // スーパーフックポイントの登録.
70                $this->registSuperHookPoint($objPlugin, HOOK_POINT_PREPROCESS, 'preProcess', $arrPluginData['priority']);
71                $this->registSuperHookPoint($objPlugin, HOOK_POINT_PROCESS, 'process', $arrPluginData['priority']);
72            }
73        }
74    }
75
76    /**
77     * SC_Helper_Plugin オブジェクトを返す(Singletonパターン)
78     *
79     * @return object SC_Helper_Pluginオブジェクト
80     */
81    function getSingletonInstance($plugin_activate_flg = true) {
82        if (!isset($GLOBALS['_SC_Helper_Plugin_instance']) || is_null($GLOBALS['_SC_Helper_Plugin_instance'])) {
83            $GLOBALS['_SC_Helper_Plugin_instance'] =& new SC_Helper_Plugin_Ex();
84            $GLOBALS['_SC_Helper_Plugin_instance']->load($plugin_activate_flg);
85        }
86        return $GLOBALS['_SC_Helper_Plugin_instance'];
87    }
88
89    /**
90     * プラグイン実行
91     *
92     * @param string $hook_point フックポイント
93     * @param array  $arrArgs    コールバック関数へ渡す引数
94     * @return void
95     */
96    function doAction($hook_point, $arrArgs = array()) {
97        if (is_array($arrArgs) === false) {
98            array(&$arrArgs);
99        }
100
101        if (array_key_exists($hook_point, $this->arrRegistedPluginActions)
102            && is_array($this->arrRegistedPluginActions[$hook_point])) {
103
104            krsort($this->arrRegistedPluginActions[$hook_point]);
105            foreach ($this->arrRegistedPluginActions[$hook_point] as $priority => $arrFuncs) {
106
107                foreach ($arrFuncs as $func) {
108                    if (!is_null($func['function'])) {
109                        call_user_func_array($func['function'], $arrArgs);
110                    }
111                }
112            }
113        }
114    }
115
116    /**
117     * 稼働中のプラグインを取得する。
118     */
119    function getEnablePlugin() {
120        $objQuery = new SC_Query_Ex();
121        $col = '*';
122        $table = 'dtb_plugin';
123        $where = 'enable = 1';
124        // XXX 2.11.0 互換のため
125        $arrCols = $objQuery->listTableFields($table);
126        if (in_array('priority', $arrCols)) {
127            $objQuery->setOrder('priority DESC, plugin_id ASC');
128        }
129        $arrRet = $objQuery->select($col,$table,$where);
130        return $arrRet;
131    }
132
133    /**
134     * インストールされているプラグインを取得する。
135     *
136     * @return array $arrRet インストールされているプラグイン.
137     */
138    function getAllPlugin() {
139        $objQuery = new SC_Query_Ex();
140        $col = '*';
141        $table = 'dtb_plugin';
142        // XXX 2.11.0 互換のため
143        $arrCols = $objQuery->listTableFields($table);
144        if (in_array('priority', $arrCols)) {
145            $objQuery->setOrder('plugin_id ASC');
146        }
147        $arrRet = $objQuery->select($col,$table);
148        return $arrRet;
149    }
150
151    /**
152     * プラグインIDをキーにプラグインを取得する。
153     *
154     * @param int $plugin_id プラグインID.
155     * @return array プラグインの基本情報.
156     */
157    function getPluginByPluginId($plugin_id) {
158        $objQuery =& SC_Query_Ex::getSingletonInstance();
159        $col = '*';
160        $table = 'dtb_plugin';
161        $where = 'plugin_id = ?';
162        $plugin = $objQuery->getRow($col, $table, $where, array($plugin_id));
163        return $plugin;
164    }
165
166    /**
167     * プラグインコードをキーにプラグインを取得する。
168     *
169     * @param string $plugin_code プラグインコード.
170     * @return array プラグインの基本情報.
171     */
172    function getPluginByPluginCode($plugin_code) {
173        $objQuery =& SC_Query_Ex::getSingletonInstance();
174        $col = '*';
175        $table = 'dtb_plugin';
176        $where = 'plugin_code = ?';
177        $plugin = $objQuery->getRow($col, $table, $where, array($plugin_code));
178        return $plugin;
179    }
180
181    /**
182     * プラグインIDをキーにプラグインを削除する。
183     *
184     * @param string $plugin_id プラグインID.
185     * @return array プラグインの基本情報.
186     */
187    function deletePluginByPluginId($plugin_id) {
188        $objQuery =& SC_Query_Ex::getSingletonInstance();
189        $objQuery->begin();
190        $where = 'plugin_id = ?';
191        $objQuery->delete('dtb_plugin', $where, array($plugin_id));
192        $objQuery->delete('dtb_plugin_hookpoint', $where, array($plugin_id));
193    }
194
195    /**
196     * プラグインディレクトリの取得
197     *
198     * @return array $arrPluginDirectory
199     */
200    function getPluginDirectory() {
201        $arrPluginDirectory = array();
202        if (is_dir(PLUGIN_UPLOAD_REALDIR)) {
203            if ($dh = opendir(PLUGIN_UPLOAD_REALDIR)) {
204                while (($pluginDirectory = readdir($dh)) !== false) {
205                    $arrPluginDirectory[] = $pluginDirectory;
206                }
207                closedir($dh);
208            }
209        }
210        return $arrPluginDirectory;
211    }
212
213    /**
214     * スーパーフックポイントを登録します.
215     *
216     * @param Object $objPlugin プラグインのインスタンス
217     * @param string $hook_point スーパーフックポイント
218     * @param string $function_name 実行する関数名
219     * @param string $priority 実行順
220     */
221    function registSuperHookPoint($objPlugin, $hook_point, $function_name, $priority) {
222        // スーパープラグイン関数を定義しているかを検証.
223        if (method_exists($objPlugin, $function_name) === true) {
224            // アクションの登録
225            $this->addAction($hook_point, array($objPlugin, $function_name), $priority);
226        }
227    }
228
229    /**
230     * ローカルフックポイントを登録します.
231     *
232     * @param Object $objPlugin プラグインのインスタンス
233     * @param string $priority 実行順
234     */
235    function registLocalHookPoint($objPlugin, $priority) {
236        // ローカルプラグイン関数を定義しているかを検証.
237        if (method_exists($objPlugin, 'regist') === true) {
238            // アクションの登録(プラグイン側に記述)
239            $objPluginHelper =& SC_Helper_Plugin::getSingletonInstance();
240            $objPlugin->regist($objPluginHelper, $priority);
241        }
242    }
243
244    /**
245     * プラグイン コールバック関数を追加する
246     *
247     * @param string   $hook_point フックポイント名
248     * @param callback $function   コールバック関数名
249     * @param string   $priority   同一フックポイント内での実行優先度
250     * @return boolean 成功すればtrue
251     */
252    function addAction($hook_point, $function, $priority) {
253        if (!is_callable($function)) {
254            // TODO エラー処理; コール可能な形式ではありません
255        }
256        $idx = $this->makeActionUniqueId($hook_point, $function, $priority);
257        $this->arrRegistedPluginActions[$hook_point][$priority][$idx] = array('function' => $function);
258        return true;
259    }
260
261    /**
262     * コールバック関数を一意に識別するIDの生成
263     *
264     * @param string   $hook_point フックポイント名
265     * @param callback $function   コールバック関数名
266     * @param integer  $priority   同一フックポイント内での実行優先度
267     * @return string コールバック関数を一意に識別するID
268     */
269    function makeActionUniqueId($hook_point, $function, $priority) {
270        static $filter_id_count = 0;
271
272        if (is_string($function)) {
273            return $function;
274        }
275
276        if (is_object($function)) {
277            $function = array($function, '');
278        } else {
279            $function = (array) $function;
280        }
281
282        if (is_object($function[0])) {
283            if (function_exists('spl_object_hash')) {
284                return spl_object_hash($function[0]) . $function[1];
285            } else {
286                $obj_idx = get_class($function[0]).$function[1];
287                if ( false === $priority)
288                    return false;
289                $obj_idx .= isset($this->arrRegistedPluginActions[$hook_point][$priority])
290                         ? count((array)$this->arrRegistedPluginActions[$hook_point][$priority])
291                         : $filter_id_count;
292                $function[0]->wp_filter_id = $filter_id_count;
293                ++$filter_id_count;
294
295                return $obj_idx;
296            }
297        } else if (is_string($function[0])) {
298            return $function[0].$function[1];
299        }
300    }
301
302    /**
303     * ブロックの配列から有効でないpluginのブロックを除外して返します.
304     *
305     * @param array $arrBlocs プラグインのインストールディレクトリ
306     * @return array $arrBlocsサイトルートからメディアディレクトリへの相対パス
307     */
308    function getEnableBlocs($arrBlocs) {
309        foreach ($arrBlocs as $key => $value) {
310            // 有効なpluginのブロック以外.
311            if (!in_array($value['plugin_id'] , $this->arrPluginIds)) {
312                // 通常ブロック以外.
313                if ($value['plugin_id'] != '') {
314                    // ブロック配列から削除する
315                    unset ($arrBlocs[$key]);
316                }
317            }
318        }
319        return $arrBlocs;
320    }
321
322   /**
323     * テンプレートのヘッダに追加するPHPのURLをセットする
324     *
325     * @param string $url PHPファイルのURL
326     * @return void
327     */
328    function setHeadNavi($url) {
329        $this->arrHeadNaviBlocsByPlugin[$url] = TARGET_ID_HEAD;
330    }
331
332    /**
333     * PHPのURLをテンプレートのヘッダに追加する
334     *
335     * @param array|null $arrBlocs  配置情報を含めたブロックの配列
336     * @return void
337     */
338    function setHeadNaviBlocs(&$arrBlocs) {
339        foreach ($this->arrHeadNaviBlocsByPlugin as $key => $value) {
340            $arrBlocs[] = array(
341                'target_id' =>$value,
342                'php_path' => $key
343            );
344        }
345    }
346}
Note: See TracBrowser for help on using the repository browser.