forked from tampe125/dump-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautoloader.php
More file actions
239 lines (218 loc) · 5.17 KB
/
Copy pathautoloader.php
File metadata and controls
239 lines (218 loc) · 5.17 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
<?php
/**
* @package Dumpmon Scraper
* @copyright 2015 Davide Tampellini - FabbricaBinaria
* @license GNU GPL version 3 or later
*/
/**
* A PSR-4 class autoloader. This is a modified version of Composer's ClassLoader class
*
* @codeCoverageIgnore
*/
class Autoloader
{
/** @var array Lengths of PSR-4 prefixes */
private $prefixLengths = array();
/** @var array Prefix to directory map */
private $prefixDirs = array();
/** @var array Fall-back directories */
private $fallbackDirs = array();
/** @var Autoloader The static instance of this autoloader */
private static $instance;
/**
* @return Autoloader
*/
public static function getInstance()
{
if (!is_object(self::$instance))
{
self::$instance = new Autoloader();
}
return self::$instance;
}
/**
* Returns the prefix to directory map
*
* @return array
*/
public function getPrefixes()
{
return $this->prefixDirs;
}
/**
* Returns the list of fall=back directories
*
* @return array
*/
public function getFallbackDirs()
{
return $this->fallbackDirs;
}
/**
* Registers a set of PSR-4 directories for a given namespace, either
* appending or prefixing to the ones previously set for this namespace.
*
* @param string $prefix The prefix/namespace, with trailing '\\'
* @param array|string $paths The PSR-0 base directories
* @param boolean $prepend Whether to prefix the directories
*
* @return $this for chaining
*
* @throws \InvalidArgumentException When the prefix is invalid
*/
public function addMap($prefix, $paths, $prepend = false)
{
if (!$prefix)
{
// Register directories for the root namespace.
if ($prepend)
{
$this->fallbackDirs = array_merge(
(array)$paths,
$this->fallbackDirs
);
}
else
{
$this->fallbackDirs = array_merge(
$this->fallbackDirs,
(array)$paths
);
}
}
elseif (!isset($this->prefixDirs[$prefix]))
{
// Register directories for a new namespace.
$length = strlen($prefix);
if ('\\' !== $prefix[$length - 1])
{
throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
}
$this->prefixLengths[$prefix[0]][$prefix] = $length;
$this->prefixDirs[$prefix] = (array)$paths;
}
elseif ($prepend)
{
// Prepend directories for an already registered namespace.
$this->prefixDirs[$prefix] = array_merge(
(array)$paths,
$this->prefixDirs[$prefix]
);
}
else
{
// Append directories for an already registered namespace.
$this->prefixDirs[$prefix] = array_merge(
$this->prefixDirs[$prefix],
(array)$paths
);
}
return $this;
}
/**
* Registers a set of PSR-4 directories for a given namespace,
* replacing any others previously set for this namespace.
*
* @param string $prefix The prefix/namespace, with trailing '\\'
* @param array|string $paths The PSR-4 base directories
*
* @return void
*
* @throws \InvalidArgumentException When the prefix is invalid
*/
public function setMap($prefix, $paths)
{
if (!$prefix)
{
$this->fallbackDirs = (array)$paths;
}
else
{
$length = strlen($prefix);
if ('\\' !== $prefix[$length - 1])
{
throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
}
$this->prefixLengths[$prefix[0]][$prefix] = $length;
$this->prefixDirs[$prefix] = (array)$paths;
}
}
/**
* Registers this instance as an autoloader.
*
* @param boolean $prepend Whether to prepend the autoloader or not
*
* @return void
*/
public function register($prepend = false)
{
spl_autoload_register(array($this, 'loadClass'), true, $prepend);
}
/**
* Unregisters this instance as an autoloader.
*
* @return void
*/
public function unregister()
{
spl_autoload_unregister(array($this, 'loadClass'));
}
/**
* Loads the given class or interface.
*
* @param string $class The name of the class
*
* @return boolean|null True if loaded, null otherwise
*/
public function loadClass($class)
{
if ($file = $this->findFile($class))
{
include $file;
return true;
}
}
/**
* Finds the path to the file where the class is defined.
*
* @param string $class The name of the class
*
* @return string|false The path if found, false otherwise
*/
public function findFile($class)
{
// work around for PHP 5.3.0 - 5.3.2 https://bugs.php.net/50731
if ('\\' == $class[0])
{
$class = substr($class, 1);
}
// PSR-4 lookup
$logicalPath = strtr($class, '\\', DIRECTORY_SEPARATOR) . '.php';
$first = $class[0];
if (isset($this->prefixLengths[$first]))
{
foreach ($this->prefixLengths[$first] as $prefix => $length)
{
if (0 === strpos($class, $prefix))
{
foreach ($this->prefixDirs[$prefix] as $dir)
{
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPath, $length)))
{
return $file;
}
}
}
}
}
// PSR-4 fallback dirs
foreach ($this->fallbackDirs as $dir)
{
if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPath))
{
return $file;
}
}
}
}
Autoloader::getInstance()->register();