PluginProbe
Social Share Buttons / 1.11
Social Share Buttons v1.11
trunk 0.9 1.0 1.1 1.1.1 1.1.2 1.10 1.11 1.12 1.15 1.16 1.17 1.18 1.19 1.2 1.20 1.3 1.30 1.4 1.5 1.6 1.7 1.8 1.9
share-button / libraries / autoload / ClassLoader.php

ClassLoader.php in Social Share Buttons 1.11, at libraries/autoload/ClassLoader.php

450 lines 13.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * This file is part of Composer.
5 *
6 * (c) Nils Adermann <naderman@naderman.de>
7 * Jordi Boggiano <j.boggiano@seld.be>
8 *
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
11 */
12
13 namespace MBSocial;
14
15 /**
16 * ClassLoader implements a PSR-0, PSR-4 and classmap class loader.
17 *
18 * $loader = new \Composer\Autoload\ClassLoader();
19 *
20 * // register classes with namespaces
21 * $loader->add('Symfony\Component', __DIR__.'/component');
22 * $loader->add('Symfony', __DIR__.'/framework');
23 *
24 * // activate the autoloader
25 * $loader->register();
26 *
27 * // to enable searching the include path (eg. for PEAR packages)
28 * $loader->setUseIncludePath(true);
29 *
30 * In this example, if you try to use a class in the Symfony\Component
31 * namespace or one of its children (Symfony\Component\Console for instance),
32 * the autoloader will first look for the class under the component/
33 * directory, and it will then fallback to the framework/ directory if not
34 * found before giving up.
35 *
36 * This class is loosely based on the Symfony UniversalClassLoader.
37 *
38 * @author Fabien Potencier <fabien@symfony.com>
39 * @author Jordi Boggiano <j.boggiano@seld.be>
40 * @see http://www.php-fig.org/psr/psr-0/
41 * @see http://www.php-fig.org/psr/psr-4/
42 */
43 class ClassLoader
44 {
45 // PSR-4
46 private $prefixLengthsPsr4 = array();
47 private $prefixDirsPsr4 = array();
48 private $fallbackDirsPsr4 = array();
49
50 // PSR-0
51 private $prefixesPsr0 = array();
52 private $fallbackDirsPsr0 = array();
53
54 private $useIncludePath = false;
55 private $classMap = array();
56 private $classMapAuthoritative = false;
57 private $missingClasses = array();
58 private $apcuPrefix;
59
60 public function getPrefixes()
61 {
62 if (!empty($this->prefixesPsr0)) {
63 return call_user_func_array('array_merge', $this->prefixesPsr0);
64 }
65
66 return array();
67 }
68
69 public function getPrefixesPsr4()
70 {
71 return $this->prefixDirsPsr4;
72 }
73
74 public function getFallbackDirs()
75 {
76 return $this->fallbackDirsPsr0;
77 }
78
79 public function getFallbackDirsPsr4()
80 {
81 return $this->fallbackDirsPsr4;
82 }
83
84 public function getClassMap()
85 {
86 return $this->classMap;
87 }
88
89 /**
90 * @param array $classMap Class to filename map
91 */
92 public function addClassMap(array $classMap)
93 {
94 if ($this->classMap) {
95 $this->classMap = array_merge($this->classMap, $classMap);
96 } else {
97 $this->classMap = $classMap;
98 }
99 }
100
101 /**
102 * Registers a set of PSR-0 directories for a given prefix, either
103 * appending or prepending to the ones previously set for this prefix.
104 *
105 * @param string $prefix The prefix
106 * @param array|string $paths The PSR-0 root directories
107 * @param bool $prepend Whether to prepend the directories
108 */
109 public function add($prefix, $paths, $prepend = false)
110 {
111
112 if (!$prefix) {
113 if ($prepend) {
114 $this->fallbackDirsPsr0 = array_merge(
115 (array) $paths,
116 $this->fallbackDirsPsr0
117 );
118 } else {
119 $this->fallbackDirsPsr0 = array_merge(
120 $this->fallbackDirsPsr0,
121 (array) $paths
122 );
123 }
124
125 return;
126 }
127
128 $first = $prefix[0];
129 if (!isset($this->prefixesPsr0[$first][$prefix])) {
130 $this->prefixesPsr0[$first][$prefix] = (array) $paths;
131
132 return;
133 }
134 if ($prepend) {
135 $this->prefixesPsr0[$first][$prefix] = array_merge(
136 (array) $paths,
137 $this->prefixesPsr0[$first][$prefix]
138 );
139 } else {
140 $this->prefixesPsr0[$first][$prefix] = array_merge(
141 $this->prefixesPsr0[$first][$prefix],
142 (array) $paths
143 );
144 }
145 }
146
147 /**
148 * Registers a set of PSR-4 directories for a given namespace, either
149 * appending or prepending to the ones previously set for this namespace.
150 *
151 * @param string $prefix The prefix/namespace, with trailing '\\'
152 * @param array|string $paths The PSR-4 base directories
153 * @param bool $prepend Whether to prepend the directories
154 *
155 * @throws \InvalidArgumentException
156 */
157 public function addPsr4($prefix, $paths, $prepend = false)
158 {
159 if (!$prefix) {
160 // Register directories for the root namespace.
161 if ($prepend) {
162 $this->fallbackDirsPsr4 = array_merge(
163 (array) $paths,
164 $this->fallbackDirsPsr4
165 );
166 } else {
167 $this->fallbackDirsPsr4 = array_merge(
168 $this->fallbackDirsPsr4,
169 (array) $paths
170 );
171 }
172 } elseif (!isset($this->prefixDirsPsr4[$prefix])) {
173 // Register directories for a new namespace.
174 $length = strlen($prefix);
175 if ('\\' !== $prefix[$length - 1]) {
176 throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
177 }
178 $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
179 $this->prefixDirsPsr4[$prefix] = (array) $paths;
180 } elseif ($prepend) {
181 // Prepend directories for an already registered namespace.
182 $this->prefixDirsPsr4[$prefix] = array_merge(
183 (array) $paths,
184 $this->prefixDirsPsr4[$prefix]
185 );
186 } else {
187 // Append directories for an already registered namespace.
188 $this->prefixDirsPsr4[$prefix] = array_merge(
189 $this->prefixDirsPsr4[$prefix],
190 (array) $paths
191 );
192 }
193 }
194
195 /**
196 * Registers a set of PSR-0 directories for a given prefix,
197 * replacing any others previously set for this prefix.
198 *
199 * @param string $prefix The prefix
200 * @param array|string $paths The PSR-0 base directories
201 */
202 public function set($prefix, $paths)
203 {
204 if (!$prefix) {
205 $this->fallbackDirsPsr0 = (array) $paths;
206 } else {
207 $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths;
208 }
209 }
210
211 /**
212 * Registers a set of PSR-4 directories for a given namespace,
213 * replacing any others previously set for this namespace.
214 *
215 * @param string $prefix The prefix/namespace, with trailing '\\'
216 * @param array|string $paths The PSR-4 base directories
217 *
218 * @throws \InvalidArgumentException
219 */
220 public function setPsr4($prefix, $paths)
221 {
222 if (!$prefix) {
223 $this->fallbackDirsPsr4 = (array) $paths;
224 } else {
225 $length = strlen($prefix);
226 if ('\\' !== $prefix[$length - 1]) {
227 throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator.");
228 }
229 $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length;
230 $this->prefixDirsPsr4[$prefix] = (array) $paths;
231 }
232 }
233
234 /**
235 * Turns on searching the include path for class files.
236 *
237 * @param bool $useIncludePath
238 */
239 public function setUseIncludePath($useIncludePath)
240 {
241 $this->useIncludePath = $useIncludePath;
242 }
243
244 /**
245 * Can be used to check if the autoloader uses the include path to check
246 * for classes.
247 *
248 * @return bool
249 */
250 public function getUseIncludePath()
251 {
252 return $this->useIncludePath;
253 }
254
255 /**
256 * Turns off searching the prefix and fallback directories for classes
257 * that have not been registered with the class map.
258 *
259 * @param bool $classMapAuthoritative
260 */
261 public function setClassMapAuthoritative($classMapAuthoritative)
262 {
263 $this->classMapAuthoritative = $classMapAuthoritative;
264 }
265
266 /**
267 * Should class lookup fail if not found in the current class map?
268 *
269 * @return bool
270 */
271 public function isClassMapAuthoritative()
272 {
273 return $this->classMapAuthoritative;
274 }
275
276 /**
277 * APCu prefix to use to cache found/not-found classes, if the extension is enabled.
278 *
279 * @param string|null $apcuPrefix
280 */
281 public function setApcuPrefix($apcuPrefix)
282 {
283 $this->apcuPrefix = function_exists('apcu_fetch') && ini_get('apc.enabled') ? $apcuPrefix : null;
284 }
285
286 /**
287 * The APCu prefix in use, or null if APCu caching is not enabled.
288 *
289 * @return string|null
290 */
291 public function getApcuPrefix()
292 {
293 return $this->apcuPrefix;
294 }
295
296 /**
297 * Registers this instance as an autoloader.
298 *
299 * @param bool $prepend Whether to prepend the autoloader or not
300 */
301 public function register($prepend = false)
302 {
303 spl_autoload_register(array($this, 'loadClass'), true, $prepend);
304 }
305
306 /**
307 * Unregisters this instance as an autoloader.
308 */
309 public function unregister()
310 {
311 spl_autoload_unregister(array($this, 'loadClass'));
312 }
313
314 /**
315 * Loads the given class or interface.
316 *
317 * @param string $class The name of the class
318 * @return bool|null True if loaded, null otherwise
319 */
320 public function loadClass($class)
321 {
322 if ($file = $this->findFile($class)) {
323 includeFile($file);
324
325 return true;
326 }
327 }
328
329 /**
330 * Finds the path to the file where the class is defined.
331 *
332 * @param string $class The name of the class
333 *
334 * @return string|false The path if found, false otherwise
335 */
336 public function findFile($class)
337 {
338 // class map lookup
339 if (isset($this->classMap[$class])) {
340 return $this->classMap[$class];
341 }
342 if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) {
343 return false;
344 }
345 if (null !== $this->apcuPrefix) {
346 $file = apcu_fetch($this->apcuPrefix.$class, $hit);
347 if ($hit) {
348 return $file;
349 }
350 }
351
352 $file = $this->findFileWithExtension($class, '.php');
353
354 // Search for Hack files if we are running on HHVM
355 if (false === $file && defined('HHVM_VERSION')) {
356 $file = $this->findFileWithExtension($class, '.hh');
357 }
358
359 if (null !== $this->apcuPrefix) {
360 apcu_add($this->apcuPrefix.$class, $file);
361 }
362
363 if (false === $file) {
364 // Remember that this class does not exist.
365 $this->missingClasses[$class] = true;
366 }
367
368 return $file;
369 }
370
371 private function findFileWithExtension($class, $ext)
372 {
373 // PSR-4 lookup
374 $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext;
375
376
377 $first = $class[0];
378 if (isset($this->prefixLengthsPsr4[$first])) {
379
380 $subPath = $class;
381 while (false !== $lastPos = strrpos($subPath, '\\')) {
382 $subPath = substr($subPath, 0, $lastPos);
383 $search = $subPath.'\\';
384 if (isset($this->prefixDirsPsr4[$search])) {
385 foreach ($this->prefixDirsPsr4[$search] as $dir) {
386 $length = $this->prefixLengthsPsr4[$first][$search];
387
388 if (file_exists($file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) {
389 return $file;
390 }
391 }
392 }
393 }
394 }
395
396 // PSR-4 fallback dirs
397 foreach ($this->fallbackDirsPsr4 as $dir) {
398 if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) {
399 return $file;
400 }
401 }
402
403 // PSR-0 lookup
404 if (false !== $pos = strrpos($class, '\\')) {
405 // namespaced class name
406 $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1)
407 . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR);
408 } else {
409 // PEAR-like class name
410 $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext;
411 }
412
413 if (isset($this->prefixesPsr0[$first])) {
414 foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) {
415 if (0 === strpos($class, $prefix)) {
416 foreach ($dirs as $dir) {
417 if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
418 return $file;
419 }
420 }
421 }
422 }
423 }
424
425 // PSR-0 fallback dirs
426 foreach ($this->fallbackDirsPsr0 as $dir) {
427 if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) {
428 return $file;
429 }
430 }
431
432 // PSR-0 include paths.
433 if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) {
434 return $file;
435 }
436
437 return false;
438 }
439 }
440
441 /**
442 * Scope isolated include.
443 *
444 * Prevents access to $this/self from included files.
445 */
446 function includeFile($file)
447 {
448 include $file;
449 }
450