Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 203 lines (183 sloc) 5.349 kb
ce2d1411 » stof
2012-04-01 [ClassLoader] Added a simplified PSR-0 ClassLoader
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
94bf7492 » Grummfy
2012-06-19 fixed phpdoc (closes symfony/ClassLoader#3)
22 * $loader->addPrefix('Symfony\Component', __DIR__.'/component');
23 * $loader->addPrefix('Symfony', __DIR__.'/framework');
ce2d1411 » stof
2012-04-01 [ClassLoader] Added a simplified PSR-0 ClassLoader
24 *
25 * // activate the autoloader
26 * $loader->register();
27 *
4e509e34 » gajdaw
2012-04-23 [2.1][Component][ClassLoader] cs
28 * // to enable searching the include path (e.g. for PEAR packages)
ce2d1411 » stof
2012-04-01 [ClassLoader] Added a simplified PSR-0 ClassLoader
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
4e509e34 » gajdaw
2012-04-23 [2.1][Component][ClassLoader] cs
46 /**
47 * Returns prefixes.
48 *
49 * @return array
50 */
ce2d1411 » stof
2012-04-01 [ClassLoader] Added a simplified PSR-0 ClassLoader
51 public function getPrefixes()
52 {
53 return $this->prefixes;
54 }
55
4e509e34 » gajdaw
2012-04-23 [2.1][Component][ClassLoader] cs
56 /**
57 * Returns fallback directories.
58 *
59 * @return array
60 */
ce2d1411 » stof
2012-04-01 [ClassLoader] Added a simplified PSR-0 ClassLoader
61 public function getFallbackDirs()
62 {
63 return $this->fallbackDirs;
64 }
65
4e509e34 » gajdaw
2012-04-23 [2.1][Component][ClassLoader] cs
66 /**
67 * Adds prefixes.
68 *
69 * @param array $prefixes Prefixes to add
70 */
ce2d1411 » stof
2012-04-01 [ClassLoader] Added a simplified PSR-0 ClassLoader
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 *
4e509e34 » gajdaw
2012-04-23 [2.1][Component][ClassLoader] cs
81 * @param string $prefix The classes prefix
82 * @param array|string $paths The location(s) of the classes
ce2d1411 » stof
2012-04-01 [ClassLoader] Added a simplified PSR-0 ClassLoader
83 */
84 public function addPrefix($prefix, $paths)
85 {
86 if (!$prefix) {
87 foreach ((array) $paths as $path) {
88 $this->fallbackDirs[] = $path;
89 }
1d4c94bd » fabpot
2012-04-07 fixed CS
90
ce2d1411 » stof
2012-04-01 [ClassLoader] Added a simplified PSR-0 ClassLoader
91 return;
92 }
93 if (isset($this->prefixes[$prefix])) {
94 $this->prefixes[$prefix] = array_merge(
95 $this->prefixes[$prefix],
96 (array) $paths
97 );
98 } else {
99 $this->prefixes[$prefix] = (array) $paths;
100 }
101 }
102
103 /**
104 * Turns on searching the include for class files.
105 *
106 * @param Boolean $useIncludePath
107 */
108 public function setUseIncludePath($useIncludePath)
109 {
110 $this->useIncludePath = $useIncludePath;
111 }
112
113 /**
114 * Can be used to check if the autoloader uses the include path to check
115 * for classes.
116 *
117 * @return Boolean
118 */
119 public function getUseIncludePath()
120 {
121 return $this->useIncludePath;
122 }
123
124 /**
125 * Registers this instance as an autoloader.
126 *
127 * @param Boolean $prepend Whether to prepend the autoloader or not
128 */
129 public function register($prepend = false)
130 {
131 spl_autoload_register(array($this, 'loadClass'), true, $prepend);
132 }
133
134 /**
135 * Unregisters this instance as an autoloader.
136 */
137 public function unregister()
138 {
139 spl_autoload_unregister(array($this, 'loadClass'));
140 }
141
142 /**
143 * Loads the given class or interface.
144 *
145 * @param string $class The name of the class
0b8b41c5 » fabpot
2012-05-01 fixed CS
146 *
ce2d1411 » stof
2012-04-01 [ClassLoader] Added a simplified PSR-0 ClassLoader
147 * @return Boolean|null True, if loaded
148 */
149 public function loadClass($class)
150 {
151 if ($file = $this->findFile($class)) {
152 require $file;
1d4c94bd » fabpot
2012-04-07 fixed CS
153
ce2d1411 » stof
2012-04-01 [ClassLoader] Added a simplified PSR-0 ClassLoader
154 return true;
155 }
156 }
157
158 /**
159 * Finds the path to the file where the class is defined.
160 *
161 * @param string $class The name of the class
162 *
163 * @return string|null The path, if found
164 */
165 public function findFile($class)
166 {
167 if ('\\' == $class[0]) {
168 $class = substr($class, 1);
169 }
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 (0 === strpos($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 }
Something went wrong with that request. Please try again.