1 | <?php |
||
11 | class AutoloadManager { |
||
12 | |||
13 | const FILENAME = 'autoload_data.php'; |
||
14 | const KEY_CLASSES = 'classes'; |
||
15 | const KEY_SCANNED_DIRS = 'scannedDirs'; |
||
16 | |||
17 | /** |
||
18 | * @var \Elgg\ClassLoader |
||
19 | */ |
||
20 | protected $loader; |
||
21 | |||
22 | /** |
||
23 | * @var array directories that have already been scanned for classes |
||
24 | */ |
||
25 | protected $scannedDirs = array(); |
||
26 | |||
27 | /** |
||
28 | * @var bool was data in the manager altered? |
||
29 | */ |
||
30 | protected $altered = false; |
||
31 | |||
32 | /** |
||
33 | * @var \ElggCache |
||
34 | */ |
||
35 | protected $storage = null; |
||
36 | |||
37 | /** |
||
38 | * Constructor |
||
39 | * |
||
40 | * @param \Elgg\ClassLoader $loader Class loader object |
||
41 | */ |
||
42 | 6 | public function __construct(\Elgg\ClassLoader $loader) { |
|
45 | |||
46 | /** |
||
47 | * Add classes found in this directory to the class map and allow classes in |
||
48 | * subdirectories to be found by PSR-0 rules. |
||
49 | * |
||
50 | * We keep track of which dirs were scanned on previous requests so we don't need to |
||
51 | * rescan unless the cache is emptied. |
||
52 | * |
||
53 | * @param string $dir Directory of classes |
||
54 | * @return \Elgg\AutoloadManager |
||
55 | */ |
||
56 | public function addClasses($dir) { |
||
66 | |||
67 | /** |
||
68 | * Scan (non-recursively) a /classes directory for PHP files to map directly to classes. |
||
69 | * |
||
70 | * For BC with Elgg 1.8's autoloader we map these files directly, but besides this |
||
71 | * the autoloader is PSR-0 compatible. |
||
72 | * |
||
73 | * @param string $dir Directory of classes |
||
74 | * @return array |
||
75 | */ |
||
76 | protected function scanClassesDir($dir) { |
||
97 | |||
98 | /** |
||
99 | * Register the location of a class on the class map |
||
100 | * |
||
101 | * @param string $class Class name |
||
102 | * @param string $path Path of class file |
||
103 | * @return \Elgg\AutoloadManager |
||
104 | */ |
||
105 | public function setClassPath($class, $path) { |
||
109 | |||
110 | /** |
||
111 | * If necessary, save necessary state details |
||
112 | * |
||
113 | * @return \Elgg\AutoloadManager |
||
114 | */ |
||
115 | public function saveCache() { |
||
126 | |||
127 | /** |
||
128 | * Set the state of the manager from the cache |
||
129 | * |
||
130 | * @return bool was the cache loaded? |
||
131 | */ |
||
132 | public function loadCache() { |
||
146 | |||
147 | /** |
||
148 | * Some method that does something |
||
149 | * |
||
150 | * @todo what is a spec? |
||
151 | * @return bool|array |
||
152 | */ |
||
153 | protected function getSpec() { |
||
165 | |||
166 | /** |
||
167 | * Delete the cache file |
||
168 | * |
||
169 | * @return \Elgg\AutoloadManager |
||
170 | */ |
||
171 | public function deleteCache() { |
||
177 | |||
178 | /** |
||
179 | * Get the class loader |
||
180 | * |
||
181 | * @return \Elgg\ClassLoader |
||
182 | */ |
||
183 | public function getLoader() { |
||
186 | |||
187 | /** |
||
188 | * Set the cache storage object |
||
189 | * |
||
190 | * @param \ElggCache $storage Cache object |
||
191 | * @return void |
||
192 | */ |
||
193 | public function setStorage(\ElggCache $storage) { |
||
196 | } |
||
197 | |||
198 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.