|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Magento |
|
4
|
|
|
* |
|
5
|
|
|
* NOTICE OF LICENSE |
|
6
|
|
|
* |
|
7
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
8
|
|
|
* that is bundled with this package in the file LICENSE.txt. |
|
9
|
|
|
* It is also available through the world-wide-web at this URL: |
|
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
|
11
|
|
|
* If you did not receive a copy of the license and are unable to |
|
12
|
|
|
* obtain it through the world-wide-web, please send an email |
|
13
|
|
|
* to [email protected] so we can send you a copy immediately. |
|
14
|
|
|
* |
|
15
|
|
|
* DISCLAIMER |
|
16
|
|
|
* |
|
17
|
|
|
* Do not edit or add to this file if you wish to upgrade Magento to newer |
|
18
|
|
|
* versions in the future. If you wish to customize Magento for your |
|
19
|
|
|
* needs please refer to http://www.magentocommerce.com for more information. |
|
20
|
|
|
* |
|
21
|
|
|
* @category MageTest |
|
22
|
|
|
* @package PhpSpec_MagentoExtension |
|
23
|
|
|
* @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com) |
|
24
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
25
|
|
|
*/ |
|
26
|
|
|
namespace MageTest\PhpSpec\MagentoExtension\Autoloader; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Classes source autoload |
|
30
|
|
|
*/ |
|
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') |
|
51
|
|
|
{ |
|
52
|
|
|
$this->srcPath = $srcPath; |
|
53
|
|
|
$this->codePool = $codePool; |
|
54
|
|
|
$this->isIncludePathDefined = defined('COMPILER_INCLUDE_PATH'); |
|
55
|
|
|
if ($this->isIncludePathDefined) { |
|
56
|
|
|
$this->collectClasses = true; |
|
57
|
|
|
$this->collectPath = COMPILER_COLLECT_PATH; |
|
58
|
|
|
} |
|
59
|
|
|
set_include_path(get_include_path() . PATH_SEPARATOR . $this->srcPath . $this->codePool); |
|
60
|
|
|
self::registerScope(self::$scope); |
|
61
|
|
|
} |
|
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) |
|
71
|
|
|
{ |
|
72
|
|
|
if (!self::$instance) { |
|
73
|
|
|
self::$instance = new MageLoader($srcPath, $codePool); |
|
74
|
|
|
} |
|
75
|
|
|
return self::$instance; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Register SPL autoload function |
|
80
|
|
|
* @param string $srcPath |
|
81
|
|
|
* @param string $codePool |
|
82
|
|
|
*/ |
|
83
|
|
|
public static function register($srcPath, $codePool) |
|
84
|
|
|
{ |
|
85
|
|
|
spl_autoload_register(array(self::instance($srcPath, $codePool), 'autoload')); |
|
86
|
|
|
} |
|
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) |
|
121
|
|
|
{ |
|
122
|
|
|
self::$scope = $code; |
|
123
|
|
|
if (defined('COMPILER_INCLUDE_PATH')) { |
|
124
|
|
|
$file = COMPILER_INCLUDE_PATH . DIRECTORY_SEPARATOR . self::SCOPE_FILE_PREFIX . $code . '.php'; |
|
125
|
|
|
if (file_exists($file)) { |
|
126
|
|
|
include $file; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Get current autoload scope |
|
133
|
|
|
* |
|
134
|
|
|
* @return string |
|
135
|
|
|
*/ |
|
136
|
|
|
public static function getScope() |
|
137
|
|
|
{ |
|
138
|
|
|
return self::$scope; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Class destructor |
|
143
|
|
|
*/ |
|
144
|
|
|
public function __destruct() |
|
145
|
|
|
{ |
|
146
|
|
|
if ($this->collectClasses) { |
|
147
|
|
|
$this->saveCollectedState(); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
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() |
|
158
|
|
|
{ |
|
159
|
|
|
$this->prepareCollectPath(); |
|
160
|
|
|
|
|
161
|
|
|
if (!is_writeable($this->collectPath)) { |
|
162
|
|
|
return $this; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
foreach ($this->arrLoadedClasses as $scope => $classes) { |
|
166
|
|
|
$this->saveClassScope($scope, $classes); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
return $this; |
|
170
|
|
|
} |
|
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) |
|
196
|
|
|
{ |
|
197
|
|
|
if ($this->isIncludePathDefined) { |
|
198
|
|
|
return COMPILER_INCLUDE_PATH . DIRECTORY_SEPARATOR . $class; |
|
199
|
|
|
} |
|
200
|
|
|
return str_replace(' ', DIRECTORY_SEPARATOR, ucwords(str_replace('_', ' ', $class))); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
private function prepareCollectPath() |
|
204
|
|
|
{ |
|
205
|
|
|
if (!is_dir($this->collectPath)) { |
|
206
|
|
|
mkdir($this->collectPath); |
|
207
|
|
|
chmod($this->collectPath, 0777); |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* @param $scope |
|
213
|
|
|
* @param $classes |
|
214
|
|
|
*/ |
|
215
|
|
|
protected function saveClassScope($scope, $classes) |
|
216
|
|
|
{ |
|
217
|
|
|
$file = $this->collectPath . DIRECTORY_SEPARATOR . $scope . '.csv'; |
|
218
|
|
|
|
|
219
|
|
|
if (!file_exists($file)) { |
|
220
|
|
|
return; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
$data = $this->extractDataFromFile($classes, $file); |
|
224
|
|
|
|
|
225
|
|
|
file_put_contents($file, implode("\n", $data)); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* @param $classes |
|
230
|
|
|
* @param $file |
|
231
|
|
|
* @return array |
|
232
|
|
|
*/ |
|
233
|
|
|
protected function extractDataFromFile($classes, $file) |
|
234
|
|
|
{ |
|
235
|
|
|
$data = explode("\n", file_get_contents($file)); |
|
236
|
|
|
|
|
237
|
|
|
foreach ($data as $index => $class) { |
|
238
|
|
|
$class = explode(':', $class); |
|
239
|
|
|
$searchIndex = array_search($class[0], $classes); |
|
240
|
|
|
if ($searchIndex !== false) { |
|
241
|
|
|
$class[1] += 1; |
|
242
|
|
|
unset($classes[$searchIndex]); |
|
243
|
|
|
} |
|
244
|
|
|
$data[$index] = $class[0] . ':' . $class[1]; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
foreach ($classes as $class) { |
|
248
|
|
|
$data[] = $class . ':1'; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
return $data; |
|
252
|
|
|
} |
|
253
|
|
|
} |
|
254
|
|
|
|