Drupal investigation

ClassLoader.php 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. * @deprecated the ClassLoader is deprecated and will be removed in version 3.0 of doctrine/common.
  33. */
  34. class ClassLoader
  35. {
  36. /**
  37. * PHP file extension.
  38. *
  39. * @var string
  40. */
  41. protected $fileExtension = '.php';
  42. /**
  43. * Current namespace.
  44. *
  45. * @var string|null
  46. */
  47. protected $namespace;
  48. /**
  49. * Current include path.
  50. *
  51. * @var string|null
  52. */
  53. protected $includePath;
  54. /**
  55. * PHP namespace separator.
  56. *
  57. * @var string
  58. */
  59. protected $namespaceSeparator = '\\';
  60. /**
  61. * Creates a new <tt>ClassLoader</tt> that loads classes of the
  62. * specified namespace from the specified include path.
  63. *
  64. * If no include path is given, the ClassLoader relies on the PHP include_path.
  65. * If neither a namespace nor an include path is given, the ClassLoader will
  66. * be responsible for loading all classes, thereby relying on the PHP include_path.
  67. *
  68. * @param string|null $ns The namespace of the classes to load.
  69. * @param string|null $includePath The base include path to use.
  70. */
  71. public function __construct($ns = null, $includePath = null)
  72. {
  73. $this->namespace = $ns;
  74. $this->includePath = $includePath;
  75. }
  76. /**
  77. * Sets the namespace separator used by classes in the namespace of this ClassLoader.
  78. *
  79. * @param string $sep The separator to use.
  80. *
  81. * @return void
  82. */
  83. public function setNamespaceSeparator($sep)
  84. {
  85. $this->namespaceSeparator = $sep;
  86. }
  87. /**
  88. * Gets the namespace separator used by classes in the namespace of this ClassLoader.
  89. *
  90. * @return string
  91. */
  92. public function getNamespaceSeparator()
  93. {
  94. return $this->namespaceSeparator;
  95. }
  96. /**
  97. * Sets the base include path for all class files in the namespace of this ClassLoader.
  98. *
  99. * @param string|null $includePath
  100. *
  101. * @return void
  102. */
  103. public function setIncludePath($includePath)
  104. {
  105. $this->includePath = $includePath;
  106. }
  107. /**
  108. * Gets the base include path for all class files in the namespace of this ClassLoader.
  109. *
  110. * @return string|null
  111. */
  112. public function getIncludePath()
  113. {
  114. return $this->includePath;
  115. }
  116. /**
  117. * Sets the file extension of class files in the namespace of this ClassLoader.
  118. *
  119. * @param string $fileExtension
  120. *
  121. * @return void
  122. */
  123. public function setFileExtension($fileExtension)
  124. {
  125. $this->fileExtension = $fileExtension;
  126. }
  127. /**
  128. * Gets the file extension of class files in the namespace of this ClassLoader.
  129. *
  130. * @return string
  131. */
  132. public function getFileExtension()
  133. {
  134. return $this->fileExtension;
  135. }
  136. /**
  137. * Registers this ClassLoader on the SPL autoload stack.
  138. *
  139. * @return void
  140. */
  141. public function register()
  142. {
  143. spl_autoload_register([$this, 'loadClass']);
  144. }
  145. /**
  146. * Removes this ClassLoader from the SPL autoload stack.
  147. *
  148. * @return void
  149. */
  150. public function unregister()
  151. {
  152. spl_autoload_unregister([$this, 'loadClass']);
  153. }
  154. /**
  155. * Loads the given class or interface.
  156. *
  157. * @param string $className The name of the class to load.
  158. *
  159. * @return boolean TRUE if the class has been successfully loaded, FALSE otherwise.
  160. */
  161. public function loadClass($className)
  162. {
  163. if (self::typeExists($className)) {
  164. return true;
  165. }
  166. if (! $this->canLoadClass($className)) {
  167. return false;
  168. }
  169. require ($this->includePath !== null ? $this->includePath . DIRECTORY_SEPARATOR : '')
  170. . str_replace($this->namespaceSeparator, DIRECTORY_SEPARATOR, $className)
  171. . $this->fileExtension;
  172. return self::typeExists($className);
  173. }
  174. /**
  175. * Asks this ClassLoader whether it can potentially load the class (file) with
  176. * the given name.
  177. *
  178. * @param string $className The fully-qualified name of the class.
  179. *
  180. * @return boolean TRUE if this ClassLoader can load the class, FALSE otherwise.
  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 (false !== stream_resolve_include_path($file));
  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 boolean TRUE if the class exists as per the definition given above, FALSE otherwise.
  214. */
  215. public static function classExists($className)
  216. {
  217. return self::typeExists($className, true);
  218. }
  219. /**
  220. * Gets the <tt>ClassLoader</tt> from the SPL autoload stack that is responsible
  221. * for (and is able to load) the class with the given name.
  222. *
  223. * @param string $className The name of the class.
  224. *
  225. * @return ClassLoader The <tt>ClassLoader</tt> for the class or NULL if no such <tt>ClassLoader</tt> exists.
  226. */
  227. public static function getClassLoader($className)
  228. {
  229. foreach (spl_autoload_functions() as $loader) {
  230. if (is_array($loader)
  231. && ($classLoader = reset($loader))
  232. && $classLoader instanceof ClassLoader
  233. && $classLoader->canLoadClass($className)
  234. ) {
  235. return $classLoader;
  236. }
  237. }
  238. return null;
  239. }
  240. /**
  241. * Checks whether a given type exists
  242. *
  243. * @param string $type
  244. * @param bool $autoload
  245. *
  246. * @return bool
  247. */
  248. private static function typeExists($type, $autoload = false)
  249. {
  250. return class_exists($type, $autoload)
  251. || interface_exists($type, $autoload)
  252. || trait_exists($type, $autoload);
  253. }
  254. }