Repository navigation
Expand file tree
/
Copy pathClassLoader.php
More file actions
executable file
·190 lines (167 loc) · 4.97 KB
/
Copy pathClassLoader.php
File metadata and controls
executable file
·190 lines (167 loc) · 4.97 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
<?php
/**
* Webiny Framework (http://www.webiny.com/framework)
*
* @copyright Copyright Webiny LTD
*/
namespace Webiny\Component\ClassLoader;
use Webiny\Component\Cache\CacheStorage;
require_once __DIR__ . '/Loaders/LoaderAbstract.php';
require_once __DIR__ . '/Loaders/Pear.php';
require_once __DIR__ . '/Loaders/Psr0.php';
require_once __DIR__ . '/Loaders/Psr4.php';
/**
* Class loader implements a more standardized way of autoloading files.
*
* @package Webiny\Component\ClassLoader
*/
class ClassLoader
{
/**
* @var null Holds the ClassLoader instance.
*/
private static $_instance = null;
/**
* @var bool|CacheInterface
*/
private $_cache = false;
/**
* Base constructor.
*/
private function __construct()
{
// omit the constructor
}
/**
* Get an instance of ClassLoader.
*
* @return $this
*/
static public function getInstance()
{
if (self::$_instance != null) {
return self::$_instance;
}
self::$_instance = new static;
self::_registerAutoloader();
return self::$_instance;
}
/**
* Sets a cache layer in front of the autoloader.
* Unregister the old ClassLoader::getClass autoload method.
*
* @param CacheStorage $cache Instance of the \Webiny\Component\Cache\Cache class.
*
* @throws \Exception
*/
public function registerCacheDriver(CacheStorage $cache)
{
// set cache
$this->_cache = $cache;
// unregister the old autoloader
spl_autoload_unregister([
self::$_instance,
'getClass'
]
);
// prepend the new cache autoloader
spl_autoload_register([
self::$_instance,
'getClassFromCache'
], true, true
);
}
/**
* Register a namespace or PEAR map rule.
* NOTE: PEAR rules must end with an underline '_'.
*
* @param array $maps - Array of maps rules. An example rule is ['Webiny' => '/var/WebinyFramework/library']
*/
public function registerMap(array $maps)
{
foreach ($maps as $prefix => $library) {
$endChar = substr($prefix, -1);
if ($endChar == '_') {
Loaders\Pear::getInstance()->registerMap($prefix, $library);
} else {
if (is_array($library)) {
if (isset($library['Psr']) && $library['Psr'] == '0') {
Loaders\Psr0::getInstance()->registerMap($prefix, $library);
continue;
}
}
Loaders\Psr4::getInstance()->registerMap($prefix, $library);
}
}
}
/**
* Tries to find the class file based on currently registered rules.
*
* @param string $class Name of the class you are trying to find.
*
* @return bool True is returned if the class if found and loaded into memory.
*/
public function getClass($class)
{
if ($file = $this->findClass($class)) {
require $file;
return true;
}
return false;
}
/**
* First tries to find the class in the cache. If the class is not found in the cache, then it tries to find it
* by using the registered maps.
*
* @param string $class Name of the class you are trying to find.
*
* @return bool True is retuned if the class if found and loaded into memory.
*/
public function getClassFromCache($class)
{
// from cache
if (($file = $this->_cache->read($class))) {
require $file;
}
// from disk
if ($file = $this->findClass($class)) {
$this->_cache->save('wf.component.class_loader.' . $class, $file, 600, [
'_wf',
'_component',
'_class_loader'
]
);
require $file;
}
}
/**
* Tries to get the path to the class based on registered maps.
*
* @param string $class The name of the class
*
* @return string|bool The path, if found, or false.
*/
public function findClass($class)
{
if (strrpos($class, '\\') !== false) {
$file = Loaders\Psr0::getInstance()->findClass($class);
if (!$file) {
$file = Loaders\Psr4::getInstance()->findClass($class);
}
} else {
$file = Loaders\Pear::getInstance()->findClass($class);
}
return $file;
}
/**
* Registers SPL autoload function.
*/
private static function _registerAutoloader()
{
spl_autoload_register([
self::$_instance,
'getClass'
], true, true
);
}
}