DebugClassLoader.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 $loaded = array();
  27. private $wasFinder;
  28. private static $caseCheck;
  29. private static $deprecated = array();
  30. private static $php7Reserved = array('int', 'float', 'bool', 'string', 'true', 'false', 'null');
  31. private static $darwinCache = array('/' => array('/', array()));
  32. /**
  33. * @param callable|object $classLoader Passing an object is @deprecated since version 2.5 and support for it will be removed in 3.0
  34. */
  35. public function __construct($classLoader)
  36. {
  37. $this->wasFinder = \is_object($classLoader) && method_exists($classLoader, 'findFile');
  38. if ($this->wasFinder) {
  39. @trigger_error('The '.__METHOD__.' method will no longer support receiving an object into its $classLoader argument in 3.0.', E_USER_DEPRECATED);
  40. $this->classLoader = array($classLoader, 'loadClass');
  41. $this->isFinder = true;
  42. } else {
  43. $this->classLoader = $classLoader;
  44. $this->isFinder = \is_array($classLoader) && method_exists($classLoader[0], 'findFile');
  45. }
  46. if (!isset(self::$caseCheck)) {
  47. $file = file_exists(__FILE__) ? __FILE__ : rtrim(realpath('.'), \DIRECTORY_SEPARATOR);
  48. $i = strrpos($file, \DIRECTORY_SEPARATOR);
  49. $dir = substr($file, 0, 1 + $i);
  50. $file = substr($file, 1 + $i);
  51. $test = strtoupper($file) === $file ? strtolower($file) : strtoupper($file);
  52. $test = realpath($dir.$test);
  53. if (false === $test || false === $i) {
  54. // filesystem is case sensitive
  55. self::$caseCheck = 0;
  56. } elseif (substr($test, -\strlen($file)) === $file) {
  57. // filesystem is case insensitive and realpath() normalizes the case of characters
  58. self::$caseCheck = 1;
  59. } elseif (false !== stripos(PHP_OS, 'darwin')) {
  60. // on MacOSX, HFS+ is case insensitive but realpath() doesn't normalize the case of characters
  61. self::$caseCheck = 2;
  62. } else {
  63. // filesystem case checks failed, fallback to disabling them
  64. self::$caseCheck = 0;
  65. }
  66. }
  67. }
  68. /**
  69. * Gets the wrapped class loader.
  70. *
  71. * @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
  72. */
  73. public function getClassLoader()
  74. {
  75. return $this->wasFinder ? $this->classLoader[0] : $this->classLoader;
  76. }
  77. /**
  78. * Wraps all autoloaders.
  79. */
  80. public static function enable()
  81. {
  82. // Ensures we don't hit https://bugs.php.net/42098
  83. class_exists('Symfony\Component\Debug\ErrorHandler');
  84. class_exists('Psr\Log\LogLevel');
  85. if (!\is_array($functions = spl_autoload_functions())) {
  86. return;
  87. }
  88. foreach ($functions as $function) {
  89. spl_autoload_unregister($function);
  90. }
  91. foreach ($functions as $function) {
  92. if (!\is_array($function) || !$function[0] instanceof self) {
  93. $function = array(new static($function), 'loadClass');
  94. }
  95. spl_autoload_register($function);
  96. }
  97. }
  98. /**
  99. * Disables the wrapping.
  100. */
  101. public static function disable()
  102. {
  103. if (!\is_array($functions = spl_autoload_functions())) {
  104. return;
  105. }
  106. foreach ($functions as $function) {
  107. spl_autoload_unregister($function);
  108. }
  109. foreach ($functions as $function) {
  110. if (\is_array($function) && $function[0] instanceof self) {
  111. $function = $function[0]->getClassLoader();
  112. }
  113. spl_autoload_register($function);
  114. }
  115. }
  116. /**
  117. * Finds a file by class name.
  118. *
  119. * @param string $class A class name to resolve to file
  120. *
  121. * @return string|null
  122. *
  123. * @deprecated since version 2.5, to be removed in 3.0.
  124. */
  125. public function findFile($class)
  126. {
  127. @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);
  128. if ($this->wasFinder) {
  129. return $this->classLoader[0]->findFile($class);
  130. }
  131. }
  132. /**
  133. * Loads the given class or interface.
  134. *
  135. * @param string $class The name of the class
  136. *
  137. * @return bool|null True, if loaded
  138. *
  139. * @throws \RuntimeException
  140. */
  141. public function loadClass($class)
  142. {
  143. ErrorHandler::stackErrors();
  144. try {
  145. if ($this->isFinder && !isset($this->loaded[$class])) {
  146. $this->loaded[$class] = true;
  147. if ($file = $this->classLoader[0]->findFile($class)) {
  148. require $file;
  149. }
  150. } else {
  151. \call_user_func($this->classLoader, $class);
  152. $file = false;
  153. }
  154. } catch (\Exception $e) {
  155. ErrorHandler::unstackErrors();
  156. throw $e;
  157. } catch (\Throwable $e) {
  158. ErrorHandler::unstackErrors();
  159. throw $e;
  160. }
  161. ErrorHandler::unstackErrors();
  162. $exists = class_exists($class, false) || interface_exists($class, false) || (\function_exists('trait_exists') && trait_exists($class, false));
  163. if ($class && '\\' === $class[0]) {
  164. $class = substr($class, 1);
  165. }
  166. if ($exists) {
  167. $refl = new \ReflectionClass($class);
  168. $name = $refl->getName();
  169. if ($name !== $class && 0 === strcasecmp($name, $class)) {
  170. throw new \RuntimeException(sprintf('Case mismatch between loaded and declared class names: %s vs %s', $class, $name));
  171. }
  172. if (\in_array(strtolower($refl->getShortName()), self::$php7Reserved)) {
  173. @trigger_error(sprintf('%s uses a reserved class name (%s) that will break on PHP 7 and higher', $name, $refl->getShortName()), E_USER_DEPRECATED);
  174. } elseif (preg_match('#\n \* @deprecated (.*?)\r?\n \*(?: @|/$)#s', $refl->getDocComment(), $notice)) {
  175. self::$deprecated[$name] = preg_replace('#\s*\r?\n \* +#', ' ', $notice[1]);
  176. } else {
  177. if (2 > $len = 1 + (strpos($name, '\\') ?: strpos($name, '_'))) {
  178. $len = 0;
  179. $ns = '';
  180. } else {
  181. $ns = substr($name, 0, $len);
  182. }
  183. $parent = get_parent_class($class);
  184. if (!$parent || strncmp($ns, $parent, $len)) {
  185. if ($parent && isset(self::$deprecated[$parent]) && strncmp($ns, $parent, $len)) {
  186. @trigger_error(sprintf('The %s class extends %s that is deprecated %s', $name, $parent, self::$deprecated[$parent]), E_USER_DEPRECATED);
  187. }
  188. $parentInterfaces = array();
  189. $deprecatedInterfaces = array();
  190. if ($parent) {
  191. foreach (class_implements($parent) as $interface) {
  192. $parentInterfaces[$interface] = 1;
  193. }
  194. }
  195. foreach ($refl->getInterfaceNames() as $interface) {
  196. if (isset(self::$deprecated[$interface]) && strncmp($ns, $interface, $len)) {
  197. $deprecatedInterfaces[] = $interface;
  198. }
  199. foreach (class_implements($interface) as $interface) {
  200. $parentInterfaces[$interface] = 1;
  201. }
  202. }
  203. foreach ($deprecatedInterfaces as $interface) {
  204. if (!isset($parentInterfaces[$interface])) {
  205. @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);
  206. }
  207. }
  208. }
  209. }
  210. }
  211. if ($file) {
  212. if (!$exists) {
  213. if (false !== strpos($class, '/')) {
  214. 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));
  215. }
  216. 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));
  217. }
  218. if (self::$caseCheck) {
  219. $real = explode('\\', $class.strrchr($file, '.'));
  220. $tail = explode(\DIRECTORY_SEPARATOR, str_replace('/', \DIRECTORY_SEPARATOR, $file));
  221. $i = \count($tail) - 1;
  222. $j = \count($real) - 1;
  223. while (isset($tail[$i], $real[$j]) && $tail[$i] === $real[$j]) {
  224. --$i;
  225. --$j;
  226. }
  227. array_splice($tail, 0, $i + 1);
  228. }
  229. if (self::$caseCheck && $tail) {
  230. $tail = \DIRECTORY_SEPARATOR.implode(\DIRECTORY_SEPARATOR, $tail);
  231. $tailLen = \strlen($tail);
  232. $real = $refl->getFileName();
  233. if (2 === self::$caseCheck) {
  234. // realpath() on MacOSX doesn't normalize the case of characters
  235. $i = 1 + strrpos($real, '/');
  236. $file = substr($real, $i);
  237. $real = substr($real, 0, $i);
  238. if (isset(self::$darwinCache[$real])) {
  239. $kDir = $real;
  240. } else {
  241. $kDir = strtolower($real);
  242. if (isset(self::$darwinCache[$kDir])) {
  243. $real = self::$darwinCache[$kDir][0];
  244. } else {
  245. $dir = getcwd();
  246. chdir($real);
  247. $real = getcwd().'/';
  248. chdir($dir);
  249. $dir = $real;
  250. $k = $kDir;
  251. $i = \strlen($dir) - 1;
  252. while (!isset(self::$darwinCache[$k])) {
  253. self::$darwinCache[$k] = array($dir, array());
  254. self::$darwinCache[$dir] = &self::$darwinCache[$k];
  255. while ('/' !== $dir[--$i]) {
  256. }
  257. $k = substr($k, 0, ++$i);
  258. $dir = substr($dir, 0, $i--);
  259. }
  260. }
  261. }
  262. $dirFiles = self::$darwinCache[$kDir][1];
  263. if (isset($dirFiles[$file])) {
  264. $kFile = $file;
  265. } else {
  266. $kFile = strtolower($file);
  267. if (!isset($dirFiles[$kFile])) {
  268. foreach (scandir($real, 2) as $f) {
  269. if ('.' !== $f[0]) {
  270. $dirFiles[$f] = $f;
  271. if ($f === $file) {
  272. $kFile = $k = $file;
  273. } elseif ($f !== $k = strtolower($f)) {
  274. $dirFiles[$k] = $f;
  275. }
  276. }
  277. }
  278. self::$darwinCache[$kDir][1] = $dirFiles;
  279. }
  280. }
  281. $real .= $dirFiles[$kFile];
  282. }
  283. if (0 === substr_compare($real, $tail, -$tailLen, $tailLen, true)
  284. && 0 !== substr_compare($real, $tail, -$tailLen, $tailLen, false)
  285. ) {
  286. 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)));
  287. }
  288. }
  289. return true;
  290. }
  291. }
  292. }