ClassLoader.php 9.0KB

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