Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/class-loader/ClassLoader.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 * ClassLoader implements an PSR-0 class loader. | |
16 * | |
17 * See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md | |
18 * | |
19 * $loader = new ClassLoader(); | |
20 * | |
21 * // register classes with namespaces | |
22 * $loader->addPrefix('Symfony\Component', __DIR__.'/component'); | |
23 * $loader->addPrefix('Symfony', __DIR__.'/framework'); | |
24 * | |
25 * // activate the autoloader | |
26 * $loader->register(); | |
27 * | |
28 * // to enable searching the include path (e.g. for PEAR packages) | |
29 * $loader->setUseIncludePath(true); | |
30 * | |
31 * In this example, if you try to use a class in the Symfony\Component | |
32 * namespace or one of its children (Symfony\Component\Console for instance), | |
33 * the autoloader will first look for the class under the component/ | |
34 * directory, and it will then fallback to the framework/ directory if not | |
35 * found before giving up. | |
36 * | |
37 * @author Fabien Potencier <fabien@symfony.com> | |
38 * @author Jordi Boggiano <j.boggiano@seld.be> | |
39 */ | |
40 class ClassLoader | |
41 { | |
42 private $prefixes = array(); | |
43 private $fallbackDirs = array(); | |
44 private $useIncludePath = false; | |
45 | |
46 /** | |
47 * Returns prefixes. | |
48 * | |
49 * @return array | |
50 */ | |
51 public function getPrefixes() | |
52 { | |
53 return $this->prefixes; | |
54 } | |
55 | |
56 /** | |
57 * Returns fallback directories. | |
58 * | |
59 * @return array | |
60 */ | |
61 public function getFallbackDirs() | |
62 { | |
63 return $this->fallbackDirs; | |
64 } | |
65 | |
66 /** | |
67 * Adds prefixes. | |
68 * | |
69 * @param array $prefixes Prefixes to add | |
70 */ | |
71 public function addPrefixes(array $prefixes) | |
72 { | |
73 foreach ($prefixes as $prefix => $path) { | |
74 $this->addPrefix($prefix, $path); | |
75 } | |
76 } | |
77 | |
78 /** | |
79 * Registers a set of classes. | |
80 * | |
81 * @param string $prefix The classes prefix | |
82 * @param array|string $paths The location(s) of the classes | |
83 */ | |
84 public function addPrefix($prefix, $paths) | |
85 { | |
86 if (!$prefix) { | |
87 foreach ((array) $paths as $path) { | |
88 $this->fallbackDirs[] = $path; | |
89 } | |
90 | |
91 return; | |
92 } | |
93 if (isset($this->prefixes[$prefix])) { | |
94 if (is_array($paths)) { | |
95 $this->prefixes[$prefix] = array_unique(array_merge( | |
96 $this->prefixes[$prefix], | |
97 $paths | |
98 )); | |
99 } elseif (!in_array($paths, $this->prefixes[$prefix])) { | |
100 $this->prefixes[$prefix][] = $paths; | |
101 } | |
102 } else { | |
103 $this->prefixes[$prefix] = array_unique((array) $paths); | |
104 } | |
105 } | |
106 | |
107 /** | |
108 * Turns on searching the include for class files. | |
109 * | |
110 * @param bool $useIncludePath | |
111 */ | |
112 public function setUseIncludePath($useIncludePath) | |
113 { | |
114 $this->useIncludePath = (bool) $useIncludePath; | |
115 } | |
116 | |
117 /** | |
118 * Can be used to check if the autoloader uses the include path to check | |
119 * for classes. | |
120 * | |
121 * @return bool | |
122 */ | |
123 public function getUseIncludePath() | |
124 { | |
125 return $this->useIncludePath; | |
126 } | |
127 | |
128 /** | |
129 * Registers this instance as an autoloader. | |
130 * | |
131 * @param bool $prepend Whether to prepend the autoloader or not | |
132 */ | |
133 public function register($prepend = false) | |
134 { | |
135 spl_autoload_register(array($this, 'loadClass'), true, $prepend); | |
136 } | |
137 | |
138 /** | |
139 * Unregisters this instance as an autoloader. | |
140 */ | |
141 public function unregister() | |
142 { | |
143 spl_autoload_unregister(array($this, 'loadClass')); | |
144 } | |
145 | |
146 /** | |
147 * Loads the given class or interface. | |
148 * | |
149 * @param string $class The name of the class | |
150 * | |
151 * @return bool|null True, if loaded | |
152 */ | |
153 public function loadClass($class) | |
154 { | |
155 if ($file = $this->findFile($class)) { | |
156 require $file; | |
157 | |
158 return true; | |
159 } | |
160 } | |
161 | |
162 /** | |
163 * Finds the path to the file where the class is defined. | |
164 * | |
165 * @param string $class The name of the class | |
166 * | |
167 * @return string|null The path, if found | |
168 */ | |
169 public function findFile($class) | |
170 { | |
171 if (false !== $pos = strrpos($class, '\\')) { | |
172 // namespaced class name | |
173 $classPath = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 0, $pos)).DIRECTORY_SEPARATOR; | |
174 $className = substr($class, $pos + 1); | |
175 } else { | |
176 // PEAR-like class name | |
177 $classPath = null; | |
178 $className = $class; | |
179 } | |
180 | |
181 $classPath .= str_replace('_', DIRECTORY_SEPARATOR, $className).'.php'; | |
182 | |
183 foreach ($this->prefixes as $prefix => $dirs) { | |
184 if ($class === strstr($class, $prefix)) { | |
185 foreach ($dirs as $dir) { | |
186 if (file_exists($dir.DIRECTORY_SEPARATOR.$classPath)) { | |
187 return $dir.DIRECTORY_SEPARATOR.$classPath; | |
188 } | |
189 } | |
190 } | |
191 } | |
192 | |
193 foreach ($this->fallbackDirs as $dir) { | |
194 if (file_exists($dir.DIRECTORY_SEPARATOR.$classPath)) { | |
195 return $dir.DIRECTORY_SEPARATOR.$classPath; | |
196 } | |
197 } | |
198 | |
199 if ($this->useIncludePath && $file = stream_resolve_include_path($classPath)) { | |
200 return $file; | |
201 } | |
202 } | |
203 } |