ClassLoader.php 8.3 KB

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