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.
15
* This software consists of voluntary contributions made by many individuals
16
* and is licensed under the MIT license. For more information, see
17
* <http://www.doctrine-project.org>.
20
namespace Doctrine\Common;
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.
28
* If no include path is configured through the constructor or {@link setIncludePath}, a ClassLoader
29
* relies on the PHP <code>include_path</code>.
31
* @author Roman Borschel <roman@code-factory.org>
37
* @var string PHP file extension
39
protected $fileExtension = '.php';
42
* @var string Current namespace
47
* @var string Current include path
49
protected $includePath;
52
* @var string PHP namespace separator
54
protected $namespaceSeparator = '\\';
57
* Creates a new <tt>ClassLoader</tt> that loads classes of the
58
* specified namespace from the specified include path.
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.
64
* @param string $ns The namespace of the classes to load.
65
* @param string $includePath The base include path to use.
67
public function __construct($ns = null, $includePath = null)
69
$this->namespace = $ns;
70
$this->includePath = $includePath;
74
* Sets the namespace separator used by classes in the namespace of this ClassLoader.
76
* @param string $sep The separator to use.
78
public function setNamespaceSeparator($sep)
80
$this->namespaceSeparator = $sep;
84
* Gets the namespace separator used by classes in the namespace of this ClassLoader.
88
public function getNamespaceSeparator()
90
return $this->namespaceSeparator;
94
* Sets the base include path for all class files in the namespace of this ClassLoader.
96
* @param string $includePath
98
public function setIncludePath($includePath)
100
$this->includePath = $includePath;
104
* Gets the base include path for all class files in the namespace of this ClassLoader.
108
public function getIncludePath()
110
return $this->includePath;
114
* Sets the file extension of class files in the namespace of this ClassLoader.
116
* @param string $fileExtension
118
public function setFileExtension($fileExtension)
120
$this->fileExtension = $fileExtension;
124
* Gets the file extension of class files in the namespace of this ClassLoader.
128
public function getFileExtension()
130
return $this->fileExtension;
134
* Registers this ClassLoader on the SPL autoload stack.
136
public function register()
138
spl_autoload_register(array($this, 'loadClass'));
142
* Removes this ClassLoader from the SPL autoload stack.
144
public function unregister()
146
spl_autoload_unregister(array($this, 'loadClass'));
150
* Loads the given class or interface.
152
* @param string $className The name of the class to load.
154
* @return boolean TRUE if the class has been successfully loaded, FALSE otherwise.
156
public function loadClass($className)
158
if ($this->namespace !== null && strpos($className, $this->namespace.$this->namespaceSeparator) !== 0) {
162
require ($this->includePath !== null ? $this->includePath . DIRECTORY_SEPARATOR : '')
163
. str_replace($this->namespaceSeparator, DIRECTORY_SEPARATOR, $className)
164
. $this->fileExtension;
170
* Asks this ClassLoader whether it can potentially load the class (file) with
173
* @param string $className The fully-qualified name of the class.
174
* @return boolean TRUE if this ClassLoader can load the class, FALSE otherwise.
176
public function canLoadClass($className)
178
if ($this->namespace !== null && strpos($className, $this->namespace.$this->namespaceSeparator) !== 0) {
182
$file = str_replace($this->namespaceSeparator, DIRECTORY_SEPARATOR, $className) . $this->fileExtension;
184
if ($this->includePath !== null) {
185
return file_exists($this->includePath . DIRECTORY_SEPARATOR . $file);
188
return (false !== stream_resolve_include_path($file));
192
* Checks whether a class with a given name exists. A class "exists" if it is either
193
* already defined in the current request or if there is an autoloader on the SPL
194
* autoload stack that is a) responsible for the class in question and b) is able to
195
* load a class file in which the class definition resides.
197
* If the class is not already defined, each autoloader in the SPL autoload stack
198
* is asked whether it is able to tell if the class exists. If the autoloader is
199
* a <tt>ClassLoader</tt>, {@link canLoadClass} is used, otherwise the autoload
200
* function of the autoloader is invoked and expected to return a value that
201
* evaluates to TRUE if the class (file) exists. As soon as one autoloader reports
202
* that the class exists, TRUE is returned.
204
* Note that, depending on what kinds of autoloaders are installed on the SPL
205
* autoload stack, the class (file) might already be loaded as a result of checking
206
* for its existence. This is not the case with a <tt>ClassLoader</tt>, who separates
207
* these responsibilities.
209
* @param string $className The fully-qualified name of the class.
210
* @return boolean TRUE if the class exists as per the definition given above, FALSE otherwise.
212
public static function classExists($className)
214
if (class_exists($className, false) || interface_exists($className, false)) {
218
foreach (spl_autoload_functions() as $loader) {
219
if (is_array($loader)) { // array(???, ???)
220
if (is_object($loader[0])) {
221
if ($loader[0] instanceof ClassLoader) { // array($obj, 'methodName')
222
if ($loader[0]->canLoadClass($className)) {
225
} else if ($loader[0]->{$loader[1]}($className)) {
228
} else if ($loader[0]::$loader[1]($className)) { // array('ClassName', 'methodName')
231
} else if ($loader instanceof \Closure) { // function($className) {..}
232
if ($loader($className)) {
235
} else if (is_string($loader) && $loader($className)) { // "MyClass::loadClass"
240
return class_exists($className, false) || interface_exists($className, false);
244
* Gets the <tt>ClassLoader</tt> from the SPL autoload stack that is responsible
245
* for (and is able to load) the class with the given name.
247
* @param string $className The name of the class.
248
* @return ClassLoader The <tt>ClassLoader</tt> for the class or NULL if no such <tt>ClassLoader</tt> exists.
250
public static function getClassLoader($className)
252
foreach (spl_autoload_functions() as $loader) {
253
if (is_array($loader)
254
&& $loader[0] instanceof ClassLoader
255
&& $loader[0]->canLoadClass($className)