PageRenderTime 50ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/debug/DebugClassLoader.php

https://gitlab.com/judielsm/Handora
PHP | 258 lines | 167 code | 31 blank | 60 comment | 43 complexity | b5eca64adf726904e9f86867bc4707f3 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. * @api
  23. */
  24. class DebugClassLoader
  25. {
  26. private $classLoader;
  27. private $isFinder;
  28. private $wasFinder;
  29. private static $caseCheck;
  30. private static $deprecated = array();
  31. private static $php7Reserved = array('int', 'float', 'bool', 'string', 'true', 'false', 'null');
  32. /**
  33. * Constructor.
  34. *
  35. * @param callable|object $classLoader Passing an object is @deprecated since version 2.5 and support for it will be removed in 3.0
  36. *
  37. * @api
  38. */
  39. public function __construct($classLoader)
  40. {
  41. $this->wasFinder = is_object($classLoader) && method_exists($classLoader, 'findFile');
  42. if ($this->wasFinder) {
  43. @trigger_error('The '.__METHOD__.' method will no longer support receiving an object into its $classLoader argument in 3.0.', E_USER_DEPRECATED);
  44. $this->classLoader = array($classLoader, 'loadClass');
  45. $this->isFinder = true;
  46. } else {
  47. $this->classLoader = $classLoader;
  48. $this->isFinder = is_array($classLoader) && method_exists($classLoader[0], 'findFile');
  49. }
  50. if (!isset(self::$caseCheck)) {
  51. self::$caseCheck = false !== stripos(PHP_OS, 'win') ? (false !== stripos(PHP_OS, 'darwin') ? 2 : 1) : 0;
  52. }
  53. }
  54. /**
  55. * Gets the wrapped class loader.
  56. *
  57. * @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
  58. */
  59. public function getClassLoader()
  60. {
  61. return $this->wasFinder ? $this->classLoader[0] : $this->classLoader;
  62. }
  63. /**
  64. * Wraps all autoloaders.
  65. */
  66. public static function enable()
  67. {
  68. // Ensures we don't hit https://bugs.php.net/42098
  69. class_exists('Symfony\Component\Debug\ErrorHandler');
  70. class_exists('Psr\Log\LogLevel');
  71. if (!is_array($functions = spl_autoload_functions())) {
  72. return;
  73. }
  74. foreach ($functions as $function) {
  75. spl_autoload_unregister($function);
  76. }
  77. foreach ($functions as $function) {
  78. if (!is_array($function) || !$function[0] instanceof self) {
  79. $function = array(new static($function), 'loadClass');
  80. }
  81. spl_autoload_register($function);
  82. }
  83. }
  84. /**
  85. * Disables the wrapping.
  86. */
  87. public static function disable()
  88. {
  89. if (!is_array($functions = spl_autoload_functions())) {
  90. return;
  91. }
  92. foreach ($functions as $function) {
  93. spl_autoload_unregister($function);
  94. }
  95. foreach ($functions as $function) {
  96. if (is_array($function) && $function[0] instanceof self) {
  97. $function = $function[0]->getClassLoader();
  98. }
  99. spl_autoload_register($function);
  100. }
  101. }
  102. /**
  103. * Finds a file by class name.
  104. *
  105. * @param string $class A class name to resolve to file
  106. *
  107. * @return string|null
  108. *
  109. * @deprecated since version 2.5, to be removed in 3.0.
  110. */
  111. public function findFile($class)
  112. {
  113. @trigger_error('The '.__METHOD__.' method is deprecated since version 2.5 and will be removed in 3.0.', E_USER_DEPRECATED);
  114. if ($this->wasFinder) {
  115. return $this->classLoader[0]->findFile($class);
  116. }
  117. }
  118. /**
  119. * Loads the given class or interface.
  120. *
  121. * @param string $class The name of the class
  122. *
  123. * @return bool|null True, if loaded
  124. *
  125. * @throws \RuntimeException
  126. */
  127. public function loadClass($class)
  128. {
  129. ErrorHandler::stackErrors();
  130. try {
  131. if ($this->isFinder) {
  132. if ($file = $this->classLoader[0]->findFile($class)) {
  133. require $file;
  134. }
  135. } else {
  136. call_user_func($this->classLoader, $class);
  137. $file = false;
  138. }
  139. } catch (\Exception $e) {
  140. ErrorHandler::unstackErrors();
  141. throw $e;
  142. }
  143. ErrorHandler::unstackErrors();
  144. $exists = class_exists($class, false) || interface_exists($class, false) || (function_exists('trait_exists') && trait_exists($class, false));
  145. if ('\\' === $class[0]) {
  146. $class = substr($class, 1);
  147. }
  148. if ($exists) {
  149. $refl = new \ReflectionClass($class);
  150. $name = $refl->getName();
  151. if ($name !== $class && 0 === strcasecmp($name, $class)) {
  152. throw new \RuntimeException(sprintf('Case mismatch between loaded and declared class names: %s vs %s', $class, $name));
  153. }
  154. if (in_array(strtolower($refl->getShortName()), self::$php7Reserved)) {
  155. @trigger_error(sprintf('%s uses a reserved class name (%s) that will break on PHP 7 and higher', $name, $refl->getShortName()), E_USER_DEPRECATED);
  156. } elseif (preg_match('#\n \* @deprecated (.*?)\r?\n \*(?: @|/$)#s', $refl->getDocComment(), $notice)) {
  157. self::$deprecated[$name] = preg_replace('#\s*\r?\n \* +#', ' ', $notice[1]);
  158. } else {
  159. if (2 > $len = 1 + (strpos($name, '\\', 1 + strpos($name, '\\')) ?: strpos($name, '_'))) {
  160. $len = 0;
  161. $ns = '';
  162. } else {
  163. switch ($ns = substr($name, 0, $len)) {
  164. case 'Symfony\Bridge\\':
  165. case 'Symfony\Bundle\\':
  166. case 'Symfony\Component\\':
  167. $ns = 'Symfony\\';
  168. $len = strlen($ns);
  169. break;
  170. }
  171. }
  172. $parent = $refl->getParentClass();
  173. if (!$parent || strncmp($ns, $parent->name, $len)) {
  174. if ($parent && isset(self::$deprecated[$parent->name]) && strncmp($ns, $parent->name, $len)) {
  175. @trigger_error(sprintf('The %s class extends %s that is deprecated %s', $name, $parent->name, self::$deprecated[$parent->name]), E_USER_DEPRECATED);
  176. }
  177. foreach ($refl->getInterfaceNames() as $interface) {
  178. if (isset(self::$deprecated[$interface]) && strncmp($ns, $interface, $len) && !($parent && $parent->implementsInterface($interface))) {
  179. @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);
  180. }
  181. }
  182. }
  183. }
  184. }
  185. if ($file) {
  186. if (!$exists) {
  187. if (false !== strpos($class, '/')) {
  188. 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));
  189. }
  190. 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));
  191. }
  192. if (self::$caseCheck && preg_match('#([/\\\\][a-zA-Z_\x7F-\xFF][a-zA-Z0-9_\x7F-\xFF]*)+\.(php|hh)$#D', $file, $tail)) {
  193. $tail = $tail[0];
  194. $real = $refl->getFileName();
  195. if (2 === self::$caseCheck) {
  196. // realpath() on MacOSX doesn't normalize the case of characters
  197. $cwd = getcwd();
  198. $basename = strrpos($real, '/');
  199. chdir(substr($real, 0, $basename));
  200. $basename = substr($real, $basename + 1);
  201. // glob() patterns are case-sensitive even if the underlying fs is not
  202. if (!in_array($basename, glob($basename.'*', GLOB_NOSORT), true)) {
  203. $real = getcwd().'/';
  204. $h = opendir('.');
  205. while (false !== $f = readdir($h)) {
  206. if (0 === strcasecmp($f, $basename)) {
  207. $real .= $f;
  208. break;
  209. }
  210. }
  211. closedir($h);
  212. }
  213. chdir($cwd);
  214. }
  215. if (0 === substr_compare($real, $tail, -strlen($tail), strlen($tail), true)
  216. && 0 !== substr_compare($real, $tail, -strlen($tail), strlen($tail), false)
  217. ) {
  218. throw new \RuntimeException(sprintf('Case mismatch between class and source file names: %s vs %s', $class, $real));
  219. }
  220. }
  221. return true;
  222. }
  223. }
  224. }