Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 136 lines (124 sloc) 4.081 kb
618b939 @jbroadway Moved code to lib/ folder, added optional PSR-0 support, added PSR-0 …
authored
1 <?php
2
3 /**
4 * SplClassLoader implementation that implements the technical interoperability
5 * standards for PHP 5.3 namespaces and class names.
6 *
7 * http://groups.google.com/group/php-standards/web/final-proposal
8 *
9 * // Example which loads classes for the Doctrine Common package in the
10 * // Doctrine\Common namespace.
11 * $classLoader = new SplClassLoader('Doctrine\Common', '/path/to/doctrine');
12 * $classLoader->register();
13 *
14 * @author Jonathan H. Wage <jonwage@gmail.com>
15 * @author Roman S. Borschel <roman@code-factory.org>
16 * @author Matthew Weier O'Phinney <matthew@zend.com>
17 * @author Kris Wallsmith <kris.wallsmith@gmail.com>
18 * @author Fabien Potencier <fabien.potencier@symfony-project.org>
19 */
20 class SplClassLoader
21 {
22 private $_fileExtension = '.php';
23 private $_namespace;
24 private $_includePath;
25 private $_namespaceSeparator = '\\';
26
27 /**
28 * Creates a new <tt>SplClassLoader</tt> that loads classes of the
29 * specified namespace.
30 *
31 * @param string $ns The namespace to use.
32 */
33 public function __construct($ns = null, $includePath = null)
34 {
35 $this->_namespace = $ns;
36 $this->_includePath = $includePath;
37 }
38
39 /**
40 * Sets the namespace separator used by classes in the namespace of this class loader.
41 *
42 * @param string $sep The separator to use.
43 */
44 public function setNamespaceSeparator($sep)
45 {
46 $this->_namespaceSeparator = $sep;
47 }
48
49 /**
50 * Gets the namespace seperator used by classes in the namespace of this class loader.
51 *
52 * @return void
53 */
54 public function getNamespaceSeparator()
55 {
56 return $this->_namespaceSeparator;
57 }
58
59 /**
60 * Sets the base include path for all class files in the namespace of this class loader.
61 *
62 * @param string $includePath
63 */
64 public function setIncludePath($includePath)
65 {
66 $this->_includePath = $includePath;
67 }
68
69 /**
70 * Gets the base include path for all class files in the namespace of this class loader.
71 *
72 * @return string $includePath
73 */
74 public function getIncludePath()
75 {
76 return $this->_includePath;
77 }
78
79 /**
80 * Sets the file extension of class files in the namespace of this class loader.
81 *
82 * @param string $fileExtension
83 */
84 public function setFileExtension($fileExtension)
85 {
86 $this->_fileExtension = $fileExtension;
87 }
88
89 /**
90 * Gets the file extension of class files in the namespace of this class loader.
91 *
92 * @return string $fileExtension
93 */
94 public function getFileExtension()
95 {
96 return $this->_fileExtension;
97 }
98
99 /**
100 * Installs this class loader on the SPL autoload stack.
101 */
102 public function register()
103 {
104 spl_autoload_register(array($this, 'loadClass'));
105 }
106
107 /**
108 * Uninstalls this class loader from the SPL autoloader stack.
109 */
110 public function unregister()
111 {
112 spl_autoload_unregister(array($this, 'loadClass'));
113 }
114
115 /**
116 * Loads the given class or interface.
117 *
118 * @param string $className The name of the class to load.
119 * @return void
120 */
121 public function loadClass($className)
122 {
123 if (null === $this->_namespace || $this->_namespace.$this->_namespaceSeparator === substr($className, 0, strlen($this->_namespace.$this->_namespaceSeparator))) {
124 $fileName = '';
125 $namespace = '';
126 if (false !== ($lastNsPos = strripos($className, $this->_namespaceSeparator))) {
127 $namespace = substr($className, 0, $lastNsPos);
128 $className = substr($className, $lastNsPos + 1);
129 $fileName = str_replace($this->_namespaceSeparator, DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
130 }
131 $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . $this->_fileExtension;
132
133 require ($this->_includePath !== null ? $this->_includePath . DIRECTORY_SEPARATOR : '') . $fileName;
134 }
135 }
136 }
Something went wrong with that request. Please try again.