-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoader.php
More file actions
340 lines (300 loc) · 8.93 KB
/
Copy pathLoader.php
File metadata and controls
340 lines (300 loc) · 8.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<?php
/**
*
*===================================================================
*
* materia
*-------------------------------------------------------------------
* @category materia
* @package materia
* @author emberlabs.org
* @copyright (c) 2011 emberlabs.org
* @license MIT License
* @link https://github.com/emberlabs/materia
*
*===================================================================
*
* This source file is subject to the MIT license that is bundled
* with this package in the file LICENSE.
*
*/
namespace emberlabs\materia;
use \emberlabs\materia\Internal\MetadataException;
use \emberlabs\materia\Internal\DependencyException;
/**
* materia - Addon manager class,
* Manages the loading and initialization of addons.
*
*
* @category materia
* @package materia
* @author emberlabs.org
* @license MIT License
* @link https://github.com/emberlabs/materia
*/
class Loader implements \Iterator
{
/**
* materia version stamp
*/
const VERSION = '1.0.0-dev';
/**
* @var string - The base path to use with all addon loading.
*/
protected $base_path = '';
/**
* @var false|string - The directory containing all addon phar packages (or false if no phar loading in use)
*/
protected $addon_phar_dir = false;
/**
* @var string - The directory containing all addon include files.
*/
protected $addon_dir = '/addons/';
/**
* @var \Closure|NULL - NULL if no callback, or \Closure of the callback to run on addon load.
*/
protected $autoloader_callback = NULL;
/**
* @var array - Array of instantiated metadata objects.
*/
protected $metadata = array();
/**
* @var array - Array of loaded addon names.
*/
protected $addons_loaded = array();
/**
* Constructor
* @param string $base_path - The base load path to use.
*/
public function __construct($base_path)
{
$this->setBasePath($base_path);
}
/**
* Set the base load path.
* @param string $base_path - The base load path to use.
* @return \emberlabs\materia\Loader - Provides a fluent interface.
*/
public function setBasePath($base_path)
{
$this->base_path = rtrim($base_path, '\\/');
return $this;
}
/**
* Set the addon load directories.
* @param string $addon_dir - The directory containing the include files for all addons.
* @param string $addon_phar_dir - The directory containing addon phar packages. May be set as false to disable phar loading.
* @return \emberlabs\materia\Loader - Provides a fluent interface.
*/
public function setAddonDirs($addon_dir, $addon_phar_dir = false)
{
$this->addon_dir = '/' . rtrim(ltrim($addon_dir, '\\/'), '\\/') . '/';
$this->addon_phar_dir = ($addon_phar_dir) ? rtrim(ltrim($addon_phar_dir, '\\/'), '\\/') . '/' : false;
return $this;
}
/**
* Set a callback to be triggered on addon load, so that addons can be added to an autoloader
* @param \Closure $callback - The callback to use.
* @return \emberlabs\materia\Loader - Provides a fluent interface.
*/
public function setCallback(\Closure $callback)
{
$this->autoloader_callback = $callback;
return $this;
}
/**
* Get an addon's metadata object
* @param string $addon - The addon to load.
* @return \emberlabs\materia\Metadata\MetadataBase|NULL - NULL if no object available, or the metadata object requested
*/
public function get($addon)
{
if($this->check($addon) === true)
{
return $this->metadata[$addon];
}
else
{
return NULL;
}
}
/**
* Check to see if an addon has been loaded
* @param string $addon - The addon to check.
* @return boolean - True if addon is loaded, false if it was attempted to be loaded previously and failed, NULL if not loaded and no attempt has been made to load yet.
*/
public function check($addon)
{
if(isset($this->metadata[$addon]))
{
return ($this->metadata[$addon] !== false) ? true : false;
}
else
{
return NULL;
}
}
/**
* Loads an addon's metadata object, verifies dependencies, and initializes the addon
* @param string $addon - The addon to load.
* @param string $alias_as - Do we want to alias the addon? (Useful for loading multiple instances)
* @param boolean $ignore_phar - Do we want to ignore phar loading?
* @return void
*
*/
public function load($addon, $alias_as = '', $ignore_phar = false)
{
// Check to see if the addon has already been loaded.
if(isset($this->metadata[$addon]))
{
return;
}
$alias_as = $alias_as ?: $addon;
$using_phar = ($this->addon_phar_dir === false) ? false : !$ignore_phar;
$phar_path = $this->addon_phar_dir . $addon . '.phar';
$metadata_class = '\\emberlabs\materia\\Metadata\\' . ucfirst($addon);
if(!class_exists($metadata_class, false))
{
require $this->findMetadata($addon, $using_phar);
}
// Load the metadata object
try
{
$metadata = $this->loadMetadata($metadata_class);
}
catch(DependencyException $e)
{
$this->metadata[$addon] = false;
throw $e;
}
// If the addon's metadata object passes all checks and we're not using a phar file, then we add the addon's directory to the autoloader include path
if($using_phar)
{
$set_path = 'phar://' . $phar_path;
}
else
{
$set_path = $this->base_path . $this->addon_dir . $addon;
}
// If we need to update an autoloader with new load paths, we trigger the autoloader callback that should have been defined earlier and provide it the $set_path var
if($this->autoloader_callback !== NULL && !isset($this->addons_loaded[$addon]))
{
$_ac = $this->autoloader_callback;
$_ac($set_path);
$this->addons_loaded[$addon] = true;
}
// Initialize the addon
$metadata->setAlias($alias_as)
->initialize();
// Store the metadata object in a predictable slot.
$this->metadata[$alias_as] = $metadata;
}
/**
* Locate the addon's metadata file
* @param string $addon - The addon file to find the metadata object for
* @param boolean &$using_phar - Are we using a phar?
* @return string - The location of the metadata object file.
*
* @throws MetadataException
*/
protected function findMetadata($addon, &$using_phar)
{
$phar_path = $this->addon_phar_dir . $addon . '.phar';
$metadata_path = '/emberlabs/materia/Metadata/' . ucfirst($addon) . '.php';
// Check to see if there's a phar we are dealing with here before moving on to try to load the standard class files.
if($using_phar !== false && file_exists($this->base_path . '/' . $phar_path))
{
if(!file_exists('phar://' . $phar_path . $metadata_path))
{
throw new MetadataException('Could not locate required addon metadata file');
}
return 'phar://' . $phar_path . $metadata_path;
}
else
{
if(!file_exists($this->base_path . $this->addon_dir . $addon . $metadata_path))
{
throw new MetadataException('Could not locate addon metadata file');
}
return $this->base_path . $this->addon_dir . $addon . $metadata_path;
}
}
/**
* Load the metadata file for this addon.
* @param string $metadata_class - The class to load for the metadata object.
* @return \emberlabs\materia\Metadata\MetadataBase - The metadata object.
*
* @throws DependencyException
* @throws MetadataException
* @throws \LogicException
*/
protected function loadMetadata($metadata_class)
{
if(!class_exists($metadata_class))
{
throw new MetadataException('Addon metadata class not defined');
}
// We want to instantiate the addon's metadata object, and make sure it's the right type of object.
$metadata = new $metadata_class($this);
if(!($metadata instanceof \emberlabs\materia\Metadata\MetadataBase))
{
throw new \LogicException('Addon metadata class does not extend class MetadataBase');
}
// Let our addons check for their dependencies here.
try
{
if(!$metadata->checkDependencies())
{
throw new DependencyException('Addon metadata object declares that its required dependencies have not been met');
}
}
catch(\RuntimeException $e)
{
throw new DependencyException(sprintf('Dependency check failed, reason: %1$s', $e->getMessage()));
}
return $metadata;
}
/**
* Iterator methods
*/
/**
* Iterator method, rewinds the array back to the first element.
* @return void
*/
public function rewind()
{
return reset($this->metadata);
}
/**
* Iterator method, returns the key of the current element
* @return scalar - The key of the current element.
*/
public function key()
{
return key($this->metadata);
}
/**
* Iterator method, checks to see if the current position is valid.
* @return boolean - Whether or not the current array position is valid.
*/
public function valid()
{
return (!is_null(key($this->metadata)));
}
/**
* Iterator method, gets the current element
* @return \emberlabs\materia\Metadata\MetadataBase - The current addon metadata object of focus.
*/
public function current()
{
return current($this->metadata);
}
/**
* Iterator method, moves to the next session available.
* @return void
*/
public function next()
{
next($this->metadata);
}
}