1 | <?php |
||
2 | /** |
||
3 | * SplClassLoader implementation that implements the technical interoperability |
||
4 | * standards for PHP 5.3 namespaces and class names. |
||
5 | * |
||
6 | * http://groups.google.com/group/php-standards/web/psr-0-final-proposal?pli=1 |
||
7 | * |
||
8 | * // Example which loads classes for the Doctrine Common package in the |
||
9 | * // Doctrine\Common namespace. |
||
10 | * $classLoader = new SplClassLoader('Doctrine\Common', '/path/to/doctrine'); |
||
11 | * $classLoader->register(); |
||
12 | * |
||
13 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
||
14 | * @author Jonathan H. Wage <[email protected]> |
||
15 | * @author Roman S. Borschel <[email protected]> |
||
16 | * @author Matthew Weier O'Phinney <[email protected]> |
||
17 | * @author Kris Wallsmith <[email protected]> |
||
18 | * @author Fabien Potencier <[email protected]> |
||
19 | */ |
||
20 | class SplClassLoader |
||
21 | { |
||
22 | private $_fileExtension = '.php'; |
||
23 | private $_namespace; |
||
24 | private $_includePath; |
||
25 | private $_namespaceSeparator = '\\'; |
||
26 | private $_prepend = false; |
||
27 | private $_excluded = array(); |
||
28 | |||
29 | /** |
||
30 | * Creates a new <tt>SplClassLoader</tt> that loads classes of the |
||
31 | * specified namespace. |
||
32 | * |
||
33 | * @param string $ns The namespace to use. |
||
34 | * @param string $includePath |
||
35 | */ |
||
36 | public function __construct($ns = null, $includePath = null) |
||
37 | { |
||
38 | $this->_namespace = $ns; |
||
39 | $this->_includePath = $includePath; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * @param string $excluded |
||
44 | */ |
||
45 | public function addExcluded($excluded) |
||
46 | { |
||
47 | $this->_excluded[] = $excluded; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * @param boolean $prepend |
||
52 | */ |
||
53 | public function setPrepend($prepend) |
||
54 | { |
||
55 | $this->_prepend = $prepend; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Gets the namespace seperator used by classes in the namespace of this class loader. |
||
60 | * |
||
61 | * @return void |
||
62 | */ |
||
63 | public function getNamespaceSeparator() |
||
64 | { |
||
65 | return $this->_namespaceSeparator; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||
66 | } |
||
67 | |||
68 | /** |
||
69 | * Sets the namespace separator used by classes in the namespace of this class loader. |
||
70 | * |
||
71 | * @param string $sep The separator to use. |
||
72 | */ |
||
73 | public function setNamespaceSeparator($sep) |
||
74 | { |
||
75 | $this->_namespaceSeparator = $sep; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * Gets the base include path for all class files in the namespace of this class loader. |
||
80 | * |
||
81 | * @return string $includePath |
||
82 | */ |
||
83 | public function getIncludePath() |
||
84 | { |
||
85 | return $this->_includePath; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * Sets the base include path for all class files in the namespace of this class loader. |
||
90 | * |
||
91 | * @param string $includePath |
||
92 | */ |
||
93 | public function setIncludePath($includePath) |
||
94 | { |
||
95 | $this->_includePath = $includePath; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Gets the file extension of class files in the namespace of this class loader. |
||
100 | * |
||
101 | * @return string $fileExtension |
||
102 | */ |
||
103 | public function getFileExtension() |
||
104 | { |
||
105 | return $this->_fileExtension; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Sets the file extension of class files in the namespace of this class loader. |
||
110 | * |
||
111 | * @param string $fileExtension |
||
112 | */ |
||
113 | public function setFileExtension($fileExtension) |
||
114 | { |
||
115 | $this->_fileExtension = $fileExtension; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Installs this class loader on the SPL autoload stack. |
||
120 | */ |
||
121 | public function register() |
||
122 | { |
||
123 | spl_autoload_register([$this, 'loadClass'], true, $this->_prepend); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * Uninstalls this class loader from the SPL autoloader stack. |
||
128 | */ |
||
129 | public function unregister() |
||
130 | { |
||
131 | spl_autoload_unregister([$this, 'loadClass']); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Loads the given class or interface. |
||
136 | * |
||
137 | * @param string $className The name of the class to load. |
||
138 | * @return void |
||
139 | */ |
||
140 | public function loadClass($className) |
||
141 | { |
||
142 | if (!in_array($className, $this->_excluded) && |
||
143 | (null === $this->_namespace |
||
144 | || 0 === strpos($className, $this->_namespace . $this->_namespaceSeparator)) |
||
145 | ) { |
||
146 | $fileName = ''; |
||
147 | if (false !== ($lastNsPos = strripos($className, $this->_namespaceSeparator))) { |
||
148 | $namespace = substr($className, 0, $lastNsPos); |
||
149 | $className = substr($className, $lastNsPos + 1); |
||
150 | $fileName = str_replace($this->_namespaceSeparator, DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR; |
||
151 | } |
||
152 | |||
153 | $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . $this->_fileExtension; |
||
154 | |||
155 | require ($this->_includePath !== null ? $this->_includePath . DIRECTORY_SEPARATOR : '') . $fileName; |
||
156 | } |
||
157 | } |
||
158 | } |