DebugClassLoader.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Debug;
  11. /**
  12. * Autoloader checking if the class is really defined in the file found.
  13. *
  14. * The ClassLoader will wrap all registered autoloaders
  15. * and will throw an exception if a file is found but does
  16. * not declare the class.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. * @author Christophe Coevoet <stof@notk.org>
  20. * @author Nicolas Grekas <p@tchwork.com>
  21. */
  22. class DebugClassLoader
  23. {
  24. private $classLoader;
  25. private $isFinder;
  26. private $wasFinder;
  27. private static $caseCheck;
  28. private static $deprecated = array();
  29. private static $php7Reserved = array('int', 'float', 'bool', 'string', 'true', 'false', 'null');
  30. private static $darwinCache = array('/' => array('/', array()));
  31. /**
  32. * Constructor.
  33. *
  34. * @param callable|object $classLoader Passing an object is @deprecated since version 2.5 and support for it will be removed in 3.0
  35. */
  36. public function __construct($classLoader)
  37. {
  38. $this->wasFinder = is_object($classLoader) && method_exists($classLoader, 'findFile');
  39. if ($this->wasFinder) {
  40. @trigger_error('The '.__METHOD__.' method will no longer support receiving an object into its $classLoader argument in 3.0.', E_USER_DEPRECATED);
  41. $this->classLoader = array($classLoader, 'loadClass');
  42. $this->isFinder = true;
  43. } else {
  44. $this->classLoader = $classLoader;
  45. $this->isFinder = is_array($classLoader) && method_exists($classLoader[0], 'findFile');
  46. }
  47. if (!isset(self::$caseCheck)) {
  48. self::$caseCheck = false !== stripos(PHP_OS, 'win') ? (false !== stripos(PHP_OS, 'darwin') ? 2 : 1) : 0;
  49. }
  50. }
  51. /**
  52. * Gets the wrapped class loader.
  53. *
  54. * @return callable|object A class loader. Since version 2.5, returning an object is @deprecated and support for it will be removed in 3.0
  55. */
  56. public function getClassLoader()
  57. {
  58. return $this->wasFinder ? $this->classLoader[0] : $this->classLoader;
  59. }
  60. /**
  61. * Wraps all autoloaders.
  62. */
  63. public static function enable()
  64. {
  65. // Ensures we don't hit https://bugs.php.net/42098
  66. class_exists('Symfony\Component\Debug\ErrorHandler');
  67. class_exists('Psr\Log\LogLevel');
  68. if (!is_array($functions = spl_autoload_functions())) {
  69. return;
  70. }
  71. foreach ($functions as $function) {
  72. spl_autoload_unregister($function);
  73. }
  74. foreach ($functions as $function) {
  75. if (!is_array($function) || !$function[0] instanceof self) {
  76. $function = array(new static($function), 'loadClass');
  77. }
  78. spl_autoload_register($function);
  79. }
  80. }
  81. /**
  82. * Disables the wrapping.
  83. */
  84. public static function disable()
  85. {
  86. if (!is_array($functions = spl_autoload_functions())) {
  87. return;
  88. }
  89. foreach ($functions as $function) {
  90. spl_autoload_unregister($function);
  91. }
  92. foreach ($functions as $function) {
  93. if (is_array($function) && $function[0] instanceof self) {
  94. $function = $function[0]->getClassLoader();
  95. }
  96. spl_autoload_register($function);
  97. }
  98. }
  99. /**
  100. * Finds a file by class name.
  101. *
  102. * @param string $class A class name to resolve to file
  103. *
  104. * @return string|null
  105. *
  106. * @deprecated since version 2.5, to be removed in 3.0.
  107. */
  108. public function findFile($class)
  109. {
  110. @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);
  111. if ($this->wasFinder) {
  112. return $this->classLoader[0]->findFile($class);
  113. }
  114. }
  115. /**
  116. * Loads the given class or interface.
  117. *
  118. * @param string $class The name of the class
  119. *
  120. * @return bool|null True, if loaded
  121. *
  122. * @throws \RuntimeException
  123. */
  124. public function loadClass($class)
  125. {
  126. ErrorHandler::stackErrors();
  127. try {
  128. if ($this->isFinder) {
  129. if ($file = $this->classLoader[0]->findFile($class)) {
  130. require $file;
  131. }
  132. } else {
  133. call_user_func($this->classLoader, $class);
  134. $file = false;
  135. }
  136. } catch (\Exception $e) {
  137. ErrorHandler::unstackErrors();
  138. throw $e;
  139. }
  140. ErrorHandler::unstackErrors();
  141. $exists = class_exists($class, false) || interface_exists($class, false) || (function_exists('trait_exists') && trait_exists($class, false));
  142. if ('\\' === $class[0]) {
  143. $class = substr($class, 1);
  144. }
  145. if ($exists) {
  146. $refl = new \ReflectionClass($class);
  147. $name = $refl->getName();
  148. if ($name !== $class && 0 === strcasecmp($name, $class)) {
  149. throw new \RuntimeException(sprintf('Case mismatch between loaded and declared class names: %s vs %s', $class, $name));
  150. }
  151. if (in_array(strtolower($refl->getShortName()), self::$php7Reserved)) {
  152. @trigger_error(sprintf('%s uses a reserved class name (%s) that will break on PHP 7 and higher', $name, $refl->getShortName()), E_USER_DEPRECATED);
  153. } elseif (preg_match('#\n \* @deprecated (.*?)\r?\n \*(?: @|/$)#s', $refl->getDocComment(), $notice)) {
  154. self::$deprecated[$name] = preg_replace('#\s*\r?\n \* +#', ' ', $notice[1]);
  155. } else {
  156. if (2 > $len = 1 + (strpos($name, '\\', 1 + strpos($name, '\\')) ?: strpos($name, '_'))) {
  157. $len = 0;
  158. $ns = '';
  159. } else {
  160. switch ($ns = substr($name, 0, $len)) {
  161. case 'Symfony\Bridge\\':
  162. case 'Symfony\Bundle\\':
  163. case 'Symfony\Component\\':
  164. $ns = 'Symfony\\';
  165. $len = strlen($ns);
  166. break;
  167. }
  168. }
  169. $parent = $refl->getParentClass();
  170. if (!$parent || strncmp($ns, $parent->name, $len)) {
  171. if ($parent && isset(self::$deprecated[$parent->name]) && strncmp($ns, $parent->name, $len)) {
  172. @trigger_error(sprintf('The %s class extends %s that is deprecated %s', $name, $parent->name, self::$deprecated[$parent->name]), E_USER_DEPRECATED);
  173. }
  174. foreach ($refl->getInterfaceNames() as $interface) {
  175. if (isset(self::$deprecated[$interface]) && strncmp($ns, $interface, $len) && !($parent && $parent->implementsInterface($interface))) {
  176. @trigger_error(sprintf('The %s %s %s that is deprecated %s', $name, $refl->isInterface() ? 'interface extends' : 'class implements', $interface, self::$deprecated[$interface]), E_USER_DEPRECATED);
  177. }
  178. }
  179. }
  180. }
  181. }
  182. if ($file) {
  183. if (!$exists) {
  184. if (false !== strpos($class, '/')) {
  185. throw new \RuntimeException(sprintf('Trying to autoload a class with an invalid name "%s". Be careful that the namespace separator is "\" in PHP, not "/".', $class));
  186. }
  187. throw new \RuntimeException(sprintf('The autoloader expected class "%s" to be defined in file "%s". The file was found but the class was not in it, the class name or namespace probably has a typo.', $class, $file));
  188. }
  189. if (self::$caseCheck) {
  190. $real = explode('\\', $class.strrchr($file, '.'));
  191. $tail = explode(DIRECTORY_SEPARATOR, str_replace('/', DIRECTORY_SEPARATOR, $file));
  192. $i = count($tail) - 1;
  193. $j = count($real) - 1;
  194. while (isset($tail[$i], $real[$j]) && $tail[$i] === $real[$j]) {
  195. --$i;
  196. --$j;
  197. }
  198. array_splice($tail, 0, $i + 1);
  199. }
  200. if (self::$caseCheck && $tail) {
  201. $tail = DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, $tail);
  202. $tailLen = strlen($tail);
  203. $real = $refl->getFileName();
  204. if (2 === self::$caseCheck) {
  205. // realpath() on MacOSX doesn't normalize the case of characters
  206. $i = 1 + strrpos($real, '/');
  207. $file = substr($real, $i);
  208. $real = substr($real, 0, $i);
  209. if (isset(self::$darwinCache[$real])) {
  210. $kDir = $real;
  211. } else {
  212. $kDir = strtolower($real);
  213. if (isset(self::$darwinCache[$kDir])) {
  214. $real = self::$darwinCache[$kDir][0];
  215. } else {
  216. $dir = getcwd();
  217. chdir($real);
  218. $real = getcwd().'/';
  219. chdir($dir);
  220. $dir = $real;
  221. $k = $kDir;
  222. $i = strlen($dir) - 1;
  223. while (!isset(self::$darwinCache[$k])) {
  224. self::$darwinCache[$k] = array($dir, array());
  225. self::$darwinCache[$dir] = &self::$darwinCache[$k];
  226. while ('/' !== $dir[--$i]) {
  227. }
  228. $k = substr($k, 0, ++$i);
  229. $dir = substr($dir, 0, $i--);
  230. }
  231. }
  232. }
  233. $dirFiles = self::$darwinCache[$kDir][1];
  234. if (isset($dirFiles[$file])) {
  235. $kFile = $file;
  236. } else {
  237. $kFile = strtolower($file);
  238. if (!isset($dirFiles[$kFile])) {
  239. foreach (scandir($real, 2) as $f) {
  240. if ('.' !== $f[0]) {
  241. $dirFiles[$f] = $f;
  242. if ($f === $file) {
  243. $kFile = $k = $file;
  244. } elseif ($f !== $k = strtolower($f)) {
  245. $dirFiles[$k] = $f;
  246. }
  247. }
  248. }
  249. self::$darwinCache[$kDir][1] = $dirFiles;
  250. }
  251. }
  252. $real .= $dirFiles[$kFile];
  253. }
  254. if (0 === substr_compare($real, $tail, -$tailLen, $tailLen, true)
  255. && 0 !== substr_compare($real, $tail, -$tailLen, $tailLen, false)
  256. ) {
  257. throw new \RuntimeException(sprintf('Case mismatch between class and real file names: %s vs %s in %s', substr($tail, -$tailLen + 1), substr($real, -$tailLen + 1), substr($real, 0, -$tailLen + 1)));
  258. }
  259. }
  260. return true;
  261. }
  262. }
  263. }