PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/debug/FatalErrorHandler/ClassNotFoundFatalErrorHandler.php

https://gitlab.com/madwanz64/laravel
PHP | 183 lines | 144 code | 15 blank | 24 comment | 13 complexity | 3f14a0e21ab1951fb00f77a99c332a72 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 Composer\Autoload\ClassLoader as ComposerClassLoader;
  12. use Symfony\Component\ClassLoader\ClassLoader as SymfonyClassLoader;
  13. use Symfony\Component\Debug\DebugClassLoader;
  14. use Symfony\Component\Debug\Exception\ClassNotFoundException;
  15. use Symfony\Component\Debug\Exception\FatalErrorException;
  16. @trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.4, use "%s" instead.', ClassNotFoundFatalErrorHandler::class, \Symfony\Component\ErrorHandler\FatalErrorHandler\ClassNotFoundFatalErrorHandler::class), \E_USER_DEPRECATED);
  17. /**
  18. * ErrorHandler for classes that do not exist.
  19. *
  20. * @author Fabien Potencier <fabien@symfony.com>
  21. *
  22. * @deprecated since Symfony 4.4, use Symfony\Component\ErrorHandler\FatalErrorHandler\ClassNotFoundFatalErrorHandler instead.
  23. */
  24. class ClassNotFoundFatalErrorHandler implements FatalErrorHandlerInterface
  25. {
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function handleError(array $error, FatalErrorException $exception)
  30. {
  31. if (!preg_match('/^(Class|Interface|Trait) [\'"]([^\'"]+)[\'"] not found$/', $error['message'], $matches)) {
  32. return null;
  33. }
  34. $typeName = strtolower($matches[1]);
  35. $fullyQualifiedClassName = $matches[2];
  36. if (false !== $namespaceSeparatorIndex = strrpos($fullyQualifiedClassName, '\\')) {
  37. $className = substr($fullyQualifiedClassName, $namespaceSeparatorIndex + 1);
  38. $namespacePrefix = substr($fullyQualifiedClassName, 0, $namespaceSeparatorIndex);
  39. $message = sprintf('Attempted to load %s "%s" from namespace "%s".', $typeName, $className, $namespacePrefix);
  40. $tail = ' for another namespace?';
  41. } else {
  42. $className = $fullyQualifiedClassName;
  43. $message = sprintf('Attempted to load %s "%s" from the global namespace.', $typeName, $className);
  44. $tail = '?';
  45. }
  46. if ($candidates = $this->getClassCandidates($className)) {
  47. $tail = array_pop($candidates).'"?';
  48. if ($candidates) {
  49. $tail = ' for e.g. "'.implode('", "', $candidates).'" or "'.$tail;
  50. } else {
  51. $tail = ' for "'.$tail;
  52. }
  53. }
  54. $message .= "\nDid you forget a \"use\" statement".$tail;
  55. return new ClassNotFoundException($message, $exception);
  56. }
  57. /**
  58. * Tries to guess the full namespace for a given class name.
  59. *
  60. * By default, it looks for PSR-0 and PSR-4 classes registered via a Symfony or a Composer
  61. * autoloader (that should cover all common cases).
  62. *
  63. * @param string $class A class name (without its namespace)
  64. *
  65. * @return array An array of possible fully qualified class names
  66. */
  67. private function getClassCandidates(string $class): array
  68. {
  69. if (!\is_array($functions = spl_autoload_functions())) {
  70. return [];
  71. }
  72. // find Symfony and Composer autoloaders
  73. $classes = [];
  74. foreach ($functions as $function) {
  75. if (!\is_array($function)) {
  76. continue;
  77. }
  78. // get class loaders wrapped by DebugClassLoader
  79. if ($function[0] instanceof DebugClassLoader) {
  80. $function = $function[0]->getClassLoader();
  81. if (!\is_array($function)) {
  82. continue;
  83. }
  84. }
  85. if ($function[0] instanceof ComposerClassLoader || $function[0] instanceof SymfonyClassLoader) {
  86. foreach ($function[0]->getPrefixes() as $prefix => $paths) {
  87. foreach ($paths as $path) {
  88. $classes = array_merge($classes, $this->findClassInPath($path, $class, $prefix));
  89. }
  90. }
  91. }
  92. if ($function[0] instanceof ComposerClassLoader) {
  93. foreach ($function[0]->getPrefixesPsr4() as $prefix => $paths) {
  94. foreach ($paths as $path) {
  95. $classes = array_merge($classes, $this->findClassInPath($path, $class, $prefix));
  96. }
  97. }
  98. }
  99. }
  100. return array_unique($classes);
  101. }
  102. private function findClassInPath(string $path, string $class, string $prefix): array
  103. {
  104. if (!$path = realpath($path.'/'.strtr($prefix, '\\_', '//')) ?: realpath($path.'/'.\dirname(strtr($prefix, '\\_', '//'))) ?: realpath($path)) {
  105. return [];
  106. }
  107. $classes = [];
  108. $filename = $class.'.php';
  109. foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
  110. if ($filename == $file->getFileName() && $class = $this->convertFileToClass($path, $file->getPathName(), $prefix)) {
  111. $classes[] = $class;
  112. }
  113. }
  114. return $classes;
  115. }
  116. private function convertFileToClass(string $path, string $file, string $prefix): ?string
  117. {
  118. $candidates = [
  119. // namespaced class
  120. $namespacedClass = str_replace([$path.\DIRECTORY_SEPARATOR, '.php', '/'], ['', '', '\\'], $file),
  121. // namespaced class (with target dir)
  122. $prefix.$namespacedClass,
  123. // namespaced class (with target dir and separator)
  124. $prefix.'\\'.$namespacedClass,
  125. // PEAR class
  126. str_replace('\\', '_', $namespacedClass),
  127. // PEAR class (with target dir)
  128. str_replace('\\', '_', $prefix.$namespacedClass),
  129. // PEAR class (with target dir and separator)
  130. str_replace('\\', '_', $prefix.'\\'.$namespacedClass),
  131. ];
  132. if ($prefix) {
  133. $candidates = array_filter($candidates, function ($candidate) use ($prefix) { return 0 === strpos($candidate, $prefix); });
  134. }
  135. // We cannot use the autoloader here as most of them use require; but if the class
  136. // is not found, the new autoloader call will require the file again leading to a
  137. // "cannot redeclare class" error.
  138. foreach ($candidates as $candidate) {
  139. if ($this->classExists($candidate)) {
  140. return $candidate;
  141. }
  142. }
  143. try {
  144. require_once $file;
  145. } catch (\Throwable $e) {
  146. return null;
  147. }
  148. foreach ($candidates as $candidate) {
  149. if ($this->classExists($candidate)) {
  150. return $candidate;
  151. }
  152. }
  153. return null;
  154. }
  155. private function classExists(string $class): bool
  156. {
  157. return class_exists($class, false) || interface_exists($class, false) || trait_exists($class, false);
  158. }
  159. }