PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/debug/DebugClassLoader.php

https://gitlab.com/Pasantias/pasantiasASLG
PHP | 328 lines | 230 code | 40 blank | 58 comment | 51 complexity | 67bb8ae10df10bfb2695565390335ccb MD5 | raw file
  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. }
  158. ErrorHandler::unstackErrors();
  159. $exists = class_exists($class, false) || interface_exists($class, false) || (function_exists('trait_exists') && trait_exists($class, false));
  160. if ('\\' === $class[0]) {
  161. $class = substr($class, 1);
  162. }
  163. if ($exists) {
  164. $refl = new \ReflectionClass($class);
  165. $name = $refl->getName();
  166. if ($name !== $class && 0 === strcasecmp($name, $class)) {
  167. throw new \RuntimeException(sprintf('Case mismatch between loaded and declared class names: %s vs %s', $class, $name));
  168. }
  169. if (in_array(strtolower($refl->getShortName()), self::$php7Reserved)) {
  170. @trigger_error(sprintf('%s uses a reserved class name (%s) that will break on PHP 7 and higher', $name, $refl->getShortName()), E_USER_DEPRECATED);
  171. } elseif (preg_match('#\n \* @deprecated (.*?)\r?\n \*(?: @|/$)#s', $refl->getDocComment(), $notice)) {
  172. self::$deprecated[$name] = preg_replace('#\s*\r?\n \* +#', ' ', $notice[1]);
  173. } else {
  174. if (2 > $len = 1 + (strpos($name, '\\', 1 + strpos($name, '\\')) ?: strpos($name, '_'))) {
  175. $len = 0;
  176. $ns = '';
  177. } else {
  178. switch ($ns = substr($name, 0, $len)) {
  179. case 'Symfony\Bridge\\':
  180. case 'Symfony\Bundle\\':
  181. case 'Symfony\Component\\':
  182. $ns = 'Symfony\\';
  183. $len = strlen($ns);
  184. break;
  185. }
  186. }
  187. $parent = get_parent_class($class);
  188. if (!$parent || strncmp($ns, $parent, $len)) {
  189. if ($parent && isset(self::$deprecated[$parent]) && strncmp($ns, $parent, $len)) {
  190. @trigger_error(sprintf('The %s class extends %s that is deprecated %s', $name, $parent, self::$deprecated[$parent]), E_USER_DEPRECATED);
  191. }
  192. foreach (class_implements($class) as $interface) {
  193. if (isset(self::$deprecated[$interface]) && strncmp($ns, $interface, $len) && !is_subclass_of($parent, $interface)) {
  194. @trigger_error(sprintf('The %s %s %s that is deprecated %s', $name, interface_exists($class) ? 'interface extends' : 'class implements', $interface, self::$deprecated[$interface]), E_USER_DEPRECATED);
  195. }
  196. }
  197. }
  198. }
  199. }
  200. if ($file) {
  201. if (!$exists) {
  202. if (false !== strpos($class, '/')) {
  203. 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));
  204. }
  205. 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));
  206. }
  207. if (self::$caseCheck) {
  208. $real = explode('\\', $class.strrchr($file, '.'));
  209. $tail = explode(DIRECTORY_SEPARATOR, str_replace('/', DIRECTORY_SEPARATOR, $file));
  210. $i = count($tail) - 1;
  211. $j = count($real) - 1;
  212. while (isset($tail[$i], $real[$j]) && $tail[$i] === $real[$j]) {
  213. --$i;
  214. --$j;
  215. }
  216. array_splice($tail, 0, $i + 1);
  217. }
  218. if (self::$caseCheck && $tail) {
  219. $tail = DIRECTORY_SEPARATOR.implode(DIRECTORY_SEPARATOR, $tail);
  220. $tailLen = strlen($tail);
  221. $real = $refl->getFileName();
  222. if (2 === self::$caseCheck) {
  223. // realpath() on MacOSX doesn't normalize the case of characters
  224. $i = 1 + strrpos($real, '/');
  225. $file = substr($real, $i);
  226. $real = substr($real, 0, $i);
  227. if (isset(self::$darwinCache[$real])) {
  228. $kDir = $real;
  229. } else {
  230. $kDir = strtolower($real);
  231. if (isset(self::$darwinCache[$kDir])) {
  232. $real = self::$darwinCache[$kDir][0];
  233. } else {
  234. $dir = getcwd();
  235. chdir($real);
  236. $real = getcwd().'/';
  237. chdir($dir);
  238. $dir = $real;
  239. $k = $kDir;
  240. $i = strlen($dir) - 1;
  241. while (!isset(self::$darwinCache[$k])) {
  242. self::$darwinCache[$k] = array($dir, array());
  243. self::$darwinCache[$dir] = &self::$darwinCache[$k];
  244. while ('/' !== $dir[--$i]) {
  245. }
  246. $k = substr($k, 0, ++$i);
  247. $dir = substr($dir, 0, $i--);
  248. }
  249. }
  250. }
  251. $dirFiles = self::$darwinCache[$kDir][1];
  252. if (isset($dirFiles[$file])) {
  253. $kFile = $file;
  254. } else {
  255. $kFile = strtolower($file);
  256. if (!isset($dirFiles[$kFile])) {
  257. foreach (scandir($real, 2) as $f) {
  258. if ('.' !== $f[0]) {
  259. $dirFiles[$f] = $f;
  260. if ($f === $file) {
  261. $kFile = $k = $file;
  262. } elseif ($f !== $k = strtolower($f)) {
  263. $dirFiles[$k] = $f;
  264. }
  265. }
  266. }
  267. self::$darwinCache[$kDir][1] = $dirFiles;
  268. }
  269. }
  270. $real .= $dirFiles[$kFile];
  271. }
  272. if (0 === substr_compare($real, $tail, -$tailLen, $tailLen, true)
  273. && 0 !== substr_compare($real, $tail, -$tailLen, $tailLen, false)
  274. ) {
  275. 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)));
  276. }
  277. }
  278. return true;
  279. }
  280. }
  281. }