/vendor/symfony/debug/Symfony/Component/Debug/FatalErrorHandler/ClassNotFoundFatalErrorHandler.php

https://gitlab.com/xolotsoft/pumasruiz · PHP · 203 lines · 124 code · 24 blank · 55 comment · 23 complexity · a18e0bcf11d9ee0d288371d0d291f564 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\FatalErrorHandler;
  11. use Symfony\Component\Debug\Exception\ClassNotFoundException;
  12. use Symfony\Component\Debug\Exception\FatalErrorException;
  13. use Symfony\Component\Debug\DebugClassLoader;
  14. use Composer\Autoload\ClassLoader as ComposerClassLoader;
  15. use Symfony\Component\ClassLoader\ClassLoader as SymfonyClassLoader;
  16. use Symfony\Component\ClassLoader\UniversalClassLoader as SymfonyUniversalClassLoader;
  17. /**
  18. * ErrorHandler for classes that do not exist.
  19. *
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. */
  22. class ClassNotFoundFatalErrorHandler implements FatalErrorHandlerInterface
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function handleError(array $error, FatalErrorException $exception)
  28. {
  29. $messageLen = strlen($error['message']);
  30. $notFoundSuffix = '\' not found';
  31. $notFoundSuffixLen = strlen($notFoundSuffix);
  32. if ($notFoundSuffixLen > $messageLen) {
  33. return;
  34. }
  35. if (0 !== substr_compare($error['message'], $notFoundSuffix, -$notFoundSuffixLen)) {
  36. return;
  37. }
  38. foreach (array('class', 'interface', 'trait') as $typeName) {
  39. $prefix = ucfirst($typeName).' \'';
  40. $prefixLen = strlen($prefix);
  41. if (0 !== strpos($error['message'], $prefix)) {
  42. continue;
  43. }
  44. $fullyQualifiedClassName = substr($error['message'], $prefixLen, -$notFoundSuffixLen);
  45. if (false !== $namespaceSeparatorIndex = strrpos($fullyQualifiedClassName, '\\')) {
  46. $className = substr($fullyQualifiedClassName, $namespaceSeparatorIndex + 1);
  47. $namespacePrefix = substr($fullyQualifiedClassName, 0, $namespaceSeparatorIndex);
  48. $message = sprintf(
  49. 'Attempted to load %s "%s" from namespace "%s" in %s line %d. Do you need to "use" it from another namespace?',
  50. $typeName,
  51. $className,
  52. $namespacePrefix,
  53. $error['file'],
  54. $error['line']
  55. );
  56. } else {
  57. $className = $fullyQualifiedClassName;
  58. $message = sprintf(
  59. 'Attempted to load %s "%s" from the global namespace in %s line %d. Did you forget a use statement for this %s?',
  60. $typeName,
  61. $className,
  62. $error['file'],
  63. $error['line'],
  64. $typeName
  65. );
  66. }
  67. if ($classes = $this->getClassCandidates($className)) {
  68. $message .= sprintf(' Perhaps you need to add a use statement for one of the following: %s.', implode(', ', $classes));
  69. }
  70. return new ClassNotFoundException($message, $exception);
  71. }
  72. }
  73. /**
  74. * Tries to guess the full namespace for a given class name.
  75. *
  76. * By default, it looks for PSR-0 classes registered via a Symfony or a Composer
  77. * autoloader (that should cover all common cases).
  78. *
  79. * @param string $class A class name (without its namespace)
  80. *
  81. * @return array An array of possible fully qualified class names
  82. */
  83. private function getClassCandidates($class)
  84. {
  85. if (!is_array($functions = spl_autoload_functions())) {
  86. return array();
  87. }
  88. // find Symfony and Composer autoloaders
  89. $classes = array();
  90. foreach ($functions as $function) {
  91. if (!is_array($function)) {
  92. continue;
  93. }
  94. // get class loaders wrapped by DebugClassLoader
  95. if ($function[0] instanceof DebugClassLoader) {
  96. $function = $function[0]->getClassLoader();
  97. // Since 2.5, returning an object from DebugClassLoader::getClassLoader() is @deprecated
  98. if (is_object($function)) {
  99. $function = array($function);
  100. }
  101. if (!is_array($function)) {
  102. continue;
  103. }
  104. }
  105. if ($function[0] instanceof ComposerClassLoader || $function[0] instanceof SymfonyClassLoader || $function[0] instanceof SymfonyUniversalClassLoader) {
  106. foreach ($function[0]->getPrefixes() as $prefix => $paths) {
  107. foreach ($paths as $path) {
  108. $classes = array_merge($classes, $this->findClassInPath($path, $class, $prefix));
  109. }
  110. }
  111. }
  112. }
  113. return array_unique($classes);
  114. }
  115. /**
  116. * @param string $path
  117. * @param string $class
  118. * @param string $prefix
  119. *
  120. * @return array
  121. */
  122. private function findClassInPath($path, $class, $prefix)
  123. {
  124. if (!$path = realpath($path)) {
  125. return array();
  126. }
  127. $classes = array();
  128. $filename = $class.'.php';
  129. foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
  130. if ($filename == $file->getFileName() && $class = $this->convertFileToClass($path, $file->getPathName(), $prefix)) {
  131. $classes[] = $class;
  132. }
  133. }
  134. return $classes;
  135. }
  136. /**
  137. * @param string $path
  138. * @param string $file
  139. * @param string $prefix
  140. *
  141. * @return string|null
  142. */
  143. private function convertFileToClass($path, $file, $prefix)
  144. {
  145. $candidates = array(
  146. // namespaced class
  147. $namespacedClass = str_replace(array($path.DIRECTORY_SEPARATOR, '.php', '/'), array('', '', '\\'), $file),
  148. // namespaced class (with target dir)
  149. $namespacedClassTargetDir = $prefix.str_replace(array($path.DIRECTORY_SEPARATOR, '.php', '/'), array('', '', '\\'), $file),
  150. // PEAR class
  151. str_replace('\\', '_', $namespacedClass),
  152. // PEAR class (with target dir)
  153. str_replace('\\', '_', $namespacedClassTargetDir),
  154. );
  155. // We cannot use the autoloader here as most of them use require; but if the class
  156. // is not found, the new autoloader call will require the file again leading to a
  157. // "cannot redeclare class" error.
  158. foreach ($candidates as $candidate) {
  159. if ($this->classExists($candidate)) {
  160. return $candidate;
  161. }
  162. }
  163. require_once $file;
  164. foreach ($candidates as $candidate) {
  165. if ($this->classExists($candidate)) {
  166. return $candidate;
  167. }
  168. }
  169. }
  170. /**
  171. * @param string $class
  172. *
  173. * @return bool
  174. */
  175. private function classExists($class)
  176. {
  177. return class_exists($class, false) || interface_exists($class, false) || (function_exists('trait_exists') && trait_exists($class, false));
  178. }
  179. }