DebugClassLoader.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. *
  37. * @deprecated since 2.5, passing an object is deprecated and support for it will be removed in 3.0
  38. */
  39. public function __construct($classLoader)
  40. {
  41. $this->wasFinder = is_object($classLoader) && method_exists($classLoader, 'findFile');
  42. if ($this->wasFinder) {
  43. $this->classLoader = array($classLoader, 'loadClass');
  44. $this->isFinder = true;
  45. } else {
  46. $this->classLoader = $classLoader;
  47. $this->isFinder = is_array($classLoader) && method_exists($classLoader[0], 'findFile');
  48. }
  49. if (!isset(self::$caseCheck)) {
  50. self::$caseCheck = false !== stripos(PHP_OS, 'win') ? (false !== stripos(PHP_OS, 'darwin') ? 2 : 1) : 0;
  51. }
  52. }
  53. /**
  54. * Gets the wrapped class loader.
  55. *
  56. * @return callable|object a class loader
  57. *
  58. * @deprecated since 2.5, returning an object is deprecated and support for it will be removed in 3.0
  59. */
  60. public function getClassLoader()
  61. {
  62. return $this->wasFinder ? $this->classLoader[0] : $this->classLoader;
  63. }
  64. /**
  65. * Wraps all autoloaders.
  66. */
  67. public static function enable()
  68. {
  69. // Ensures we don't hit https://bugs.php.net/42098
  70. class_exists('Symfony\Component\Debug\ErrorHandler');
  71. class_exists('Psr\Log\LogLevel');
  72. if (!is_array($functions = spl_autoload_functions())) {
  73. return;
  74. }
  75. foreach ($functions as $function) {
  76. spl_autoload_unregister($function);
  77. }
  78. foreach ($functions as $function) {
  79. if (!is_array($function) || !$function[0] instanceof self) {
  80. $function = array(new static($function), 'loadClass');
  81. }
  82. spl_autoload_register($function);
  83. }
  84. }
  85. /**
  86. * Disables the wrapping.
  87. */
  88. public static function disable()
  89. {
  90. if (!is_array($functions = spl_autoload_functions())) {
  91. return;
  92. }
  93. foreach ($functions as $function) {
  94. spl_autoload_unregister($function);
  95. }
  96. foreach ($functions as $function) {
  97. if (is_array($function) && $function[0] instanceof self) {
  98. $function = $function[0]->getClassLoader();
  99. }
  100. spl_autoload_register($function);
  101. }
  102. }
  103. /**
  104. * Finds a file by class name.
  105. *
  106. * @param string $class A class name to resolve to file
  107. *
  108. * @return string|null
  109. *
  110. * @deprecated Deprecated since 2.5, to be removed in 3.0.
  111. */
  112. public function findFile($class)
  113. {
  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. }
  155. if ($file) {
  156. if (!$exists) {
  157. if (false !== strpos($class, '/')) {
  158. 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));
  159. }
  160. 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));
  161. }
  162. if (self::$caseCheck && preg_match('#([/\\\\][a-zA-Z_\x7F-\xFF][a-zA-Z0-9_\x7F-\xFF]*)+\.(php|hh)$#D', $file, $tail)) {
  163. $tail = $tail[0];
  164. $real = $refl->getFileName();
  165. if (2 === self::$caseCheck) {
  166. // realpath() on MacOSX doesn't normalize the case of characters
  167. $cwd = getcwd();
  168. $basename = strrpos($real, '/');
  169. chdir(substr($real, 0, $basename));
  170. $basename = substr($real, $basename + 1);
  171. // glob() patterns are case-sensitive even if the underlying fs is not
  172. if (!in_array($basename, glob($basename.'*', GLOB_NOSORT), true)) {
  173. $real = getcwd().'/';
  174. $h = opendir('.');
  175. while (false !== $f = readdir($h)) {
  176. if (0 === strcasecmp($f, $basename)) {
  177. $real .= $f;
  178. break;
  179. }
  180. }
  181. closedir($h);
  182. }
  183. chdir($cwd);
  184. }
  185. if (0 === substr_compare($real, $tail, -strlen($tail), strlen($tail), true)
  186. && 0 !== substr_compare($real, $tail, -strlen($tail), strlen($tail), false)
  187. ) {
  188. throw new \RuntimeException(sprintf('Case mismatch between class and source file names: %s vs %s', $class, $real));
  189. }
  190. }
  191. return true;
  192. }
  193. }
  194. }