PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/debug/DebugClassLoader.php

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