ClassLoader.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. namespace Doctrine\Common;
  20. /**
  21. * A <tt>ClassLoader</tt> is an autoloader for class files that can be
  22. * installed on the SPL autoload stack. It is a class loader that either loads only classes
  23. * of a specific namespace or all namespaces and it is suitable for working together
  24. * with other autoloaders in the SPL autoload stack.
  25. *
  26. * If no include path is configured through the constructor or {@link setIncludePath}, a ClassLoader
  27. * relies on the PHP <code>include_path</code>.
  28. *
  29. * @author Roman Borschel <roman@code-factory.org>
  30. * @since 2.0
  31. */
  32. class ClassLoader
  33. {
  34. /**
  35. * @var string PHP file extension
  36. */
  37. protected $fileExtension = '.php';
  38. /**
  39. * @var string Current namespace
  40. */
  41. protected $namespace;
  42. /**
  43. * @var string Current include path
  44. */
  45. protected $includePath;
  46. /**
  47. * @var string PHP namespace separator
  48. */
  49. protected $namespaceSeparator = '\\';
  50. /**
  51. * Creates a new <tt>ClassLoader</tt> that loads classes of the
  52. * specified namespace from the specified include path.
  53. *
  54. * If no include path is given, the ClassLoader relies on the PHP include_path.
  55. * If neither a namespace nor an include path is given, the ClassLoader will
  56. * be responsible for loading all classes, thereby relying on the PHP include_path.
  57. *
  58. * @param string $ns The namespace of the classes to load.
  59. * @param string $includePath The base include path to use.
  60. */
  61. public function __construct($ns = null, $includePath = null)
  62. {
  63. $this->namespace = $ns;
  64. $this->includePath = $includePath;
  65. }
  66. /**
  67. * Sets the namespace separator used by classes in the namespace of this ClassLoader.
  68. *
  69. * @param string $sep The separator to use.
  70. */
  71. public function setNamespaceSeparator($sep)
  72. {
  73. $this->namespaceSeparator = $sep;
  74. }
  75. /**
  76. * Gets the namespace separator used by classes in the namespace of this ClassLoader.
  77. *
  78. * @return string
  79. */
  80. public function getNamespaceSeparator()
  81. {
  82. return $this->namespaceSeparator;
  83. }
  84. /**
  85. * Sets the base include path for all class files in the namespace of this ClassLoader.
  86. *
  87. * @param string $includePath
  88. */
  89. public function setIncludePath($includePath)
  90. {
  91. $this->includePath = $includePath;
  92. }
  93. /**
  94. * Gets the base include path for all class files in the namespace of this ClassLoader.
  95. *
  96. * @return string
  97. */
  98. public function getIncludePath()
  99. {
  100. return $this->includePath;
  101. }
  102. /**
  103. * Sets the file extension of class files in the namespace of this ClassLoader.
  104. *
  105. * @param string $fileExtension
  106. */
  107. public function setFileExtension($fileExtension)
  108. {
  109. $this->fileExtension = $fileExtension;
  110. }
  111. /**
  112. * Gets the file extension of class files in the namespace of this ClassLoader.
  113. *
  114. * @return string
  115. */
  116. public function getFileExtension()
  117. {
  118. return $this->fileExtension;
  119. }
  120. /**
  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. * Removes this ClassLoader from the SPL autoload stack.
  129. */
  130. public function unregister()
  131. {
  132. spl_autoload_unregister(array($this, 'loadClass'));
  133. }
  134. /**
  135. * Loads the given class or interface.
  136. *
  137. * @param string $classname The name of the class to load.
  138. * @return boolean TRUE if the class has been successfully loaded, FALSE otherwise.
  139. */
  140. public function loadClass($className)
  141. {
  142. if ($this->namespace !== null && strpos($className, $this->namespace.$this->namespaceSeparator) !== 0) {
  143. return false;
  144. }
  145. require ($this->includePath !== null ? $this->includePath . DIRECTORY_SEPARATOR : '')
  146. . str_replace($this->namespaceSeparator, DIRECTORY_SEPARATOR, $className)
  147. . $this->fileExtension;
  148. return true;
  149. }
  150. /**
  151. * Asks this ClassLoader whether it can potentially load the class (file) with
  152. * the given name.
  153. *
  154. * @param string $className The fully-qualified name of the class.
  155. * @return boolean TRUE if this ClassLoader can load the class, FALSE otherwise.
  156. */
  157. public function canLoadClass($className)
  158. {
  159. if ($this->namespace !== null && strpos($className, $this->namespace.$this->namespaceSeparator) !== 0) {
  160. return false;
  161. }
  162. $file = str_replace($this->namespaceSeparator, DIRECTORY_SEPARATOR, $className) . $this->fileExtension;
  163. if ($this->includePath !== null) {
  164. return file_exists($this->includePath . DIRECTORY_SEPARATOR . $file);
  165. }
  166. return (false !== stream_resolve_include_path($file));
  167. }
  168. /**
  169. * Checks whether a class with a given name exists. A class "exists" if it is either
  170. * already defined in the current request or if there is an autoloader on the SPL
  171. * autoload stack that is a) responsible for the class in question and b) is able to
  172. * load a class file in which the class definition resides.
  173. *
  174. * If the class is not already defined, each autoloader in the SPL autoload stack
  175. * is asked whether it is able to tell if the class exists. If the autoloader is
  176. * a <tt>ClassLoader</tt>, {@link canLoadClass} is used, otherwise the autoload
  177. * function of the autoloader is invoked and expected to return a value that
  178. * evaluates to TRUE if the class (file) exists. As soon as one autoloader reports
  179. * that the class exists, TRUE is returned.
  180. *
  181. * Note that, depending on what kinds of autoloaders are installed on the SPL
  182. * autoload stack, the class (file) might already be loaded as a result of checking
  183. * for its existence. This is not the case with a <tt>ClassLoader</tt>, who separates
  184. * these responsibilities.
  185. *
  186. * @param string $className The fully-qualified name of the class.
  187. * @return boolean TRUE if the class exists as per the definition given above, FALSE otherwise.
  188. */
  189. public static function classExists($className)
  190. {
  191. if (class_exists($className, false) || interface_exists($className, false)) {
  192. return true;
  193. }
  194. foreach (spl_autoload_functions() as $loader) {
  195. if (is_array($loader)) { // array(???, ???)
  196. if (is_object($loader[0])) {
  197. if ($loader[0] instanceof ClassLoader) { // array($obj, 'methodName')
  198. if ($loader[0]->canLoadClass($className)) {
  199. return true;
  200. }
  201. } else if ($loader[0]->{$loader[1]}($className)) {
  202. return true;
  203. }
  204. } else if ($loader[0]::$loader[1]($className)) { // array('ClassName', 'methodName')
  205. return true;
  206. }
  207. } else if ($loader instanceof \Closure) { // function($className) {..}
  208. if ($loader($className)) {
  209. return true;
  210. }
  211. } else if (is_string($loader) && $loader($className)) { // "MyClass::loadClass"
  212. return true;
  213. }
  214. }
  215. return class_exists($className, false) || interface_exists($className, false);
  216. }
  217. /**
  218. * Gets the <tt>ClassLoader</tt> from the SPL autoload stack that is responsible
  219. * for (and is able to load) the class with the given name.
  220. *
  221. * @param string $className The name of the class.
  222. * @return The <tt>ClassLoader</tt> for the class or NULL if no such <tt>ClassLoader</tt> exists.
  223. */
  224. public static function getClassLoader($className)
  225. {
  226. foreach (spl_autoload_functions() as $loader) {
  227. if (is_array($loader)
  228. && $loader[0] instanceof ClassLoader
  229. && $loader[0]->canLoadClass($className)
  230. ) {
  231. return $loader[0];
  232. }
  233. }
  234. return null;
  235. }
  236. }