PageRenderTime 29ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/Pasantias/pasantiasASLG
PHP | 212 lines | 163 code | 15 blank | 34 comment | 16 complexity | d7c83ada98b868b2bf5d41a26bb20ac8 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 and PSR-4 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. // @deprecated since version 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. if ($function[0] instanceof ComposerClassLoader) {
  107. foreach ($function[0]->getPrefixesPsr4() as $prefix => $paths) {
  108. foreach ($paths as $path) {
  109. $classes = array_merge($classes, $this->findClassInPath($path, $class, $prefix));
  110. }
  111. }
  112. }
  113. }
  114. return array_unique($classes);
  115. }
  116. /**
  117. * @param string $path
  118. * @param string $class
  119. * @param string $prefix
  120. *
  121. * @return array
  122. */
  123. private function findClassInPath($path, $class, $prefix)
  124. {
  125. if (!$path = realpath($path.'/'.strtr($prefix, '\\_', '//')) ?: realpath($path.'/'.dirname(strtr($prefix, '\\_', '//'))) ?: realpath($path)) {
  126. return array();
  127. }
  128. $classes = array();
  129. $filename = $class.'.php';
  130. foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
  131. if ($filename == $file->getFileName() && $class = $this->convertFileToClass($path, $file->getPathName(), $prefix)) {
  132. $classes[] = $class;
  133. }
  134. }
  135. return $classes;
  136. }
  137. /**
  138. * @param string $path
  139. * @param string $file
  140. * @param string $prefix
  141. *
  142. * @return string|null
  143. */
  144. private function convertFileToClass($path, $file, $prefix)
  145. {
  146. $candidates = array(
  147. // namespaced class
  148. $namespacedClass = str_replace(array($path.DIRECTORY_SEPARATOR, '.php', '/'), array('', '', '\\'), $file),
  149. // namespaced class (with target dir)
  150. $prefix.$namespacedClass,
  151. // namespaced class (with target dir and separator)
  152. $prefix.'\\'.$namespacedClass,
  153. // PEAR class
  154. str_replace('\\', '_', $namespacedClass),
  155. // PEAR class (with target dir)
  156. str_replace('\\', '_', $prefix.$namespacedClass),
  157. // PEAR class (with target dir and separator)
  158. str_replace('\\', '_', $prefix.'\\'.$namespacedClass),
  159. );
  160. if ($prefix) {
  161. $candidates = array_filter($candidates, function ($candidate) use ($prefix) {return 0 === strpos($candidate, $prefix);});
  162. }
  163. // We cannot use the autoloader here as most of them use require; but if the class
  164. // is not found, the new autoloader call will require the file again leading to a
  165. // "cannot redeclare class" error.
  166. foreach ($candidates as $candidate) {
  167. if ($this->classExists($candidate)) {
  168. return $candidate;
  169. }
  170. }
  171. require_once $file;
  172. foreach ($candidates as $candidate) {
  173. if ($this->classExists($candidate)) {
  174. return $candidate;
  175. }
  176. }
  177. }
  178. /**
  179. * @param string $class
  180. *
  181. * @return bool
  182. */
  183. private function classExists($class)
  184. {
  185. return class_exists($class, false) || interface_exists($class, false) || (function_exists('trait_exists') && trait_exists($class, false));
  186. }
  187. }