Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 262 lines (242 sloc) 7.577 kB
b895171 @fabpot moved the class loader to its own component
fabpot authored
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
0364560 @fabpot replaced symfony-project.org by symfony.com
fabpot authored
6 * (c) Fabien Potencier <fabien@symfony.com>
b895171 @fabpot moved the class loader to its own component
fabpot authored
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 * UniversalClassLoader implements a "universal" autoloader for PHP 5.3.
16 *
17 * It is able to load classes that use either:
18 *
19 * * The technical interoperability standards for PHP 5.3 namespaces and
20 * class names (http://groups.google.com/group/php-standards/web/psr-0-final-proposal);
21 *
22 * * The PEAR naming convention for classes (http://pear.php.net/).
23 *
24 * Classes from a sub-namespace or a sub-hierarchy of PEAR classes can be
25 * looked for in a list of locations to ease the vendoring of a sub-set of
26 * classes for large projects.
27 *
28 * Example usage:
29 *
30 * $loader = new UniversalClassLoader();
31 *
32 * // register classes with namespaces
33 * $loader->registerNamespaces(array(
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
34 * 'Symfony\Component' => __DIR__.'/component',
35 * 'Symfony' => __DIR__.'/framework',
36 * 'Sensio' => array(__DIR__.'/src', __DIR__.'/vendor'),
b895171 @fabpot moved the class loader to its own component
fabpot authored
37 * ));
38 *
39 * // register a library using the PEAR naming convention
40 * $loader->registerPrefixes(array(
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
41 * 'Swift_' => __DIR__.'/Swift',
b895171 @fabpot moved the class loader to its own component
fabpot authored
42 * ));
43 *
44 * // activate the autoloader
45 * $loader->register();
46 *
47 * In this example, if you try to use a class in the Symfony\Component
48 * namespace or one of its children (Symfony\Component\Console for instance),
49 * the autoloader will first look for the class under the component/
50 * directory, and it will then fallback to the framework/ directory if not
51 * found before giving up.
52 *
0364560 @fabpot replaced symfony-project.org by symfony.com
fabpot authored
53 * @author Fabien Potencier <fabien@symfony.com>
250f88a @fabpot [CssSelector] tagged thlassLoader]d BC API
fabpot authored
54 *
55 * @api
b895171 @fabpot moved the class loader to its own component
fabpot authored
56 */
80862ea @fabpot [ClassLoader] removed ClassLoaderInterface
fabpot authored
57 class UniversalClassLoader
b895171 @fabpot moved the class loader to its own component
fabpot authored
58 {
a1dbe36 @fabpot [ClassLoader] moved most protected methods and properties to private
fabpot authored
59 private $namespaces = array();
60 private $prefixes = array();
61 private $namespaceFallback = array();
62 private $prefixFallback = array();
b895171 @fabpot moved the class loader to its own component
fabpot authored
63
64 /**
65 * Gets the configured namespaces.
66 *
67 * @return array A hash with namespaces as keys and directories as values
68 */
69 public function getNamespaces()
70 {
71 return $this->namespaces;
72 }
73
74 /**
75 * Gets the configured class prefixes.
76 *
77 * @return array A hash with class prefixes as keys and directories as values
78 */
79 public function getPrefixes()
80 {
81 return $this->prefixes;
82 }
83
84 /**
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
85 * Gets the directory(ies) to use as a fallback for namespaces.
b895171 @fabpot moved the class loader to its own component
fabpot authored
86 *
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
87 * @return array An array of directories
b895171 @fabpot moved the class loader to its own component
fabpot authored
88 */
89 public function getNamespaceFallback()
90 {
91 return $this->namespaceFallback;
92 }
93
94 /**
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
95 * Gets the directory(ies) to use as a fallback for class prefixes.
b895171 @fabpot moved the class loader to its own component
fabpot authored
96 *
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
97 * @return array An array of directories
b895171 @fabpot moved the class loader to its own component
fabpot authored
98 */
99 public function getPrefixFallback()
100 {
101 return $this->prefixFallback;
102 }
103
104 /**
105 * Registers the directory to use as a fallback for namespaces.
106 *
7af6c90 @merk Classloader PHPDoc
merk authored
107 * @param string|array $dirs A directory path or an array of directories
250f88a @fabpot [CssSelector] tagged thlassLoader]d BC API
fabpot authored
108 *
109 * @api
b895171 @fabpot moved the class loader to its own component
fabpot authored
110 */
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
111 public function registerNamespaceFallback($dirs)
b895171 @fabpot moved the class loader to its own component
fabpot authored
112 {
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
113 $this->namespaceFallback = (array) $dirs;
b895171 @fabpot moved the class loader to its own component
fabpot authored
114 }
115
116 /**
117 * Registers the directory to use as a fallback for class prefixes.
118 *
7af6c90 @merk Classloader PHPDoc
merk authored
119 * @param string|array $dirs A directory path or an array of directories
250f88a @fabpot [CssSelector] tagged thlassLoader]d BC API
fabpot authored
120 *
121 * @api
b895171 @fabpot moved the class loader to its own component
fabpot authored
122 */
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
123 public function registerPrefixFallback($dirs)
b895171 @fabpot moved the class loader to its own component
fabpot authored
124 {
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
125 $this->prefixFallback = (array) $dirs;
b895171 @fabpot moved the class loader to its own component
fabpot authored
126 }
127
128 /**
129 * Registers an array of namespaces
130 *
131 * @param array $namespaces An array of namespaces (namespaces as keys and locations as values)
250f88a @fabpot [CssSelector] tagged thlassLoader]d BC API
fabpot authored
132 *
133 * @api
b895171 @fabpot moved the class loader to its own component
fabpot authored
134 */
135 public function registerNamespaces(array $namespaces)
136 {
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
137 foreach ($namespaces as $namespace => $locations) {
138 $this->namespaces[$namespace] = (array) $locations;
139 }
b895171 @fabpot moved the class loader to its own component
fabpot authored
140 }
141
142 /**
143 * Registers a namespace.
144 *
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
145 * @param string $namespace The namespace
146 * @param array|string $paths The location(s) of the namespace
250f88a @fabpot [CssSelector] tagged thlassLoader]d BC API
fabpot authored
147 *
148 * @api
b895171 @fabpot moved the class loader to its own component
fabpot authored
149 */
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
150 public function registerNamespace($namespace, $paths)
b895171 @fabpot moved the class loader to its own component
fabpot authored
151 {
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
152 $this->namespaces[$namespace] = (array) $paths;
b895171 @fabpot moved the class loader to its own component
fabpot authored
153 }
154
155 /**
156 * Registers an array of classes using the PEAR naming convention.
157 *
158 * @param array $classes An array of classes (prefixes as keys and locations as values)
250f88a @fabpot [CssSelector] tagged thlassLoader]d BC API
fabpot authored
159 *
160 * @api
b895171 @fabpot moved the class loader to its own component
fabpot authored
161 */
162 public function registerPrefixes(array $classes)
163 {
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
164 foreach ($classes as $prefix => $locations) {
165 $this->prefixes[$prefix] = (array) $locations;
166 }
b895171 @fabpot moved the class loader to its own component
fabpot authored
167 }
168
169 /**
170 * Registers a set of classes using the PEAR naming convention.
171 *
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
172 * @param string $prefix The classes prefix
173 * @param array|string $paths The location(s) of the classes
250f88a @fabpot [CssSelector] tagged thlassLoader]d BC API
fabpot authored
174 *
175 * @api
b895171 @fabpot moved the class loader to its own component
fabpot authored
176 */
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
177 public function registerPrefix($prefix, $paths)
b895171 @fabpot moved the class loader to its own component
fabpot authored
178 {
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
179 $this->prefixes[$prefix] = (array) $paths;
b895171 @fabpot moved the class loader to its own component
fabpot authored
180 }
181
182 /**
183 * Registers this instance as an autoloader.
184 *
185 * @param Boolean $prepend Whether to prepend the autoloader or not
250f88a @fabpot [CssSelector] tagged thlassLoader]d BC API
fabpot authored
186 *
187 * @api
b895171 @fabpot moved the class loader to its own component
fabpot authored
188 */
189 public function register($prepend = false)
190 {
191 spl_autoload_register(array($this, 'loadClass'), true, $prepend);
192 }
193
194 /**
195 * Loads the given class or interface.
196 *
197 * @param string $class The name of the class
198 */
199 public function loadClass($class)
200 {
608bce3 @kriswallsmith [ClassLoader] created protected findFile() method to allow creating a…
kriswallsmith authored
201 if ($file = $this->findFile($class)) {
202 require $file;
203 }
204 }
205
206 /**
207 * Finds the path to the file where the class is defined.
208 *
209 * @param string $class The name of the class
210 *
211 * @return string|null The path, if found
212 */
0091d0e @fabpot [ClassLoader] added ClassLoaderInterface
fabpot authored
213 public function findFile($class)
608bce3 @kriswallsmith [ClassLoader] created protected findFile() method to allow creating a…
kriswallsmith authored
214 {
7567a77 @kriswallsmith [ClassLoader] added a check before trimming the leading \
kriswallsmith authored
215 if ('\\' == $class[0]) {
216 $class = substr($class, 1);
217 }
b895171 @fabpot moved the class loader to its own component
fabpot authored
218
608bce3 @kriswallsmith [ClassLoader] created protected findFile() method to allow creating a…
kriswallsmith authored
219 if (false !== $pos = strrpos($class, '\\')) {
b895171 @fabpot moved the class loader to its own component
fabpot authored
220 // namespaced class name
221 $namespace = substr($class, 0, $pos);
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
222 foreach ($this->namespaces as $ns => $dirs) {
223 foreach ($dirs as $dir) {
224 if (0 === strpos($namespace, $ns)) {
225 $className = substr($class, $pos + 1);
226 $file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $className).'.php';
227 if (file_exists($file)) {
608bce3 @kriswallsmith [ClassLoader] created protected findFile() method to allow creating a…
kriswallsmith authored
228 return $file;
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
229 }
b895171 @fabpot moved the class loader to its own component
fabpot authored
230 }
231 }
232 }
233
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
234 foreach ($this->namespaceFallback as $dir) {
235 $file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $class).'.php';
b895171 @fabpot moved the class loader to its own component
fabpot authored
236 if (file_exists($file)) {
608bce3 @kriswallsmith [ClassLoader] created protected findFile() method to allow creating a…
kriswallsmith authored
237 return $file;
b895171 @fabpot moved the class loader to its own component
fabpot authored
238 }
239 }
240 } else {
241 // PEAR-like class name
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
242 foreach ($this->prefixes as $prefix => $dirs) {
243 foreach ($dirs as $dir) {
244 if (0 === strpos($class, $prefix)) {
245 $file = $dir.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
246 if (file_exists($file)) {
608bce3 @kriswallsmith [ClassLoader] created protected findFile() method to allow creating a…
kriswallsmith authored
247 return $file;
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
248 }
b895171 @fabpot moved the class loader to its own component
fabpot authored
249 }
250 }
251 }
252
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
253 foreach ($this->prefixFallback as $dir) {
254 $file = $dir.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
b895171 @fabpot moved the class loader to its own component
fabpot authored
255 if (file_exists($file)) {
608bce3 @kriswallsmith [ClassLoader] created protected findFile() method to allow creating a…
kriswallsmith authored
256 return $file;
b895171 @fabpot moved the class loader to its own component
fabpot authored
257 }
258 }
259 }
260 }
261 }
Something went wrong with that request. Please try again.