PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/debug/Symfony/Component/Debug/DebugClassLoader.php

https://gitlab.com/Laolballs/evotting
PHP | 223 lines | 139 code | 23 blank | 61 comment | 19 complexity | 517eb50b9896e9bd91a077de927934c6 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. /**
  31. * Constructor.
  32. *
  33. * @param callable|object $classLoader
  34. *
  35. * @api
  36. * @deprecated since 2.5, passing an object is deprecated and support for it will be removed in 3.0
  37. */
  38. public function __construct($classLoader)
  39. {
  40. $this->wasFinder = is_object($classLoader) && method_exists($classLoader, 'findFile');
  41. if ($this->wasFinder) {
  42. $this->classLoader = array($classLoader, 'loadClass');
  43. $this->isFinder = true;
  44. } else {
  45. $this->classLoader = $classLoader;
  46. $this->isFinder = is_array($classLoader) && method_exists($classLoader[0], 'findFile');
  47. }
  48. if (!isset(self::$caseCheck)) {
  49. self::$caseCheck = false !== stripos(PHP_OS, 'win') ? (false !== stripos(PHP_OS, 'darwin') ? 2 : 1) : 0;
  50. }
  51. }
  52. /**
  53. * Gets the wrapped class loader.
  54. *
  55. * @return callable|object a class loader
  56. *
  57. * @deprecated since 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 Deprecated since 2.5, to be removed in 3.0.
  110. */
  111. public function findFile($class)
  112. {
  113. if ($this->wasFinder) {
  114. return $this->classLoader[0]->findFile($class);
  115. }
  116. }
  117. /**
  118. * Loads the given class or interface.
  119. *
  120. * @param string $class The name of the class
  121. *
  122. * @return bool|null True, if loaded
  123. *
  124. * @throws \RuntimeException
  125. */
  126. public function loadClass($class)
  127. {
  128. ErrorHandler::stackErrors();
  129. try {
  130. if ($this->isFinder) {
  131. if ($file = $this->classLoader[0]->findFile($class)) {
  132. require $file;
  133. }
  134. } else {
  135. call_user_func($this->classLoader, $class);
  136. $file = false;
  137. }
  138. } catch (\Exception $e) {
  139. ErrorHandler::unstackErrors();
  140. throw $e;
  141. }
  142. ErrorHandler::unstackErrors();
  143. $exists = class_exists($class, false) || interface_exists($class, false) || (function_exists('trait_exists') && trait_exists($class, false));
  144. if ('\\' === $class[0]) {
  145. $class = substr($class, 1);
  146. }
  147. if ($exists) {
  148. $refl = new \ReflectionClass($class);
  149. $name = $refl->getName();
  150. if ($name !== $class && 0 === strcasecmp($name, $class)) {
  151. throw new \RuntimeException(sprintf('Case mismatch between loaded and declared class names: %s vs %s', $class, $name));
  152. }
  153. }
  154. if ($file) {
  155. if (!$exists) {
  156. if (false !== strpos($class, '/')) {
  157. 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));
  158. }
  159. 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));
  160. }
  161. if (self::$caseCheck && preg_match('#([/\\\\][a-zA-Z_\x7F-\xFF][a-zA-Z0-9_\x7F-\xFF]*)+\.(php|hh)$#D', $file, $tail)) {
  162. $tail = $tail[0];
  163. $real = $refl->getFilename();
  164. if (2 === self::$caseCheck) {
  165. // realpath() on MacOSX doesn't normalize the case of characters
  166. $cwd = getcwd();
  167. $basename = strrpos($real, '/');
  168. chdir(substr($real, 0, $basename));
  169. $basename = substr($real, $basename + 1);
  170. // glob() patterns are case-sensitive even if the underlying fs is not
  171. if (!in_array($basename, glob($basename.'*', GLOB_NOSORT), true)) {
  172. $real = getcwd().'/';
  173. $h = opendir('.');
  174. while (false !== $f = readdir($h)) {
  175. if (0 === strcasecmp($f, $basename)) {
  176. $real .= $f;
  177. break;
  178. }
  179. }
  180. closedir($h);
  181. }
  182. chdir($cwd);
  183. }
  184. if (0 === substr_compare($real, $tail, -strlen($tail), strlen($tail), true)
  185. && 0 !== substr_compare($real, $tail, -strlen($tail), strlen($tail), false)
  186. ) {
  187. throw new \RuntimeException(sprintf('Case mismatch between class and source file names: %s vs %s', $class, $real));
  188. }
  189. }
  190. return true;
  191. }
  192. }
  193. }