|
1
|
|
|
<?php namespace BuildR\ClassLoader; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* The class loader component initializer. Because the class loader not have |
|
5
|
|
|
* a loader, this class will help to load all files that needs to be loaded |
|
6
|
|
|
* before start using the class loader. |
|
7
|
|
|
* |
|
8
|
|
|
* BuildR PHP Framework |
|
9
|
|
|
* |
|
10
|
|
|
* @author Zoltán Borsos <[email protected]> |
|
11
|
|
|
* @package ClassLoader |
|
12
|
|
|
* |
|
13
|
|
|
* @copyright Copyright 2015, Zoltán Borsos. |
|
14
|
|
|
* @license https://github.com/Zolli/BuildR/blob/master/LICENSE.md |
|
15
|
|
|
* @link https://github.com/Zolli/BuildR |
|
16
|
|
|
*/ |
|
17
|
|
|
class ClassLoaderInitializer { |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @type bool |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $isLoaded = FALSE; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The files that loaded during the initialization phase |
|
26
|
|
|
* |
|
27
|
|
|
* @type array |
|
28
|
|
|
*/ |
|
29
|
|
|
protected static $files = [ |
|
30
|
|
|
'Modules' . DIRECTORY_SEPARATOR . 'ClassLoaderModuleInterface.php', |
|
31
|
|
|
'Modules' . DIRECTORY_SEPARATOR . 'AbstractClassLoaderModule.php', |
|
32
|
|
|
'Exception' . DIRECTORY_SEPARATOR . 'ModuleException.php', |
|
33
|
|
|
'Exception' . DIRECTORY_SEPARATOR . 'ClassLoaderException.php', |
|
34
|
|
|
'ModuleLoader.php', |
|
35
|
|
|
'ClassLoader.php', |
|
36
|
|
|
]; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Extends the initializer internal file registry before initialize |
|
40
|
|
|
* |
|
41
|
|
|
* @param $additionalFile |
|
42
|
|
|
*/ |
|
43
|
2 |
|
public function extend($additionalFile) { |
|
44
|
2 |
|
if($this->isLoaded === TRUE) { |
|
45
|
1 |
|
trigger_error('The initializer is loaded, so you cannot extend a loaded initializer!', E_USER_NOTICE); |
|
46
|
1 |
|
} |
|
47
|
|
|
|
|
48
|
2 |
|
self::$files = array_merge(self::$files, (array) $additionalFile); |
|
49
|
2 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Determines that the current instance of the initializer is loaded or not |
|
53
|
|
|
* |
|
54
|
|
|
* @return bool |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public function isLoaded() { |
|
57
|
1 |
|
return (bool) $this->isLoaded; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Returns all files loaded by the initializer |
|
62
|
|
|
* |
|
63
|
|
|
* @return array |
|
64
|
|
|
*/ |
|
65
|
2 |
|
public static function getLoadedFiles() { |
|
66
|
2 |
|
return self::$files; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Load all files that needs to use the class loader |
|
71
|
|
|
* |
|
72
|
|
|
* @return void |
|
73
|
|
|
*/ |
|
74
|
4 |
|
public function load() { |
|
75
|
4 |
|
if($this->isLoaded === TRUE) { |
|
76
|
1 |
|
trigger_error("Unable to load ClassLoader because its already loaded!", E_USER_NOTICE); |
|
77
|
1 |
|
} |
|
78
|
|
|
|
|
79
|
4 |
|
$rootDir = __DIR__ . DIRECTORY_SEPARATOR; |
|
80
|
4 |
|
ksort(self::$files); |
|
81
|
|
|
|
|
82
|
4 |
|
foreach(self::$files as $priority => $file) { |
|
83
|
4 |
|
$fileAbsolute = $rootDir . $file; |
|
84
|
|
|
|
|
85
|
4 |
|
include_once $fileAbsolute; |
|
86
|
4 |
|
} |
|
87
|
|
|
|
|
88
|
4 |
|
$this->isLoaded = TRUE; |
|
89
|
4 |
|
} |
|
90
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|