source: branches/version-2_12-dev/data/class/SC_ClassAutoloader.php @ 21742

Revision 21742, 4.5 KB checked in by AMUAMU, 12 years ago (diff)

#1692 (プラグイン機能) SC_系のクラス読込をプラグインがフックして、別のクラスファイルを使えるようにする修正。詳しくはプラグイン仕様参照。

Line 
1<?php
2/*
3 * This file is part of EC-CUBE
4 *
5 * Copyright(c) 2000-2011 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 Page
28 * @author LOCKON CO.,LTD.
29 * @version $Id$
30 */
31class SC_ClassAutoloader {
32    /**
33     * クラスのオートローディング本体
34     *
35     * LC_* には対応していない。
36     * @return void
37     */
38    public static function autoload($class) {
39        $arrClassNamePart = explode('_', $class);
40        $is_ex = end($arrClassNamePart) === 'Ex';
41        $count = count($arrClassNamePart);
42        $classpath = $is_ex ? CLASS_EX_REALDIR : CLASS_REALDIR;
43
44        if (($arrClassNamePart[0] === 'GC' || $arrClassNamePart[0] === 'SC') && $arrClassNamePart[1] === 'Utils') {
45            $classpath .= $is_ex ? 'util_extends/' : 'util/';
46        }
47        elseif ($arrClassNamePart[0] === 'SC' && $is_ex === true && $count >= 4) {
48            $arrClassNamePartTemp = $arrClassNamePart;
49            // FIXME クラスファイルのディレクトリ命名が変。変な現状に合わせて強引な処理をしてる。
50            $arrClassNamePartTemp[1] = $arrClassNamePartTemp[1] . '_extends';
51            $classpath .= strtolower(implode('/', array_slice($arrClassNamePartTemp, 1, -2))) . '/';
52        }
53        elseif ($arrClassNamePart[0] === 'SC' && $is_ex === false && $count >= 3) {
54            $classpath .= strtolower(implode('/', array_slice($arrClassNamePart, 1, -1))) . '/';
55        }
56        elseif ($arrClassNamePart[0] === 'SC') {
57            // 処理なし
58        }
59        // PEAR用
60        // FIXME トリッキー
61        else {
62            $classpath = '';
63            $class = str_replace('_', '/', $class);
64        }
65
66        $classpath .= "$class.php";
67
68        // プラグイン向けフックポイント
69        // MEMO: プラグインのローダーがDB接続を必要とするため、SC_Queryがロードされた後のみ呼び出される。
70        //       プラグイン情報のキャッシュ化が行われれば、全部にフックさせることを可能に?
71        if (isset($GLOBALS['_SC_Query_instance'])
72            && !is_null($GLOBALS['_SC_Query_instance'])) {
73            $objPlugin = SC_Helper_Plugin_Ex::getSingletonInstance(true);
74
75            // 元の設定を一時保存
76            $plugin_class = $class;
77            $plugin_classpath = $classpath;
78            $objPlugin->doAction('loadClassFileChange', array(&$plugin_class, &$plugin_classpath));
79            // FIXME: トリッキーな処理で _Ex ファイルを無視しないようにする(無視するとユーザーカスタマイズで分かりにくい)
80            //        SC_XXXX_Ex がロードされる場合にextendsのchainを
81            //        SC_XXXX_Ex -> SC_XXXX から、 SC_XXXX_Ex -> $class (-> SC_XXXX) と変える。
82            //        そうでない場合は、直接置き換えと想定して帰ってきたクラスをロードする
83            if ($plugin_class !== $class) {
84                if ($is_ex) {
85                    // Ex ファイルへのフックの場合のみチェイン変更する。
86                    $exp = "/(class[ ]+{$class}[ ]+extends +)[a-zA-Z_\-]+( *{)/";
87                    $replace = '$1' . $plugin_class . '$2';
88                    $base_class_str = file_get_contents($classpath);
89                    $base_class_str = str_replace(array('<?php', '?>'), '', $base_class_str);
90                    $base_class_str = preg_replace($exp, $replace, $base_class_str, 1);
91                    include $plugin_classpath;
92                    eval($base_class_str);
93                    return;
94                } else {
95                    include $plugin_classpath;
96                }
97            }
98        }
99        include $classpath;
100    }
101}
Note: See TracBrowser for help on using the repository browser.