1: <?php
2:
3: // Copyright (c) 2004-2013 Fabien Potencier
4:
5: // Permission is hereby granted, free of charge, to any person obtaining a copy
6: // of this software and associated documentation files (the "Software"), to deal
7: // in the Software without restriction, including without limitation the rights
8: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9: // copies of the Software, and to permit persons to whom the Software is furnished
10: // to do so, subject to the following conditions:
11:
12: // The above copyright notice and this permission notice shall be included in all
13: // copies or substantial portions of the Software.
14:
15: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21: // THE SOFTWARE.
22:
23: class ClassLoader
24: {
25: private $namespaces = array();
26: private $prefixes = array();
27: private $namespaceFallbacks = array();
28: private $prefixFallbacks = array();
29: private $useIncludePath = false;
30:
31: /**
32: * Turns on searching the include for class files. Allows easy loading
33: * of installed PEAR packages
34: *
35: * @param Boolean $useIncludePath
36: */
37: public function useIncludePath($useIncludePath)
38: {
39: $this->useIncludePath = $useIncludePath;
40: }
41:
42: /**
43: * Can be used to check if the autoloader uses the include path to check
44: * for classes.
45: *
46: * @return Boolean
47: */
48: public function getUseIncludePath()
49: {
50: return $this->useIncludePath;
51: }
52:
53: /**
54: * Gets the configured namespaces.
55: *
56: * @return array A hash with namespaces as keys and directories as values
57: */
58: public function getNamespaces()
59: {
60: return $this->namespaces;
61: }
62:
63: /**
64: * Gets the configured class prefixes.
65: *
66: * @return array A hash with class prefixes as keys and directories as values
67: */
68: public function getPrefixes()
69: {
70: return $this->prefixes;
71: }
72:
73: /**
74: * Gets the directory(ies) to use as a fallback for namespaces.
75: *
76: * @return array An array of directories
77: */
78: public function getNamespaceFallbacks()
79: {
80: return $this->namespaceFallbacks;
81: }
82:
83: /**
84: * Gets the directory(ies) to use as a fallback for class prefixes.
85: *
86: * @return array An array of directories
87: */
88: public function getPrefixFallbacks()
89: {
90: return $this->prefixFallbacks;
91: }
92:
93: /**
94: * Registers the directory to use as a fallback for namespaces.
95: *
96: * @param array $dirs An array of directories
97: *
98: * @api
99: */
100: public function registerNamespaceFallbacks(array $dirs)
101: {
102: $this->namespaceFallbacks = $dirs;
103: }
104:
105: /**
106: * Registers a directory to use as a fallback for namespaces.
107: *
108: * @param string $dir A directory
109: */
110: public function registerNamespaceFallback($dir)
111: {
112: $this->namespaceFallbacks[] = $dir;
113: }
114:
115: /**
116: * Registers directories to use as a fallback for class prefixes.
117: *
118: * @param array $dirs An array of directories
119: *
120: * @api
121: */
122: public function registerPrefixFallbacks(array $dirs)
123: {
124: $this->prefixFallbacks = $dirs;
125: }
126:
127: /**
128: * Registers a directory to use as a fallback for class prefixes.
129: *
130: * @param string $dir A directory
131: */
132: public function registerPrefixFallback($dir)
133: {
134: $this->prefixFallbacks[] = $dir;
135: }
136:
137: /**
138: * Registers an array of namespaces
139: *
140: * @param array $namespaces An array of namespaces (namespaces as keys and locations as values)
141: *
142: * @api
143: */
144: public function registerNamespaces(array $namespaces)
145: {
146: foreach ($namespaces as $namespace => $locations) {
147: $this->namespaces[$namespace] = (array) $locations;
148: }
149: }
150:
151: /**
152: * Registers a namespace.
153: *
154: * @param string $namespace The namespace
155: * @param array|string $paths The location(s) of the namespace
156: *
157: * @api
158: */
159: public function registerNamespace($namespace, $paths)
160: {
161: $this->namespaces[$namespace] = (array) $paths;
162: }
163:
164: /**
165: * Registers an array of classes using the PEAR naming convention.
166: *
167: * @param array $classes An array of classes (prefixes as keys and locations as values)
168: *
169: * @api
170: */
171: public function registerPrefixes(array $classes)
172: {
173: foreach ($classes as $prefix => $locations) {
174: $this->prefixes[$prefix] = (array) $locations;
175: }
176: }
177:
178: /**
179: * Registers a set of classes using the PEAR naming convention.
180: *
181: * @param string $prefix The classes prefix
182: * @param array|string $paths The location(s) of the classes
183: *
184: * @api
185: */
186: public function registerPrefix($prefix, $paths)
187: {
188: $this->prefixes[$prefix] = (array) $paths;
189: }
190:
191: /**
192: * Registers this instance as an autoloader.
193: *
194: * @param Boolean $prepend Whether to prepend the autoloader or not
195: *
196: * @api
197: */
198: public function register($prepend = false)
199: {
200: spl_autoload_register(array($this, 'loadClass'), true, $prepend);
201: }
202:
203: /**
204: * Fix for certain versions of PHP that have trouble with
205: * namespaces with leading separators.
206: *
207: * @access private
208: * @param mixed $className
209: * @return void
210: */
211: private function makeBackwardsCompatible($className)
212: {
213: return (phpversion() < '5.3.3') ? ltrim($className, '\\') : $className;
214: }
215:
216: /**
217: * Loads the given class or interface.
218: *
219: * @param string $class The name of the class
220: *
221: * @return Boolean|null True, if loaded
222: */
223: public function loadClass($class)
224: {
225: $class = $this->makeBackwardsCompatible($class);
226:
227: if ($file = $this->findFile($class)) {
228: require $file;
229:
230: return true;
231: }
232: }
233:
234: /**
235: * Finds the path to the file where the class is defined.
236: *
237: * @param string $class The name of the class
238: *
239: * @return string|null The path, if found
240: */
241: public function findFile($class)
242: {
243: if (false !== $pos = strrpos($class, '\\')) {
244: // namespaced class name
245: $namespace = substr($class, 0, $pos);
246: $className = substr($class, $pos + 1);
247: $normalizedClass = str_replace('\\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR.str_replace('_', DIRECTORY_SEPARATOR, $className).'.php';
248: foreach ($this->namespaces as $ns => $dirs) {
249: if (0 !== strpos($namespace, $ns)) {
250: continue;
251: }
252:
253: foreach ($dirs as $dir) {
254: $file = $dir.DIRECTORY_SEPARATOR.$normalizedClass;
255: if (is_file($file)) {
256: return $file;
257: }
258: }
259: }
260:
261: foreach ($this->namespaceFallbacks as $dir) {
262: $file = $dir.DIRECTORY_SEPARATOR.$normalizedClass;
263: if (is_file($file)) {
264: return $file;
265: }
266: }
267:
268: } else {
269: // PEAR-like class name
270: $normalizedClass = str_replace('_', DIRECTORY_SEPARATOR, $class).'.php';
271: foreach ($this->prefixes as $prefix => $dirs) {
272: if (0 !== strpos($class, $prefix)) {
273: continue;
274: }
275:
276: foreach ($dirs as $dir) {
277: $file = $dir.DIRECTORY_SEPARATOR.$normalizedClass;
278: if (is_file($file)) {
279: return $file;
280: }
281: }
282: }
283:
284: foreach ($this->prefixFallbacks as $dir) {
285: $file = $dir.DIRECTORY_SEPARATOR.$normalizedClass;
286: if (is_file($file)) {
287: return $file;
288: }
289: }
290: }
291:
292: if ($this->useIncludePath && $file = stream_resolve_include_path($normalizedClass)) {
293: return $file;
294: }
295: }
296: }
297: