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 = []; |
||
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 | 1 | 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 | * If necessary, save necessary state details |
||
100 | * |
||
101 | * @return \Elgg\AutoloadManager |
||
102 | */ |
||
103 | 1 | public function saveCache() { |
|
114 | |||
115 | /** |
||
116 | * Set the state of the manager from the cache |
||
117 | * |
||
118 | * @return bool was the cache loaded? |
||
119 | */ |
||
120 | 1 | public function loadCache() { |
|
134 | |||
135 | /** |
||
136 | * Tries to read the contents of the cache file and if valid returns the content |
||
137 | * |
||
138 | * @return false|array |
||
139 | */ |
||
140 | 1 | protected function getCacheFileContents() { |
|
141 | 1 | if (!$this->storage) { |
|
142 | return false; |
||
143 | } |
||
144 | |||
145 | 1 | $serialization = $this->storage->load(self::FILENAME); |
|
146 | 1 | if (!$serialization) { |
|
147 | 1 | return false; |
|
148 | } |
||
149 | |||
150 | $spec = unserialize($serialization); |
||
151 | if (isset($spec[self::KEY_CLASSES])) { |
||
152 | return $spec; |
||
153 | } |
||
154 | |||
155 | return false; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Delete the cache file |
||
160 | * |
||
161 | * @return \Elgg\AutoloadManager |
||
162 | */ |
||
163 | public function deleteCache() { |
||
169 | |||
170 | /** |
||
171 | * Get the class loader |
||
172 | * |
||
173 | * @return \Elgg\ClassLoader |
||
174 | */ |
||
175 | public function getLoader() { |
||
178 | |||
179 | /** |
||
180 | * Set the cache storage object |
||
181 | * |
||
182 | * @param \ElggCache $storage Cache object |
||
183 | * @return void |
||
184 | */ |
||
185 | 1 | public function setStorage(\ElggCache $storage) { |
|
188 | |||
189 | /** |
||
190 | * Save the cache on object destruction |
||
191 | * |
||
192 | * @return void |
||
193 | */ |
||
194 | 1 | public function __destruct() { |
|
197 | } |
||
198 |