PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Symfony/Component/Debug/DebugClassLoader.php

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