Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 7.77 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. diff --git a/lib/Doctrine/Common/ClassLoader.php b/lib/Doctrine/Common/ClassLoader.php
  2. index d971fe9..507131e 100644
  3. --- a/lib/Doctrine/Common/ClassLoader.php
  4. +++ b/lib/Doctrine/Common/ClassLoader.php
  5. @@ -21,12 +21,12 @@ namespace Doctrine\Common;
  6.  
  7.  /**
  8.   * A <tt>ClassLoader</tt> is an autoloader for class files that can be
  9. - * installed on the SPL autoload stack. It is a class loader that loads only classes
  10. - * of a specific namespace or all namespaces and is suitable for working together
  11. + * installed on the SPL autoload stack. It is a class loader that either loads only classes
  12. + * of a specific namespace or all namespaces and it is suitable for working together
  13.   * with other autoloaders in the SPL autoload stack.
  14.   *
  15.   * If no include path is configured through {@link setIncludePath}, a ClassLoader
  16. - * relies on the PHP include_path.
  17. + * relies on the PHP <code>include_path</code>.
  18.   *
  19.   * @author Roman Borschel <roman@code-factory.org>
  20.   * @since 2.0
  21. @@ -37,21 +37,26 @@ class ClassLoader
  22.      private $_namespace;
  23.      private $_includePath;
  24.      private $_namespaceSeparator = '\\';
  25. -    
  26. +
  27.      /**
  28.       * Creates a new <tt>ClassLoader</tt> that loads classes of the
  29. -     * specified namespace.
  30. +     * specified namespace from the specified include path.
  31. +     *
  32. +     * If no include path is given, the ClassLoader relies on the PHP include_path.
  33. +     * If neither a namespace nor an include path is given, the ClassLoader will
  34. +     * be responsible for loading all classes, thereby relying on the PHP include_path.
  35.       *
  36. -     * @param string $ns The namespace to use.
  37. +     * @param string $ns The namespace of the classes to load.
  38. +     * @param string $includePath The base include path to use.
  39.       */
  40.      public function __construct($ns = null, $includePath = null)
  41.      {
  42.          $this->_namespace = $ns;
  43.          $this->_includePath = $includePath;
  44.      }
  45. -    
  46. +
  47.      /**
  48. -     * Sets the namespace separator used by classes in the namespace of this class loader.
  49. +     * Sets the namespace separator used by classes in the namespace of this ClassLoader.
  50.       *
  51.       * @param string $sep The separator to use.
  52.       */
  53. @@ -59,9 +64,9 @@ class ClassLoader
  54.      {
  55.          $this->_namespaceSeparator = $sep;
  56.      }
  57. -    
  58. +
  59.      /**
  60. -     * Gets the namespace separator used by classes in the namespace of this class loader.
  61. +     * Gets the namespace separator used by classes in the namespace of this ClassLoader.
  62.       *
  63.       * @return string
  64.       */
  65. @@ -69,9 +74,9 @@ class ClassLoader
  66.      {
  67.          return $this->_namespaceSeparator;
  68.      }
  69. -    
  70. +
  71.      /**
  72. -     * Sets the base include path for all class files in the namespace of this class loader.
  73. +     * Sets the base include path for all class files in the namespace of this ClassLoader.
  74.       *
  75.       * @param string $includePath
  76.       */
  77. @@ -79,9 +84,9 @@ class ClassLoader
  78.      {
  79.          $this->_includePath = $includePath;
  80.      }
  81. -    
  82. +
  83.      /**
  84. -     * Gets the base include path for all class files in the namespace of this class loader.
  85. +     * Gets the base include path for all class files in the namespace of this ClassLoader.
  86.       *
  87.       * @return string
  88.       */
  89. @@ -89,9 +94,9 @@ class ClassLoader
  90.      {
  91.          return $this->_includePath;
  92.      }
  93. -    
  94. +
  95.      /**
  96. -     * Sets the file extension of class files in the namespace of this class loader.
  97. +     * Sets the file extension of class files in the namespace of this ClassLoader.
  98.       *
  99.       * @param string $fileExtension
  100.       */
  101. @@ -99,9 +104,9 @@ class ClassLoader
  102.      {
  103.          $this->_fileExtension = $fileExtension;
  104.      }
  105. -    
  106. +
  107.      /**
  108. -     * Gets the file extension of class files in the namespace of this class loader.
  109. +     * Gets the file extension of class files in the namespace of this ClassLoader.
  110.       *
  111.       * @return string
  112.       */
  113. @@ -109,23 +114,23 @@ class ClassLoader
  114.      {
  115.          return $this->_fileExtension;
  116.      }
  117. -    
  118. +
  119.      /**
  120. -     * Installs this class loader on the SPL autoload stack.
  121. +     * Registers this ClassLoader on the SPL autoload stack.
  122.       */
  123.      public function register()
  124.      {
  125.          spl_autoload_register(array($this, 'loadClass'));
  126.      }
  127. -    
  128. +
  129.      /**
  130. -     * Uninstalls this class loader on the SPL autoload stack.
  131. +     * Removes this ClassLoader from the SPL autoload stack.
  132.       */
  133.      public function unregister()
  134.      {
  135.          spl_autoload_unregister(array($this, 'loadClass'));
  136.      }
  137. -    
  138. +
  139.      /**
  140.       * Loads the given class or interface.
  141.       *
  142. @@ -144,4 +149,69 @@ class ClassLoader
  143.          
  144.          return true;
  145.      }
  146. +
  147. +    /**
  148. +     * Asks this ClassLoader whether it can potentially load the class (file) with
  149. +     * the given name.
  150. +     *
  151. +     * @param string $className The fully-qualified name of the class.
  152. +     * @return boolean TRUE if this ClassLoader can load the class, FALSE otherwise.
  153. +     */
  154. +    public function canLoadClass($className)
  155. +    {
  156. +        if ($this->_namespace !== null && strpos($className, $this->_namespace.$this->_namespaceSeparator) !== 0) {
  157. +            return false;
  158. +        }
  159. +        return file_exists(($this->_includePath !== null ? $this->_includePath . DIRECTORY_SEPARATOR : '')
  160. +               . str_replace($this->_namespaceSeparator, DIRECTORY_SEPARATOR, $className)
  161. +               . $this->_fileExtension);
  162. +    }
  163. +
  164. +    /**
  165. +     * Checks whether a class with a given name exists. A class "exists" if it is either
  166. +     * already defined in the current request or if there is an autoloader on the SPL
  167. +     * autoload stack that is a) responsible for the class in question and b) is able to
  168. +     * load a class file in which the class definition resides.
  169. +     *
  170. +     * If the class is not already defined, each autoloader in the SPL autoload stack
  171. +     * is asked whether it is able to tell if the class exists. If the autoloader is
  172. +     * a <tt>ClassLoader</tt>, {@link canLoadClass} is used, otherwise the autoload
  173. +     * function of the autoloader is invoked and expected to return a value that
  174. +     * evaluates to TRUE if the class (file) exists. As soon as one autoloader reports
  175. +     * that the class exists, TRUE is returned.
  176. +     *
  177. +     * Note that, depending on what kinds of autoloaders are installed on the SPL
  178. +     * autoload stack, the class (file) might already be loaded as a result of checking
  179. +     * for its existence. This is not the case with a <tt>ClassLoader</tt>, who separates
  180. +     * these responsibilities.
  181. +     *
  182. +     * @param string $className The fully-qualified name of the class.
  183. +     * @return boolean TRUE if the class exists as per the definition given above, FALSE otherwise.
  184. +     */
  185. +    public static function classExists($className)
  186. +    {
  187. +        if (class_exists($className, false)) {
  188. +            return true;
  189. +        }
  190. +        foreach (spl_autoload_functions() as $loader) {
  191. +            if (is_array($loader)) { // array(???, ???)
  192. +                if (is_object($loader[0])) {
  193. +                    if ($loader[0] instanceof ClassLoader) { // array($obj, 'methodName')
  194. +                        if ($loader[0]->canLoadClass($className)) {
  195. +                            return true;
  196. +                        }
  197. +                    } else if ($loader[0]->{$loader[1]}($className)) {
  198. +                        return true;
  199. +                    }
  200. +                } else if ($loader[0]::$loader[1]($className)) { // array('ClassName', 'methodName')
  201. +                    return true;
  202. +                }
  203. +            } else if ($loader instanceof \Closure) {
  204. +                if ($loader($className)) {
  205. +                    return true;
  206. +                }
  207. +            }
  208. +        }
  209. +        return false;
  210. +    }
  211.  }
  212. \ No newline at end of file