1 | <?php |
||
31 | class MageLoader |
||
32 | { |
||
33 | const SCOPE_FILE_PREFIX = '__'; |
||
34 | |||
35 | static protected $instance; |
||
36 | static protected $scope = 'default'; |
||
37 | |||
38 | protected $isIncludePathDefined = false; |
||
39 | protected $collectClasses = false; |
||
40 | protected $collectPath = null; |
||
41 | protected $arrLoadedClasses = array(); |
||
42 | protected $srcPath = ''; |
||
43 | protected $codePool = ''; |
||
44 | |||
45 | /** |
||
46 | * Class constructor |
||
47 | * @param string $srcPath |
||
48 | * @param string $codePool |
||
49 | */ |
||
50 | public function __construct($srcPath, $codePool = 'local') |
||
62 | |||
63 | /** |
||
64 | * Singleton pattern implementation |
||
65 | * |
||
66 | * @param string $srcPath |
||
67 | * @param string $codePool |
||
68 | * @return MageLoader |
||
69 | */ |
||
70 | public static function instance($srcPath, $codePool) |
||
77 | |||
78 | /** |
||
79 | * Register SPL autoload function |
||
80 | * @param string $srcPath |
||
81 | * @param string $codePool |
||
82 | */ |
||
83 | public static function register($srcPath, $codePool) |
||
87 | |||
88 | /** |
||
89 | * Load class source code |
||
90 | * |
||
91 | * @param string $class |
||
92 | * @return bool|mixed |
||
93 | */ |
||
94 | public function autoload($class) |
||
95 | { |
||
96 | if ($this->collectClasses) { |
||
97 | $this->arrLoadedClasses[self::$scope][] = $class; |
||
98 | } |
||
99 | |||
100 | if (substr($class, -10) === 'Controller') { |
||
101 | return $this->includeController($class); |
||
102 | } |
||
103 | |||
104 | $classFile = $this->getClassFile($class) . '.php'; |
||
105 | |||
106 | if (!stream_resolve_include_path($classFile)) { |
||
107 | return false; |
||
108 | } |
||
109 | |||
110 | return include $classFile; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Register autoload scope |
||
115 | * This process allow include scope file which can contain classes |
||
116 | * definition which are used for this scope |
||
117 | * |
||
118 | * @param string $code scope code |
||
119 | */ |
||
120 | public static function registerScope($code) |
||
130 | |||
131 | /** |
||
132 | * Get current autoload scope |
||
133 | * |
||
134 | * @return string |
||
135 | */ |
||
136 | public static function getScope() |
||
140 | |||
141 | /** |
||
142 | * Class destructor |
||
143 | */ |
||
144 | public function __destruct() |
||
150 | |||
151 | /** |
||
152 | * Save information about used classes per scope with class popularity |
||
153 | * Class_Name:popularity |
||
154 | * |
||
155 | * @return MageLoader |
||
156 | */ |
||
157 | protected function saveCollectedState() |
||
171 | |||
172 | /** |
||
173 | * Includes a controller given a controller class name |
||
174 | * |
||
175 | * @param string $class controller class name |
||
176 | * @return bool|mixed |
||
177 | */ |
||
178 | private function includeController($class) |
||
179 | { |
||
180 | $local = $this->srcPath . DIRECTORY_SEPARATOR . $this->codePool . DIRECTORY_SEPARATOR; |
||
181 | $controller = explode('_', $class); |
||
182 | array_splice($controller, 2, 0, 'controllers'); |
||
183 | $pathToController = implode(DIRECTORY_SEPARATOR, $controller); |
||
184 | $classFile = $local . $pathToController . '.php'; |
||
185 | if (!file_exists($classFile)) { |
||
186 | return false; |
||
187 | } |
||
188 | return include_once $classFile; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @param string $class |
||
193 | * @return string |
||
194 | */ |
||
195 | private function getClassFile($class) |
||
202 | |||
203 | private function prepareCollectPath() |
||
210 | |||
211 | /** |
||
212 | * @param $scope |
||
213 | * @param $classes |
||
214 | */ |
||
215 | protected function saveClassScope($scope, $classes) |
||
227 | |||
228 | /** |
||
229 | * @param $classes |
||
230 | * @param $file |
||
231 | * @return array |
||
232 | */ |
||
233 | protected function extractDataFromFile($classes, $file) |
||
253 | } |
||
254 |