ClassLoader.php 7.8 KB

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