Skip to content

HTTPS clone URL

Subversion checkout URL

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