4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Debug\FatalErrorHandler;
14 use Symfony\Component\Debug\Exception\ClassNotFoundException;
15 use Symfony\Component\Debug\Exception\FatalErrorException;
16 use Symfony\Component\Debug\DebugClassLoader;
17 use Composer\Autoload\ClassLoader as ComposerClassLoader;
18 use Symfony\Component\ClassLoader\ClassLoader as SymfonyClassLoader;
21 * ErrorHandler for classes that do not exist.
23 * @author Fabien Potencier <fabien@symfony.com>
25 class ClassNotFoundFatalErrorHandler implements FatalErrorHandlerInterface
30 public function handleError(array $error, FatalErrorException $exception)
32 $messageLen = strlen($error['message']);
33 $notFoundSuffix = '\' not found';
34 $notFoundSuffixLen = strlen($notFoundSuffix);
35 if ($notFoundSuffixLen > $messageLen) {
39 if (0 !== substr_compare($error['message'], $notFoundSuffix, -$notFoundSuffixLen)) {
43 foreach (array('class', 'interface', 'trait') as $typeName) {
44 $prefix = ucfirst($typeName).' \'';
45 $prefixLen = strlen($prefix);
46 if (0 !== strpos($error['message'], $prefix)) {
50 $fullyQualifiedClassName = substr($error['message'], $prefixLen, -$notFoundSuffixLen);
51 if (false !== $namespaceSeparatorIndex = strrpos($fullyQualifiedClassName, '\\')) {
52 $className = substr($fullyQualifiedClassName, $namespaceSeparatorIndex + 1);
53 $namespacePrefix = substr($fullyQualifiedClassName, 0, $namespaceSeparatorIndex);
54 $message = sprintf('Attempted to load %s "%s" from namespace "%s".', $typeName, $className, $namespacePrefix);
55 $tail = ' for another namespace?';
57 $className = $fullyQualifiedClassName;
58 $message = sprintf('Attempted to load %s "%s" from the global namespace.', $typeName, $className);
62 if ($candidates = $this->getClassCandidates($className)) {
63 $tail = array_pop($candidates).'"?';
65 $tail = ' for e.g. "'.implode('", "', $candidates).'" or "'.$tail;
67 $tail = ' for "'.$tail;
70 $message .= "\nDid you forget a \"use\" statement".$tail;
72 return new ClassNotFoundException($message, $exception);
77 * Tries to guess the full namespace for a given class name.
79 * By default, it looks for PSR-0 and PSR-4 classes registered via a Symfony or a Composer
80 * autoloader (that should cover all common cases).
82 * @param string $class A class name (without its namespace)
84 * @return array An array of possible fully qualified class names
86 private function getClassCandidates($class)
88 if (!is_array($functions = spl_autoload_functions())) {
92 // find Symfony and Composer autoloaders
95 foreach ($functions as $function) {
96 if (!is_array($function)) {
99 // get class loaders wrapped by DebugClassLoader
100 if ($function[0] instanceof DebugClassLoader) {
101 $function = $function[0]->getClassLoader();
103 if (!is_array($function)) {
108 if ($function[0] instanceof ComposerClassLoader || $function[0] instanceof SymfonyClassLoader) {
109 foreach ($function[0]->getPrefixes() as $prefix => $paths) {
110 foreach ($paths as $path) {
111 $classes = array_merge($classes, $this->findClassInPath($path, $class, $prefix));
115 if ($function[0] instanceof ComposerClassLoader) {
116 foreach ($function[0]->getPrefixesPsr4() as $prefix => $paths) {
117 foreach ($paths as $path) {
118 $classes = array_merge($classes, $this->findClassInPath($path, $class, $prefix));
124 return array_unique($classes);
128 * @param string $path
129 * @param string $class
130 * @param string $prefix
134 private function findClassInPath($path, $class, $prefix)
136 if (!$path = realpath($path.'/'.strtr($prefix, '\\_', '//')) ?: realpath($path.'/'.dirname(strtr($prefix, '\\_', '//'))) ?: realpath($path)) {
141 $filename = $class.'.php';
142 foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
143 if ($filename == $file->getFileName() && $class = $this->convertFileToClass($path, $file->getPathName(), $prefix)) {
152 * @param string $path
153 * @param string $file
154 * @param string $prefix
156 * @return string|null
158 private function convertFileToClass($path, $file, $prefix)
162 $namespacedClass = str_replace(array($path.DIRECTORY_SEPARATOR, '.php', '/'), array('', '', '\\'), $file),
163 // namespaced class (with target dir)
164 $prefix.$namespacedClass,
165 // namespaced class (with target dir and separator)
166 $prefix.'\\'.$namespacedClass,
168 str_replace('\\', '_', $namespacedClass),
169 // PEAR class (with target dir)
170 str_replace('\\', '_', $prefix.$namespacedClass),
171 // PEAR class (with target dir and separator)
172 str_replace('\\', '_', $prefix.'\\'.$namespacedClass),
176 $candidates = array_filter($candidates, function ($candidate) use ($prefix) { return 0 === strpos($candidate, $prefix); });
179 // We cannot use the autoloader here as most of them use require; but if the class
180 // is not found, the new autoloader call will require the file again leading to a
181 // "cannot redeclare class" error.
182 foreach ($candidates as $candidate) {
183 if ($this->classExists($candidate)) {
190 foreach ($candidates as $candidate) {
191 if ($this->classExists($candidate)) {
198 * @param string $class
202 private function classExists($class)
204 return class_exists($class, false) || interface_exists($class, false) || trait_exists($class, false);