PageRenderTime 60ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/debug/DebugClassLoader.php

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