1 | <?php |
---|
2 | /* |
---|
3 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
4 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
5 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
6 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
7 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
8 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
---|
9 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
10 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
11 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
12 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
13 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
14 | * |
---|
15 | * This software consists of voluntary contributions made by many individuals |
---|
16 | * and is licensed under the LGPL. For more information, see |
---|
17 | * <http://www.doctrine-project.org>. |
---|
18 | */ |
---|
19 | |
---|
20 | namespace Doctrine\Common; |
---|
21 | |
---|
22 | /** |
---|
23 | * A <tt>ClassLoader</tt> is an autoloader for class files that can be |
---|
24 | * installed on the SPL autoload stack. It is a class loader that either loads only classes |
---|
25 | * of a specific namespace or all namespaces and it is suitable for working together |
---|
26 | * with other autoloaders in the SPL autoload stack. |
---|
27 | * |
---|
28 | * If no include path is configured through the constructor or {@link setIncludePath}, a ClassLoader |
---|
29 | * relies on the PHP <code>include_path</code>. |
---|
30 | * |
---|
31 | * @author Roman Borschel <roman@code-factory.org> |
---|
32 | * @since 2.0 |
---|
33 | */ |
---|
34 | class ClassLoader |
---|
35 | { |
---|
36 | /** |
---|
37 | * @var string PHP file extension |
---|
38 | */ |
---|
39 | protected $fileExtension = '.php'; |
---|
40 | |
---|
41 | /** |
---|
42 | * @var string Current namespace |
---|
43 | */ |
---|
44 | protected $namespace; |
---|
45 | |
---|
46 | /** |
---|
47 | * @var string Current include path |
---|
48 | */ |
---|
49 | protected $includePath; |
---|
50 | |
---|
51 | /** |
---|
52 | * @var string PHP namespace separator |
---|
53 | */ |
---|
54 | protected $namespaceSeparator = '\\'; |
---|
55 | |
---|
56 | /** |
---|
57 | * Creates a new <tt>ClassLoader</tt> that loads classes of the |
---|
58 | * specified namespace from the specified include path. |
---|
59 | * |
---|
60 | * If no include path is given, the ClassLoader relies on the PHP include_path. |
---|
61 | * If neither a namespace nor an include path is given, the ClassLoader will |
---|
62 | * be responsible for loading all classes, thereby relying on the PHP include_path. |
---|
63 | * |
---|
64 | * @param string $ns The namespace of the classes to load. |
---|
65 | * @param string $includePath The base include path to use. |
---|
66 | */ |
---|
67 | public function __construct($ns = null, $includePath = null) |
---|
68 | { |
---|
69 | $this->namespace = $ns; |
---|
70 | $this->includePath = $includePath; |
---|
71 | } |
---|
72 | |
---|
73 | /** |
---|
74 | * Sets the namespace separator used by classes in the namespace of this ClassLoader. |
---|
75 | * |
---|
76 | * @param string $sep The separator to use. |
---|
77 | */ |
---|
78 | public function setNamespaceSeparator($sep) |
---|
79 | { |
---|
80 | $this->namespaceSeparator = $sep; |
---|
81 | } |
---|
82 | |
---|
83 | /** |
---|
84 | * Gets the namespace separator used by classes in the namespace of this ClassLoader. |
---|
85 | * |
---|
86 | * @return string |
---|
87 | */ |
---|
88 | public function getNamespaceSeparator() |
---|
89 | { |
---|
90 | return $this->namespaceSeparator; |
---|
91 | } |
---|
92 | |
---|
93 | /** |
---|
94 | * Sets the base include path for all class files in the namespace of this ClassLoader. |
---|
95 | * |
---|
96 | * @param string $includePath |
---|
97 | */ |
---|
98 | public function setIncludePath($includePath) |
---|
99 | { |
---|
100 | $this->includePath = $includePath; |
---|
101 | } |
---|
102 | |
---|
103 | /** |
---|
104 | * Gets the base include path for all class files in the namespace of this ClassLoader. |
---|
105 | * |
---|
106 | * @return string |
---|
107 | */ |
---|
108 | public function getIncludePath() |
---|
109 | { |
---|
110 | return $this->includePath; |
---|
111 | } |
---|
112 | |
---|
113 | /** |
---|
114 | * Sets the file extension of class files in the namespace of this ClassLoader. |
---|
115 | * |
---|
116 | * @param string $fileExtension |
---|
117 | */ |
---|
118 | public function setFileExtension($fileExtension) |
---|
119 | { |
---|
120 | $this->fileExtension = $fileExtension; |
---|
121 | } |
---|
122 | |
---|
123 | /** |
---|
124 | * Gets the file extension of class files in the namespace of this ClassLoader. |
---|
125 | * |
---|
126 | * @return string |
---|
127 | */ |
---|
128 | public function getFileExtension() |
---|
129 | { |
---|
130 | return $this->fileExtension; |
---|
131 | } |
---|
132 | |
---|
133 | /** |
---|
134 | * Registers this ClassLoader on the SPL autoload stack. |
---|
135 | */ |
---|
136 | public function register() |
---|
137 | { |
---|
138 | spl_autoload_register(array($this, 'loadClass')); |
---|
139 | } |
---|
140 | |
---|
141 | /** |
---|
142 | * Removes this ClassLoader from the SPL autoload stack. |
---|
143 | */ |
---|
144 | public function unregister() |
---|
145 | { |
---|
146 | spl_autoload_unregister(array($this, 'loadClass')); |
---|
147 | } |
---|
148 | |
---|
149 | /** |
---|
150 | * Loads the given class or interface. |
---|
151 | * |
---|
152 | * @param string $classname The name of the class to load. |
---|
153 | * @return boolean TRUE if the class has been successfully loaded, FALSE otherwise. |
---|
154 | */ |
---|
155 | public function loadClass($className) |
---|
156 | { |
---|
157 | if ($this->namespace !== null && strpos($className, $this->namespace.$this->namespaceSeparator) !== 0) { |
---|
158 | return false; |
---|
159 | } |
---|
160 | |
---|
161 | require ($this->includePath !== null ? $this->includePath . DIRECTORY_SEPARATOR : '') |
---|
162 | . str_replace($this->namespaceSeparator, DIRECTORY_SEPARATOR, $className) |
---|
163 | . $this->fileExtension; |
---|
164 | |
---|
165 | return true; |
---|
166 | } |
---|
167 | |
---|
168 | /** |
---|
169 | * Asks this ClassLoader whether it can potentially load the class (file) with |
---|
170 | * the given name. |
---|
171 | * |
---|
172 | * @param string $className The fully-qualified name of the class. |
---|
173 | * @return boolean TRUE if this ClassLoader can load the class, FALSE otherwise. |
---|
174 | */ |
---|
175 | public function canLoadClass($className) |
---|
176 | { |
---|
177 | if ($this->namespace !== null && strpos($className, $this->namespace.$this->namespaceSeparator) !== 0) { |
---|
178 | return false; |
---|
179 | } |
---|
180 | |
---|
181 | $file = str_replace($this->namespaceSeparator, DIRECTORY_SEPARATOR, $className) . $this->fileExtension; |
---|
182 | |
---|
183 | if ($this->includePath !== null) { |
---|
184 | return file_exists($this->includePath . DIRECTORY_SEPARATOR . $file); |
---|
185 | } |
---|
186 | |
---|
187 | return (false !== stream_resolve_include_path($file)); |
---|
188 | } |
---|
189 | |
---|
190 | /** |
---|
191 | * Checks whether a class with a given name exists. A class "exists" if it is either |
---|
192 | * already defined in the current request or if there is an autoloader on the SPL |
---|
193 | * autoload stack that is a) responsible for the class in question and b) is able to |
---|
194 | * load a class file in which the class definition resides. |
---|
195 | * |
---|
196 | * If the class is not already defined, each autoloader in the SPL autoload stack |
---|
197 | * is asked whether it is able to tell if the class exists. If the autoloader is |
---|
198 | * a <tt>ClassLoader</tt>, {@link canLoadClass} is used, otherwise the autoload |
---|
199 | * function of the autoloader is invoked and expected to return a value that |
---|
200 | * evaluates to TRUE if the class (file) exists. As soon as one autoloader reports |
---|
201 | * that the class exists, TRUE is returned. |
---|
202 | * |
---|
203 | * Note that, depending on what kinds of autoloaders are installed on the SPL |
---|
204 | * autoload stack, the class (file) might already be loaded as a result of checking |
---|
205 | * for its existence. This is not the case with a <tt>ClassLoader</tt>, who separates |
---|
206 | * these responsibilities. |
---|
207 | * |
---|
208 | * @param string $className The fully-qualified name of the class. |
---|
209 | * @return boolean TRUE if the class exists as per the definition given above, FALSE otherwise. |
---|
210 | */ |
---|
211 | public static function classExists($className) |
---|
212 | { |
---|
213 | if (class_exists($className, false) || interface_exists($className, false)) { |
---|
214 | return true; |
---|
215 | } |
---|
216 | |
---|
217 | foreach (spl_autoload_functions() as $loader) { |
---|
218 | if (is_array($loader)) { // array(???, ???) |
---|
219 | if (is_object($loader[0])) { |
---|
220 | if ($loader[0] instanceof ClassLoader) { // array($obj, 'methodName') |
---|
221 | if ($loader[0]->canLoadClass($className)) { |
---|
222 | return true; |
---|
223 | } |
---|
224 | } else if ($loader[0]->{$loader[1]}($className)) { |
---|
225 | return true; |
---|
226 | } |
---|
227 | } else if ($loader[0]::$loader[1]($className)) { // array('ClassName', 'methodName') |
---|
228 | return true; |
---|
229 | } |
---|
230 | } else if ($loader instanceof \Closure) { // function($className) {..} |
---|
231 | if ($loader($className)) { |
---|
232 | return true; |
---|
233 | } |
---|
234 | } else if (is_string($loader) && $loader($className)) { // "MyClass::loadClass" |
---|
235 | return true; |
---|
236 | } |
---|
237 | } |
---|
238 | |
---|
239 | return class_exists($className, false) || interface_exists($className, false); |
---|
240 | } |
---|
241 | |
---|
242 | /** |
---|
243 | * Gets the <tt>ClassLoader</tt> from the SPL autoload stack that is responsible |
---|
244 | * for (and is able to load) the class with the given name. |
---|
245 | * |
---|
246 | * @param string $className The name of the class. |
---|
247 | * @return The <tt>ClassLoader</tt> for the class or NULL if no such <tt>ClassLoader</tt> exists. |
---|
248 | */ |
---|
249 | public static function getClassLoader($className) |
---|
250 | { |
---|
251 | foreach (spl_autoload_functions() as $loader) { |
---|
252 | if (is_array($loader) |
---|
253 | && $loader[0] instanceof ClassLoader |
---|
254 | && $loader[0]->canLoadClass($className) |
---|
255 | ) { |
---|
256 | return $loader[0]; |
---|
257 | } |
---|
258 | } |
---|
259 | |
---|
260 | return null; |
---|
261 | } |
---|
262 | } |
---|