PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/xolotsoft/pumasruiz
PHP | 222 lines | 138 code | 23 blank | 61 comment | 19 complexity | 3bd626b7cf535811ce985f921e768643 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(__NAMESPACE__.'\ErrorHandler', true);
  70. if (!is_array($functions = spl_autoload_functions())) {
  71. return;
  72. }
  73. foreach ($functions as $function) {
  74. spl_autoload_unregister($function);
  75. }
  76. foreach ($functions as $function) {
  77. if (!is_array($function) || !$function[0] instanceof self) {
  78. $function = array(new static($function), 'loadClass');
  79. }
  80. spl_autoload_register($function);
  81. }
  82. }
  83. /**
  84. * Disables the wrapping.
  85. */
  86. public static function disable()
  87. {
  88. if (!is_array($functions = spl_autoload_functions())) {
  89. return;
  90. }
  91. foreach ($functions as $function) {
  92. spl_autoload_unregister($function);
  93. }
  94. foreach ($functions as $function) {
  95. if (is_array($function) && $function[0] instanceof self) {
  96. $function = $function[0]->getClassLoader();
  97. }
  98. spl_autoload_register($function);
  99. }
  100. }
  101. /**
  102. * Finds a file by class name
  103. *
  104. * @param string $class A class name to resolve to file
  105. *
  106. * @return string|null
  107. *
  108. * @deprecated Deprecated since 2.5, to be removed in 3.0.
  109. */
  110. public function findFile($class)
  111. {
  112. if ($this->wasFinder) {
  113. return $this->classLoader[0]->findFile($class);
  114. }
  115. }
  116. /**
  117. * Loads the given class or interface.
  118. *
  119. * @param string $class The name of the class
  120. *
  121. * @return bool|null True, if loaded
  122. *
  123. * @throws \RuntimeException
  124. */
  125. public function loadClass($class)
  126. {
  127. ErrorHandler::stackErrors();
  128. try {
  129. if ($this->isFinder) {
  130. if ($file = $this->classLoader[0]->findFile($class)) {
  131. require $file;
  132. }
  133. } else {
  134. call_user_func($this->classLoader, $class);
  135. $file = false;
  136. }
  137. } catch (\Exception $e) {
  138. ErrorHandler::unstackErrors();
  139. throw $e;
  140. }
  141. ErrorHandler::unstackErrors();
  142. $exists = class_exists($class, false) || interface_exists($class, false) || (function_exists('trait_exists') && trait_exists($class, false));
  143. if ('\\' === $class[0]) {
  144. $class = substr($class, 1);
  145. }
  146. if ($exists) {
  147. $refl = new \ReflectionClass($class);
  148. $name = $refl->getName();
  149. if ($name !== $class && 0 === strcasecmp($name, $class)) {
  150. throw new \RuntimeException(sprintf('Case mismatch between loaded and declared class names: %s vs %s', $class, $name));
  151. }
  152. }
  153. if ($file) {
  154. if (!$exists) {
  155. if (false !== strpos($class, '/')) {
  156. 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));
  157. }
  158. 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));
  159. }
  160. if (self::$caseCheck && preg_match('#([/\\\\][a-zA-Z_\x7F-\xFF][a-zA-Z0-9_\x7F-\xFF]*)+\.(php|hh)$#D', $file, $tail)) {
  161. $tail = $tail[0];
  162. $real = $refl->getFilename();
  163. if (2 === self::$caseCheck) {
  164. // realpath() on MacOSX doesn't normalize the case of characters
  165. $cwd = getcwd();
  166. $basename = strrpos($real, '/');
  167. chdir(substr($real, 0, $basename));
  168. $basename = substr($real, $basename + 1);
  169. // glob() patterns are case-sensitive even if the underlying fs is not
  170. if (!in_array($basename, glob($basename.'*', GLOB_NOSORT), true)) {
  171. $real = getcwd().'/';
  172. $h = opendir('.');
  173. while (false !== $f = readdir($h)) {
  174. if (0 === strcasecmp($f, $basename)) {
  175. $real .= $f;
  176. break;
  177. }
  178. }
  179. closedir($h);
  180. }
  181. chdir($cwd);
  182. }
  183. if (0 === substr_compare($real, $tail, -strlen($tail), strlen($tail), true)
  184. && 0 !== substr_compare($real, $tail, -strlen($tail), strlen($tail), false)
  185. ) {
  186. throw new \RuntimeException(sprintf('Case mismatch between class and source file names: %s vs %s', $class, $real));
  187. }
  188. }
  189. return true;
  190. }
  191. }
  192. }