-
Notifications
You must be signed in to change notification settings - Fork 0
/
Loader.php
227 lines (201 loc) · 7.6 KB
/
Loader.php
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
<?php
/* !
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* PACKAGE : PHP POWERTOOLS
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* COMPONENT : AUTOLOADER
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* DESCRIPTION :
*
* Zero configuration autoloader for Symfony components,
* Zend components, and other PSR-0 compatible libraries
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* REQUIREMENTS :
*
* PHP version 5.3+
* PSR-0 compatibility
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* EXAMPLE :
*
* Here's a short example of how to use this library:
* <code>
* <?php
* define("BASE_PATH", dirname(__FILE__));
* define("LIBRARY_PATH", BASE_PATH . DIRECTORY_SEPARATOR . 'vendor');
* define("USER_PATH", BASE_PATH . DIRECTORY_SEPARATOR . 'user123');
*
* require LIBRARY_PATH . DIRECTORY_SEPARATOR . 'Loader.php';
* Loader::init(array(LIBRARY_PATH, USER_PATH));
*
* ?>
* </code>
*
* If you include the code hereabove, the paths /vendor and /user123
* will be added to the include path.
*
* Any PSR-0 compliant classes will now automatically be loaded from /user123.
*
* If it can't be found there, the autoloader will attempt to locate it at the
* /vendor path
*
* It requires no additional configuration and does not require composer.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* LICENSE :
*
* LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* @category Autoloader
* @package /
* @author John Slegers
* @copyright MMXIV John Slegers
* @license http:https://www.opensource.org/licenses/mit-license.html MIT License
* @link https://github.com/jslegers
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
defined('PRODUCTION') or define('PRODUCTION', false);
class Loader {
protected static $_static_data = array();
public static function init($prependPaths = array()) {
static::setErrorReporting(!PRODUCTION);
static::_synchPaths(true);
static::appendLoader(get_called_class(), 'load');
static::prependPaths($prependPaths);
}
public static function setErrorReporting($enable = true) {
if ($enable) {
error_reporting(E_ALL ^ E_NOTICE);
ini_set('display_errors', 1);
} else {
error_reporting(0);
ini_set('display_errors', 0);
}
}
public static function load($className) {
// var_dump($className);
$className = ltrim($className, '\\');
$fileName = '';
$namespace = '';
if ($lastNsPos = strrpos($className, '\\')) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
foreach ($paths = static::getPaths() as $path) {
$fullpath = $path . DIRECTORY_SEPARATOR . $fileName;
// var_dump($fullpath);
if (@is_readable($fullpath)) {
require $fullpath;
break;
}
}
}
public static function appendLoader($class, $method) {
static::_registerLoader($class, $method, false);
}
public static function prependLoader($class, $method) {
static::_registerLoader($class, $method, true);
}
protected static function _registerLoader($class, $method, $prepend = false) {
spl_autoload_register(array($class, $method), true, $prepend);
}
public static function removeLoader($class, $method) {
spl_autoload_unregister(array($class, $method));
}
public static function getPaths() {
return static::$_static_data['includepaths'];
}
public static function setPaths(array $paths) {
static::$_static_data['includepaths'] = $paths;
static::_synchPaths();
}
public static function prependPath($path) {
static::_prepend($path);
}
public static function prependPaths(array $paths) {
static::_repeat('_prepend', $paths);
}
protected static function _prepend($path, $synch = true) {
static::removePath($path, false);
array_unshift(static::$_static_data['includepaths'], $path);
if ($synch)
static::_synchPaths();
}
public static function appendPath($path) {
static::_append($path);
}
public static function appendPaths(array $paths) {
static::_repeat('_append', $paths);
}
protected static function _append($path, $synch = true) {
static::removePath($path, false);
static::$_static_data['includepaths'][] = $path;
if ($synch)
static::_synchPaths();
}
public static function removePath($path) {
static::_remove($path);
}
public static function removePaths(array $paths) {
static::_repeat('_remove', $paths);
}
protected static function _remove($path, $synch = true) {
if (($key = array_search($path, static::$_static_data['includepaths'])) !== false) {
unset(static::$_static_data['includepaths'][$key]);
if ($synch)
static::_synchPaths();
}
}
protected static function _repeat($method, array $paths) {
foreach ($paths as $path) {
static::$method($path, false);
}
static::_synchPaths();
}
protected static function _synchPaths($reverse = false) {
if ($reverse) {
static::$_static_data['includepaths'] = explode(PATH_SEPARATOR, get_include_path());
} else {
set_include_path(implode(PATH_SEPARATOR, static::$_static_data['includepaths']));
}
}
}
?>