comparison vendor/symfony/class-loader/Psr4ClassLoader.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\ClassLoader;
13
14 /**
15 * A PSR-4 compatible class loader.
16 *
17 * See http://www.php-fig.org/psr/psr-4/
18 *
19 * @author Alexander M. Turek <me@derrabus.de>
20 */
21 class Psr4ClassLoader
22 {
23 /**
24 * @var array
25 */
26 private $prefixes = array();
27
28 /**
29 * @param string $prefix
30 * @param string $baseDir
31 */
32 public function addPrefix($prefix, $baseDir)
33 {
34 $prefix = trim($prefix, '\\').'\\';
35 $baseDir = rtrim($baseDir, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
36 $this->prefixes[] = array($prefix, $baseDir);
37 }
38
39 /**
40 * @param string $class
41 *
42 * @return string|null
43 */
44 public function findFile($class)
45 {
46 $class = ltrim($class, '\\');
47
48 foreach ($this->prefixes as list($currentPrefix, $currentBaseDir)) {
49 if (0 === strpos($class, $currentPrefix)) {
50 $classWithoutPrefix = substr($class, strlen($currentPrefix));
51 $file = $currentBaseDir.str_replace('\\', DIRECTORY_SEPARATOR, $classWithoutPrefix).'.php';
52 if (file_exists($file)) {
53 return $file;
54 }
55 }
56 }
57 }
58
59 /**
60 * @param string $class
61 *
62 * @return bool
63 */
64 public function loadClass($class)
65 {
66 $file = $this->findFile($class);
67 if (null !== $file) {
68 require $file;
69
70 return true;
71 }
72
73 return false;
74 }
75
76 /**
77 * Registers this instance as an autoloader.
78 *
79 * @param bool $prepend
80 */
81 public function register($prepend = false)
82 {
83 spl_autoload_register(array($this, 'loadClass'), true, $prepend);
84 }
85
86 /**
87 * Removes this instance from the registered autoloaders.
88 */
89 public function unregister()
90 {
91 spl_autoload_unregister(array($this, 'loadClass'));
92 }
93 }