8000 [Debug] Fix ClassNotFoundFatalErrorHandler candidates lookups · symfony/symfony@f52e8c3 · GitHub
[go: up one dir, main page]

Skip to content

Commit f52e8c3

Browse files
[Debug] Fix ClassNotFoundFatalErrorHandler candidates lookups
1 parent 386f733 commit f52e8c3

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-10
lines changed

src/Symfony/Component/Debug/FatalErrorHandler/ClassNotFoundFatalErrorHandler.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function handleError(array $error, FatalErrorException $exception)
7777
/**
7878
* Tries to guess the full namespace for a given class name.
7979
*
80-
* By default, it looks for PSR-0 classes registered via a Symfony or a Composer
80+
* By default, it looks for PSR-0 and PSR-4 classes registered via a Symfony or a Composer
8181
* autoloader (that should cover all common cases).
8282
*
8383
* @param string $class A class name (without its namespace)
@@ -101,7 +101,7 @@ private function getClassCandidates($class)
101101
if ($function[0] instanceof DebugClassLoader) {
102102
$function = $function[0]->getClassLoader();
103103

104-
// Since 2.5, returning an object from DebugClassLoader::getClassLoader() is @deprecated
104+
// @deprecated since version 2.5. Returning an object from DebugClassLoader::getClassLoader() is deprecated.
105105
if (is_object($function)) {
106106
$function = array($function);
107107
}
@@ -118,6 +118,13 @@ private function getClassCandidates($class)
118118
}
119119
}
120120
}
121+
if ($function[0] instanceof ComposerClassLoader) {
122+
foreach ($function[0]->getPrefixesPsr4() as $prefix => $paths) {
123+
foreach ($paths as $path) {
124+
$classes = array_merge($classes, $this->findClassInPath($path, $class, $prefix));
125+
}
126+
}
127+
}
121128
}
122129

123130
return array_unique($classes);
@@ -132,13 +139,13 @@ private function getClassCandidates($class)
132139
*/
133140
private function findClassInPath($path, $class, $prefix)
134141
{
135-
if (!$path = realpath($path)) {
142+
if (!$path = realpath($path.'/'.strtr($prefix, '\\_', '//')) ?: realpath($path.'/'.dirname(strtr($prefix, '\\_', '//'))) ?: realpath($path)) {
136143
return array();
137144
}
138145

139146
$classes = array();
140147
$filename = $class.'.php';
141-
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
148+
foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
142149
if ($filename == $file->getFileName() && $class = $this->convertFileToClass($path, $file->getPathName(), $prefix)) {
143150
$classes[] = $class;
144151
}
@@ -160,13 +167,21 @@ private function convertFileToClass($path, $file, $prefix)
160167
// namespaced class
161168
$namespacedClass = str_replace(array($path.DIRECTORY_SEPARATOR, '.php', '/'), array('', '', '\\'), $file),
162169
// namespaced class (with target dir)
163-
$namespacedClassTargetDir = $prefix.str_replace(array($path.DIRECTORY_SEPARATOR, '.php', '/'), array('', '', '\\'), $file),
170+
$prefix.$namespacedClass,
171+
// namespaced class (with target dir and separator)
172+
$prefix.'\\'.$namespacedClass,
164173
// PEAR class
165174
str_replace('\\', '_', $namespacedClass),
166175
// PEAR class (with target dir)
167-
str_replace('\\', '_', $namespacedClassTargetDir),
176+
str_replace('\\', '_', $prefix.$namespacedClass),
177+
// PEAR class (with target dir and separator)
178+
str_replace('\\', '_', $prefix.'\\'.$namespacedClass),
168179
);
169180

181+
if ($prefix) {
182+
$candidates = array_filter($candidates, function ($candidate) use ($prefix) {return 0 === strpos($candidate, $prefix);});
183+
}
184+
170185
// We cannot use the autoloader here as most of them use require; but if the class
171186
// is not found, the new autoloader call will require the file again leading to a
172187
// "cannot redeclare class" error.

src/Symfony/Component/Debug/Tests/FatalErrorHandler/ClassNotFoundFatalErrorHandlerTest.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,30 @@
1515
use Symfony\Component\ClassLoader\UniversalClassLoader as SymfonyUniversalClassLoader;
1616
use Symfony\Component\Debug\Exception\FatalErrorException;
1717
use Symfony\Component\Debug\FatalErrorHandler\ClassNotFoundFatalErrorHandler;
18+
use Symfony\Component\Debug\DebugClassLoader;
19+
use Composer\Autoload\ClassLoader as ComposerClassLoader;
1820

1921
class ClassNotFoundFatalErrorHandlerTest extends \PHPUnit_Framework_TestCase
2022
{
23+
public static function setUpBeforeClass()
24+
{
25+
foreach (spl_autoload_functions() as $function) {
26+
if (!is_array($function)) {
27+
continue;
28+
}
29+
30+
// get class loaders wrapped by DebugClassLoader
31+
if ($function[0] instanceof DebugClassLoader) {
32+
$function = $function[0]->getClassLoader();
33+
}
34+
35+
if ($function[0] instanceof ComposerClassLoader) {
36+
$function[0]->add('Symfony_Component_Debug_Tests_Fixtures', dirname(dirname(dirname(dirname(dirname(__DIR__))))));
37+
break;
38+
}
39+
}
40+
}
41+
2142
/**
2243
* @dataProvider provideClassNotFoundData
2344
*/
@@ -40,8 +61,6 @@ public function testHandleClassNotFound($error, $translatedMessage)
4061
*/
4162
public function testLegacyHandleClassNotFound($error, $translatedMessage, $autoloader)
4263
{
43-
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
44-
4564
// Unregister all autoloaders to ensure the custom provided
4665
// autoloader is the only one to be used during the test run.
4766
$autoloaders = spl_autoload_functions();
@@ -115,13 +134,19 @@ public function provideClassNotFoundData()
115134

116135
public function provideLegacyClassNotFoundData()
117136
{
137+
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
138+
118139
$prefixes = array('Symfony\Component\Debug\Exception\\' => realpath(__DIR__.'/../../Exception'));
119140

120141
$symfonyAutoloader = new SymfonyClassLoader();
121142
$symfonyAutoloader->addPrefixes($prefixes);
122143

123-
$symfonyUniversalClassLoader = new SymfonyUniversalClassLoader();
124-
$symfonyUniversalClassLoader->registerPrefixes($prefixes);
144+
if (class_exists('Symfony\Component\ClassLoader\UniversalClassLoader')) {
145+
$symfonyUniversalClassLoader = new SymfonyUniversalClassLoader();
146+
$symfonyUniversalClassLoader->registerPrefixes($prefixes);
147+
} else {
148+
$symfonyUniversalClassLoader = $symfonyAutoloader;
149+
}
125150

126151
return array(
127152
array(

0 commit comments

Comments
 (0)
0