ClassLoader.php 9.3 KB

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