PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/Laolballs/evotting
PHP | 197 lines | 172 code | 7 blank | 18 comment | 7 complexity | 322d46458efbb1b4387d7e530c9ef3c0 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('Attempted to load %s "%s" from namespace "%s".', $typeName, $className, $namespacePrefix);
  49. $tail = ' for another namespace?';
  50. } else {
  51. $className = $fullyQualifiedClassName;
  52. $message = sprintf('Attempted to load %s "%s" from the global namespace.', $typeName, $className);
  53. $tail = '?';
  54. }
  55. if ($candidates = $this->getClassCandidates($className)) {
  56. $tail = array_pop($candidates).'"?';
  57. if ($candidates) {
  58. $tail = ' for e.g. "'.implode('", "', $candidates).'" or "'.$tail;
  59. } else {
  60. $tail = ' for "'.$tail;
  61. }
  62. }
  63. $message .= "\nDid you forget a \"use\" statement".$tail;
  64. return new ClassNotFoundException($message, $exception);
  65. }
  66. }
  67. /**
  68. * Tries to guess the full namespace for a given class name.
  69. *
  70. * By default, it looks for PSR-0 classes registered via a Symfony or a Composer
  71. * autoloader (that should cover all common cases).
  72. *
  73. * @param string $class A class name (without its namespace)
  74. *
  75. * @return array An array of possible fully qualified class names
  76. */
  77. private function getClassCandidates($class)
  78. {
  79. if (!is_array($functions = spl_autoload_functions())) {
  80. return array();
  81. }
  82. // find Symfony and Composer autoloaders
  83. $classes = array();
  84. foreach ($functions as $function) {
  85. if (!is_array($function)) {
  86. continue;
  87. }
  88. // get class loaders wrapped by DebugClassLoader
  89. if ($function[0] instanceof DebugClassLoader) {
  90. $function = $function[0]->getClassLoader();
  91. // Since 2.5, returning an object from DebugClassLoader::getClassLoader() is @deprecated
  92. if (is_object($function)) {
  93. $function = array($function);
  94. }
  95. if (!is_array($function)) {
  96. continue;
  97. }
  98. }
  99. if ($function[0] instanceof ComposerClassLoader || $function[0] instanceof SymfonyClassLoader || $function[0] instanceof SymfonyUniversalClassLoader) {
  100. foreach ($function[0]->getPrefixes() as $prefix => $paths) {
  101. foreach ($paths as $path) {
  102. $classes = array_merge($classes, $this->findClassInPath($path, $class, $prefix));
  103. }
  104. }
  105. }
  106. }
  107. return array_unique($classes);
  108. }
  109. /**
  110. * @param string $path
  111. * @param string $class
  112. * @param string $prefix
  113. *
  114. * @return array
  115. */
  116. private function findClassInPath($path, $class, $prefix)
  117. {
  118. if (!$path = realpath($path)) {
  119. return array();
  120. }
  121. $classes = array();
  122. $filename = $class.'.php';
  123. foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
  124. if ($filename == $file->getFileName() && $class = $this->convertFileToClass($path, $file->getPathName(), $prefix)) {
  125. $classes[] = $class;
  126. }
  127. }
  128. return $classes;
  129. }
  130. /**
  131. * @param string $path
  132. * @param string $file
  133. * @param string $prefix
  134. *
  135. * @return string|null
  136. */
  137. private function convertFileToClass($path, $file, $prefix)
  138. {
  139. $candidates = array(
  140. // namespaced class
  141. $namespacedClass = str_replace(array($path.DIRECTORY_SEPARATOR, '.php', '/'), array('', '', '\\'), $file),
  142. // namespaced class (with target dir)
  143. $namespacedClassTargetDir = $prefix.str_replace(array($path.DIRECTORY_SEPARATOR, '.php', '/'), array('', '', '\\'), $file),
  144. // PEAR class
  145. str_replace('\\', '_', $namespacedClass),
  146. // PEAR class (with target dir)
  147. str_replace('\\', '_', $namespacedClassTargetDir),
  148. );
  149. // We cannot use the autoloader here as most of them use require; but if the class
  150. // is not found, the new autoloader call will require the file again leading to a
  151. // "cannot redeclare class" error.
  152. foreach ($candidates as $candidate) {
  153. if ($this->classExists($candidate)) {
  154. return $candidate;
  155. }
  156. }
  157. require_once $file;
  158. foreach ($candidates as $candidate) {
  159. if ($this->classExists($candidate)) {
  160. return $candidate;
  161. }
  162. }
  163. }
  164. /**
  165. * @param string $class
  166. *
  167. * @return bool
  168. */
  169. private function classExists($class)
  170. {
  171. return class_exists($class, false) || interface_exists($class, false) || (function_exists('trait_exists') && trait_exists($class, false));
  172. }
  173. }