- diff --git a/lib/Doctrine/Common/ClassLoader.php b/lib/Doctrine/Common/ClassLoader.php
- index d971fe9..507131e 100644
- --- a/lib/Doctrine/Common/ClassLoader.php
- +++ b/lib/Doctrine/Common/ClassLoader.php
- @@ -21,12 +21,12 @@ namespace Doctrine\Common;
- /**
- * A <tt>ClassLoader</tt> is an autoloader for class files that can be
- - * installed on the SPL autoload stack. It is a class loader that loads only classes
- - * of a specific namespace or all namespaces and is suitable for working together
- + * installed on the SPL autoload stack. It is a class loader that either loads only classes
- + * of a specific namespace or all namespaces and it is suitable for working together
- * with other autoloaders in the SPL autoload stack.
- *
- * If no include path is configured through {@link setIncludePath}, a ClassLoader
- - * relies on the PHP include_path.
- + * relies on the PHP <code>include_path</code>.
- *
- * @author Roman Borschel <roman@code-factory.org>
- * @since 2.0
- @@ -37,21 +37,26 @@ class ClassLoader
- private $_namespace;
- private $_includePath;
- private $_namespaceSeparator = '\\';
- -
- +
- /**
- * Creates a new <tt>ClassLoader</tt> that loads classes of the
- - * specified namespace.
- + * specified namespace from the specified include path.
- + *
- + * If no include path is given, the ClassLoader relies on the PHP include_path.
- + * If neither a namespace nor an include path is given, the ClassLoader will
- + * be responsible for loading all classes, thereby relying on the PHP include_path.
- *
- - * @param string $ns The namespace to use.
- + * @param string $ns The namespace of the classes to load.
- + * @param string $includePath The base include path to use.
- */
- public function __construct($ns = null, $includePath = null)
- {
- $this->_namespace = $ns;
- $this->_includePath = $includePath;
- }
- -
- +
- /**
- - * Sets the namespace separator used by classes in the namespace of this class loader.
- + * Sets the namespace separator used by classes in the namespace of this ClassLoader.
- *
- * @param string $sep The separator to use.
- */
- @@ -59,9 +64,9 @@ class ClassLoader
- {
- $this->_namespaceSeparator = $sep;
- }
- -
- +
- /**
- - * Gets the namespace separator used by classes in the namespace of this class loader.
- + * Gets the namespace separator used by classes in the namespace of this ClassLoader.
- *
- * @return string
- */
- @@ -69,9 +74,9 @@ class ClassLoader
- {
- return $this->_namespaceSeparator;
- }
- -
- +
- /**
- - * Sets the base include path for all class files in the namespace of this class loader.
- + * Sets the base include path for all class files in the namespace of this ClassLoader.
- *
- * @param string $includePath
- */
- @@ -79,9 +84,9 @@ class ClassLoader
- {
- $this->_includePath = $includePath;
- }
- -
- +
- /**
- - * Gets the base include path for all class files in the namespace of this class loader.
- + * Gets the base include path for all class files in the namespace of this ClassLoader.
- *
- * @return string
- */
- @@ -89,9 +94,9 @@ class ClassLoader
- {
- return $this->_includePath;
- }
- -
- +
- /**
- - * Sets the file extension of class files in the namespace of this class loader.
- + * Sets the file extension of class files in the namespace of this ClassLoader.
- *
- * @param string $fileExtension
- */
- @@ -99,9 +104,9 @@ class ClassLoader
- {
- $this->_fileExtension = $fileExtension;
- }
- -
- +
- /**
- - * Gets the file extension of class files in the namespace of this class loader.
- + * Gets the file extension of class files in the namespace of this ClassLoader.
- *
- * @return string
- */
- @@ -109,23 +114,23 @@ class ClassLoader
- {
- return $this->_fileExtension;
- }
- -
- +
- /**
- - * Installs this class loader on the SPL autoload stack.
- + * Registers this ClassLoader on the SPL autoload stack.
- */
- public function register()
- {
- spl_autoload_register(array($this, 'loadClass'));
- }
- -
- +
- /**
- - * Uninstalls this class loader on the SPL autoload stack.
- + * Removes this ClassLoader from the SPL autoload stack.
- */
- public function unregister()
- {
- spl_autoload_unregister(array($this, 'loadClass'));
- }
- -
- +
- /**
- * Loads the given class or interface.
- *
- @@ -144,4 +149,69 @@ class ClassLoader
- return true;
- }
- +
- + /**
- + * Asks this ClassLoader whether it can potentially load the class (file) with
- + * the given name.
- + *
- + * @param string $className The fully-qualified name of the class.
- + * @return boolean TRUE if this ClassLoader can load the class, FALSE otherwise.
- + */
- + public function canLoadClass($className)
- + {
- + if ($this->_namespace !== null && strpos($className, $this->_namespace.$this->_namespaceSeparator) !== 0) {
- + return false;
- + }
- + return file_exists(($this->_includePath !== null ? $this->_includePath . DIRECTORY_SEPARATOR : '')
- + . str_replace($this->_namespaceSeparator, DIRECTORY_SEPARATOR, $className)
- + . $this->_fileExtension);
- + }
- +
- + /**
- + * Checks whether a class with a given name exists. A class "exists" if it is either
- + * already defined in the current request or if there is an autoloader on the SPL
- + * autoload stack that is a) responsible for the class in question and b) is able to
- + * load a class file in which the class definition resides.
- + *
- + * If the class is not already defined, each autoloader in the SPL autoload stack
- + * is asked whether it is able to tell if the class exists. If the autoloader is
- + * a <tt>ClassLoader</tt>, {@link canLoadClass} is used, otherwise the autoload
- + * function of the autoloader is invoked and expected to return a value that
- + * evaluates to TRUE if the class (file) exists. As soon as one autoloader reports
- + * that the class exists, TRUE is returned.
- + *
- + * Note that, depending on what kinds of autoloaders are installed on the SPL
- + * autoload stack, the class (file) might already be loaded as a result of checking
- + * for its existence. This is not the case with a <tt>ClassLoader</tt>, who separates
- + * these responsibilities.
- + *
- + * @param string $className The fully-qualified name of the class.
- + * @return boolean TRUE if the class exists as per the definition given above, FALSE otherwise.
- + */
- + public static function classExists($className)
- + {
- + if (class_exists($className, false)) {
- + return true;
- + }
- + foreach (spl_autoload_functions() as $loader) {
- + if (is_array($loader)) { // array(???, ???)
- + if (is_object($loader[0])) {
- + if ($loader[0] instanceof ClassLoader) { // array($obj, 'methodName')
- + if ($loader[0]->canLoadClass($className)) {
- + return true;
- + }
- + } else if ($loader[0]->{$loader[1]}($className)) {
- + return true;
- + }
- + } else if ($loader[0]::$loader[1]($className)) { // array('ClassName', 'methodName')
- + return true;
- + }
- + } else if ($loader instanceof \Closure) {
- + if ($loader($className)) {
- + return true;
- + }
- + }
- + }
- + return false;
- + }
- }
- \ No newline at end of file