Drupal investigation

DebugClassLoader.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. $file = file_exists(__FILE__) ? __FILE__ : rtrim(realpath('.'), DIRECTORY_SEPARATOR);
  49. $i = strrpos($file, DIRECTORY_SEPARATOR);
  50. $dir = substr($file, 0, 1 + $i);
  51. $file = substr($file, 1 + $i);
  52. $test = strtoupper($file) === $file ? strtolower($file) : strtoupper($file);
  53. $test = realpath($dir.$test);
  54. if (false === $test || false === $i) {
  55. // filesystem is case sensitive
  56. self::$caseCheck = 0;
  57. } elseif (substr($test, -strlen($file)) === $file) {
  58. // filesystem is case insensitive and realpath() normalizes the case of characters
  59. self::$caseCheck = 1;
  60. } elseif (false !== stripos(PHP_OS, 'darwin')) {
  61. // on MacOSX, HFS+ is case insensitive but realpath() doesn't normalize the case of characters
  62. self::$caseCheck = 2;
  63. } else {
  64. // filesystem case checks failed, fallback to disabling them
  65. self::$caseCheck = 0;
  66. }
  67. }
  68. }
  69. /**
  70. * Gets the wrapped class loader.
  71. *
  72. * @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
  73. */
  74. public function getClassLoader()
  75. {
  76. return $this->wasFinder ? $this->classLoader[0] : $this->classLoader;
  77. }
  78. /**
  79. * Wraps all autoloaders.
  80. */
  81. public static function enable()
  82. {
  83. // Ensures we don't hit https://bugs.php.net/42098
  84. class_exists('Symfony\Component\Debug\ErrorHandler');
  85. class_exists('Psr\Log\LogLevel');
  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 = array(new static($function), 'loadClass');
  95. }
  96. spl_autoload_register($function);
  97. }
  98. }
  99. /**
  100. * Disables the wrapping.
  101. */
  102. public static function disable()
  103. {
  104. if (!is_array($functions = spl_autoload_functions())) {
  105. return;
  106. }
  107. foreach ($functions as $function) {
  108. spl_autoload_unregister($function);
  109. }
  110. foreach ($functions as $function) {
  111. if (is_array($function) && $function[0] instanceof self) {
  112. $function = $function[0]->getClassLoader();
  113. }
  114. spl_autoload_register($function);
  115. }
  116. }
  117. /**
  118. * Finds a file by class name.
  119. *
  120. * @param string $class A class name to resolve to file
  121. *
  122. * @return string|null
  123. *
  124. * @deprecated since version 2.5, to be removed in 3.0.
  125. */
  126. public function findFile($class)
  127. {
  128. @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);
  129. if ($this->wasFinder) {
  130. return $this->classLoader[0]->findFile($class);
  131. }
  132. }
  133. /**
  134. * Loads the given class or interface.
  135. *
  136. * @param string $class The name of the class
  137. *
  138. * @return bool|null True, if loaded
  139. *
  140. * @throws \RuntimeException
  141. */
  142. public function loadClass($class)
  143. {
  144. ErrorHandler::stackErrors();
  145. try {
  146. if ($this->isFinder) {
  147. if ($file = $this->classLoader[0]->findFile($class)) {
  148. require_once $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[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, '\\', 1 + strpos($name, '\\')) ?: strpos($name, '_'))) {
  178. $len = 0;
  179. $ns = '';
  180. } else {
  181. switch ($ns = substr($name, 0, $len)) {
  182. case 'Symfony\Bridge\\':
  183. case 'Symfony\Bundle\\':
  184. case 'Symfony\Component\\':
  185. $ns = 'Symfony\\';
  186. $len = strlen($ns);
  187. break;
  188. }
  189. }
  190. $parent = get_parent_class($class);
  191. if (!$parent || strncmp($ns, $parent, $len)) {
  192. if ($parent && isset(self::$deprecated[$parent]) && strncmp($ns, $parent, $len)) {
  193. @trigger_error(sprintf('The %s class extends %s that is deprecated %s', $name, $parent, self::$deprecated[$parent]), E_USER_DEPRECATED);
  194. }
  195. $parentInterfaces = array();
  196. $deprecatedInterfaces = array();
  197. if ($parent) {
  198. foreach (class_implements($parent) as $interface) {
  199. $parentInterfaces[$interface] = 1;
  200. }
  201. }
  202. foreach ($refl->getInterfaceNames() as $interface) {
  203. if (isset(self::$deprecated[$interface]) && strncmp($ns, $interface, $len)) {
  204. $deprecatedInterfaces[] = $interface;
  205. }
  206. foreach (class_implements($interface) as $interface) {
  207. $parentInterfaces[$interface] = 1;
  208. }
  209. }
  210. foreach ($deprecatedInterfaces as $interface) {
  211. if (!isset($parentInterfaces[$interface])) {
  212. @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);
  213. }
  214. }
  215. }
  216. }
  217. }
  218. if ($file) {
  219. if (!$exists) {
  220. if (false !== strpos($class, '/')) {
  221. 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));
  222. }
  223. 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));
  224. }
  225. if (self::$caseCheck) {
  226. $real = explode('\\', $class.strrchr($file, '.'));
  227. $tail = explode(DIRECTORY_SEPARATOR, str_replace('/', DIRECTORY_SEPARATOR, $file));
  228. $i = count($tail) - 1;
  229. $j = count($real) - 1;
  230. while (isset($tail[$i], $real[$j]) && $tail[$i] === $real[$j]) {
  231. --$i;
  232. --$j;
  233. }
  234. array_splice($tail, 0, $i + 1);
  235. }
  236. if (self::$caseCheck && $tail) {
  237. $tail = DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, $tail);
  238. $tailLen = strlen($tail);
  239. $real = $refl->getFileName();
  240. if (2 === self::$caseCheck) {
  241. // realpath() on MacOSX doesn't normalize the case of characters
  242. $i = 1 + strrpos($real, '/');
  243. $file = substr($real, $i);
  244. $real = substr($real, 0, $i);
  245. if (isset(self::$darwinCache[$real])) {
  246. $kDir = $real;
  247. } else {
  248. $kDir = strtolower($real);
  249. if (isset(self::$darwinCache[$kDir])) {
  250. $real = self::$darwinCache[$kDir][0];
  251. } else {
  252. $dir = getcwd();
  253. chdir($real);
  254. $real = getcwd().'/';
  255. chdir($dir);
  256. $dir = $real;
  257. $k = $kDir;
  258. $i = strlen($dir) - 1;
  259. while (!isset(self::$darwinCache[$k])) {
  260. self::$darwinCache[$k] = array($dir, array());
  261. self::$darwinCache[$dir] = &self::$darwinCache[$k];
  262. while ('/' !== $dir[--$i]) {
  263. }
  264. $k = substr($k, 0, ++$i);
  265. $dir = substr($dir, 0, $i--);
  266. }
  267. }
  268. }
  269. $dirFiles = self::$darwinCache[$kDir][1];
  270. if (isset($dirFiles[$file])) {
  271. $kFile = $file;
  272. } else {
  273. $kFile = strtolower($file);
  274. if (!isset($dirFiles[$kFile])) {
  275. foreach (scandir($real, 2) as $f) {
  276. if ('.' !== $f[0]) {
  277. $dirFiles[$f] = $f;
  278. if ($f === $file) {
  279. $kFile = $k = $file;
  280. } elseif ($f !== $k = strtolower($f)) {
  281. $dirFiles[$k] = $f;
  282. }
  283. }
  284. }
  285. self::$darwinCache[$kDir][1] = $dirFiles;
  286. }
  287. }
  288. $real .= $dirFiles[$kFile];
  289. }
  290. if (0 === substr_compare($real, $tail, -$tailLen, $tailLen, true)
  291. && 0 !== substr_compare($real, $tail, -$tailLen, $tailLen, false)
  292. ) {
  293. 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)));
  294. }
  295. }
  296. return true;
  297. }
  298. }
  299. }