Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 246 lines (226 sloc) 7.436 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>
b895171 @fabpot moved the class loader to its own component
fabpot authored
54 */
55 class UniversalClassLoader
56 {
a1dbe36 @fabpot [ClassLoader] moved most protected methods and properties to private
fabpot authored
57 private $namespaces = array();
58 private $prefixes = array();
59 private $namespaceFallback = array();
60 private $prefixFallback = array();
b895171 @fabpot moved the class loader to its own component
fabpot authored
61
62 /**
63 * Gets the configured namespaces.
64 *
65 * @return array A hash with namespaces as keys and directories as values
66 */
67 public function getNamespaces()
68 {
69 return $this->namespaces;
70 }
71
72 /**
73 * Gets the configured class prefixes.
74 *
75 * @return array A hash with class prefixes as keys and directories as values
76 */
77 public function getPrefixes()
78 {
79 return $this->prefixes;
80 }
81
82 /**
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
83 * Gets the directory(ies) to use as a fallback for namespaces.
b895171 @fabpot moved the class loader to its own component
fabpot authored
84 *
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
85 * @return array An array of directories
b895171 @fabpot moved the class loader to its own component
fabpot authored
86 */
87 public function getNamespaceFallback()
88 {
89 return $this->namespaceFallback;
90 }
91
92 /**
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
93 * Gets the directory(ies) to use as a fallback for class prefixes.
b895171 @fabpot moved the class loader to its own component
fabpot authored
94 *
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
95 * @return array An array of directories
b895171 @fabpot moved the class loader to its own component
fabpot authored
96 */
97 public function getPrefixFallback()
98 {
99 return $this->prefixFallback;
100 }
101
102 /**
103 * Registers the directory to use as a fallback for namespaces.
104 *
7af6c90 @merk Classloader PHPDoc
merk authored
105 * @param string|array $dirs A directory path or an array of directories
b895171 @fabpot moved the class loader to its own component
fabpot authored
106 */
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
107 public function registerNamespaceFallback($dirs)
b895171 @fabpot moved the class loader to its own component
fabpot authored
108 {
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
109 $this->namespaceFallback = (array) $dirs;
b895171 @fabpot moved the class loader to its own component
fabpot authored
110 }
111
112 /**
113 * Registers the directory to use as a fallback for class prefixes.
114 *
7af6c90 @merk Classloader PHPDoc
merk authored
115 * @param string|array $dirs A directory path or an array of directories
b895171 @fabpot moved the class loader to its own component
fabpot authored
116 */
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
117 public function registerPrefixFallback($dirs)
b895171 @fabpot moved the class loader to its own component
fabpot authored
118 {
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
119 $this->prefixFallback = (array) $dirs;
b895171 @fabpot moved the class loader to its own component
fabpot authored
120 }
121
122 /**
123 * Registers an array of namespaces
124 *
125 * @param array $namespaces An array of namespaces (namespaces as keys and locations as values)
126 */
127 public function registerNamespaces(array $namespaces)
128 {
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
129 foreach ($namespaces as $namespace => $locations) {
130 $this->namespaces[$namespace] = (array) $locations;
131 }
b895171 @fabpot moved the class loader to its own component
fabpot authored
132 }
133
134 /**
135 * Registers a namespace.
136 *
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
137 * @param string $namespace The namespace
138 * @param array|string $paths The location(s) of the namespace
b895171 @fabpot moved the class loader to its own component
fabpot authored
139 */
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
140 public function registerNamespace($namespace, $paths)
b895171 @fabpot moved the class loader to its own component
fabpot authored
141 {
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
142 $this->namespaces[$namespace] = (array) $paths;
b895171 @fabpot moved the class loader to its own component
fabpot authored
143 }
144
145 /**
146 * Registers an array of classes using the PEAR naming convention.
147 *
148 * @param array $classes An array of classes (prefixes as keys and locations as values)
149 */
150 public function registerPrefixes(array $classes)
151 {
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
152 foreach ($classes as $prefix => $locations) {
153 $this->prefixes[$prefix] = (array) $locations;
154 }
b895171 @fabpot moved the class loader to its own component
fabpot authored
155 }
156
157 /**
158 * Registers a set of classes using the PEAR naming convention.
159 *
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
160 * @param string $prefix The classes prefix
161 * @param array|string $paths The location(s) of the classes
b895171 @fabpot moved the class loader to its own component
fabpot authored
162 */
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
163 public function registerPrefix($prefix, $paths)
b895171 @fabpot moved the class loader to its own component
fabpot authored
164 {
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
165 $this->prefixes[$prefix] = (array) $paths;
b895171 @fabpot moved the class loader to its own component
fabpot authored
166 }
167
168 /**
169 * Registers this instance as an autoloader.
170 *
171 * @param Boolean $prepend Whether to prepend the autoloader or not
172 */
173 public function register($prepend = false)
174 {
175 spl_autoload_register(array($this, 'loadClass'), true, $prepend);
176 }
177
178 /**
179 * Loads the given class or interface.
180 *
181 * @param string $class The name of the class
182 */
183 public function loadClass($class)
184 {
608bce3 @kriswallsmith [ClassLoader] created protected findFile() method to allow creating a…
kriswallsmith authored
185 if ($file = $this->findFile($class)) {
186 require $file;
187 }
188 }
189
190 /**
191 * Finds the path to the file where the class is defined.
192 *
193 * @param string $class The name of the class
194 *
195 * @return string|null The path, if found
196 */
197 protected function findFile($class)
198 {
7567a77 @kriswallsmith [ClassLoader] added a check before trimming the leading \
kriswallsmith authored
199 if ('\\' == $class[0]) {
200 $class = substr($class, 1);
201 }
b895171 @fabpot moved the class loader to its own component
fabpot authored
202
608bce3 @kriswallsmith [ClassLoader] created protected findFile() method to allow creating a…
kriswallsmith authored
203 if (false !== $pos = strrpos($class, '\\')) {
b895171 @fabpot moved the class loader to its own component
fabpot authored
204 // namespaced class name
205 $namespace = substr($class, 0, $pos);
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
206 foreach ($this->namespaces as $ns => $dirs) {
207 foreach ($dirs as $dir) {
208 if (0 === strpos($namespace, $ns)) {
209 $className = substr($class, $pos + 1);
210 $file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $className).'.php';
211 if (file_exists($file)) {
608bce3 @kriswallsmith [ClassLoader] created protected findFile() method to allow creating a…
kriswallsmith authored
212 return $file;
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
213 }
b895171 @fabpot moved the class loader to its own component
fabpot authored
214 }
215 }
216 }
217
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
218 foreach ($this->namespaceFallback as $dir) {
219 $file = $dir.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $class).'.php';
b895171 @fabpot moved the class loader to its own component
fabpot authored
220 if (file_exists($file)) {
608bce3 @kriswallsmith [ClassLoader] created protected findFile() method to allow creating a…
kriswallsmith authored
221 return $file;
b895171 @fabpot moved the class loader to its own component
fabpot authored
222 }
223 }
224 } else {
225 // PEAR-like class name
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
226 foreach ($this->prefixes as $prefix => $dirs) {
227 foreach ($dirs as $dir) {
228 if (0 === strpos($class, $prefix)) {
229 $file = $dir.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
230 if (file_exists($file)) {
608bce3 @kriswallsmith [ClassLoader] created protected findFile() method to allow creating a…
kriswallsmith authored
231 return $file;
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
232 }
b895171 @fabpot moved the class loader to its own component
fabpot authored
233 }
234 }
235 }
236
6a144c7 @fabpot [ClassLoader] added the possibility to define more than one directory…
fabpot authored
237 foreach ($this->prefixFallback as $dir) {
238 $file = $dir.DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
b895171 @fabpot moved the class loader to its own component
fabpot authored
239 if (file_exists($file)) {
608bce3 @kriswallsmith [ClassLoader] created protected findFile() method to allow creating a…
kriswallsmith authored
240 return $file;
b895171 @fabpot moved the class loader to its own component
fabpot authored
241 }
242 }
243 }
244 }
245 }
Something went wrong with that request. Please try again.