Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1b34f2c

Browse files
Merge branch '2.6' into 2.7
* 2.6: [Debug] Fix ClassNotFoundFatalErrorHandler candidates lookups [2.6][Translator] Extend, refactor and simplify Translator tests. Conflicts: src/Symfony/Component/Debug/Tests/FatalErrorHandler/ClassNotFoundFatalErrorHandlerTest.php src/Symfony/Component/Translation/Tests/TranslatorCacheTest.php
2 parents 750792e + d90891a commit 1b34f2c

File tree

4 files changed

+198
-199
lines changed

4 files changed

+198
-199
lines changed

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,13 @@ private function getClassCandidates($class)
139139
*/
140140
private function findClassInPath($path, $class, $prefix)
141141
{
142-
if (!$path = realpath($path)) {
142+
if (!$path = realpath($path.'/'.strtr($prefix, '\\_', '//')) ?: realpath($path.'/'.dirname(strtr($prefix, '\\_', '//'))) ?: realpath($path)) {
143143
return array();
144144
}
145145

146146
$classes = array();
147147
$filename = $class.'.php';
148-
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) {
149149
if ($filename == $file->getFileName() && $class = $this->convertFileToClass($path, $file->getPathName(), $prefix)) {
150150
$classes[] = $class;
151151
}
@@ -167,13 +167,21 @@ private function convertFileToClass($path, $file, $prefix)
167167
// namespaced class
168168
$namespacedClass = str_replace(array($path.DIRECTORY_SEPARATOR, '.php', '/'), array('', '', '\\'), $file),
169169
// namespaced class (with target dir)
170-
$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,
171173
// PEAR class
172174
str_replace('\\', '_', $namespacedClass),
173175
// PEAR class (with target dir)
174-
str_replace('\\', '_', $namespacedClassTargetDir),
176+
str_replace('\\', '_', $prefix.$namespacedClass),
177+
// PEAR class (with target dir and separator)
178+
str_replace('\\', '_', $prefix.'\\'.$namespacedClass),
175179
);
176180

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

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

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static function setUpBeforeClass()
3333
}
3434

3535
if ($function[0] instanceof ComposerClassLoader) {
36-
$function[0]->add('Symfony\Component\Debug\Tests\Fixtures', dirname(dirname(dirname(dirname(dirname(__DIR__))))));
36+
$function[0]->add('Symfony_Component_Debug_Tests_Fixtures', dirname(dirname(dirname(dirname(dirname(__DIR__))))));
3737
break;
3838
}
3939
}
@@ -42,12 +42,25 @@ public static function setUpBeforeClass()
4242
/**
4343
* @dataProvider provideClassNotFoundData
4444
*/
45-
public function testHandleClassNotFound($error, $translatedMessage)
45+
public function testHandleClassNotFound($error, $translatedMessage, $autoloader = null)
4646
{
47+
if ($autoloader) {
48+
// Unregister all autoloaders to ensure the custom provided
49+
// autoloader is the only one to be used during the test run.
50+
$autoloaders = spl_autoload_functions();
51+
array_map('spl_autoload_unregister', $autoloaders);
52+
spl_autoload_register($autoloader);
53+
}
54+
4755
$handler = new ClassNotFoundFatalErrorHandler();
4856

4957
$exception = $handler->handleError($error, new FatalErrorException('', 0, $error['type'], $error['file'], $error['line']));
5058

59+
if ($autoloader) {
60+
spl_autoload_unregister($autoloader);
61+
array_map('spl_autoload_register', $autoloaders);
62+
}
63+
5164
$this->assertInstanceof('Symfony\Component\Debug\Exception\ClassNotFoundException', $exception);
5265
$this->assertSame($translatedMessage, $exception->getMessage());
5366
$this->assertSame($error['type'], $exception->getSeverity());
@@ -56,33 +69,37 @@ public function testHandleClassNotFound($error, $translatedMessage)
5669
}
5770

5871
/**
59-
* @dataProvider provideLegacyClassNotFoundData
6072
* @group legacy
6173
*/
62-
public function testLegacyHandleClassNotFound($error, $translatedMessage, $autoloader)
74+
public function testLegacyHandleClassNotFound()
6375
{
64-
// Unregister all autoloaders to ensure the custom provided
65-
// autoloader is the only one to be used during the test run.
66-
$autoloaders = spl_autoload_functions();
67-
array_map('spl_autoload_unregister', $autoloaders);
68-
spl_autoload_register($autoloader);
69-
70-
$handler = new ClassNotFoundFatalErrorHandler();
71-
72-
$exception = $handler->handleError($error, new FatalErrorException('', 0, $error['type'], $error['file'], $error['line']));
76+
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
7377

74-
spl_autoload_unregister($autoloader);
75-
array_map('spl_autoload_register', $autoloaders);
78+
$prefixes = array('Symfony\Component\Debug\Exception\\' => realpath(__DIR__.'/../../Exception'));
79+
$symfonyUniversalClassLoader = new SymfonyUniversalClassLoader();
80+
$symfonyUniversalClassLoader->registerPrefixes($prefixes);
7681

77-
$this->assertInstanceof('Symfony\Component\Debug\Exception\ClassNotFoundException', $exception);
78-
$this->assertSame($translatedMessage, $exception->getMessage());
79-
$this->assertSame($error['type'], $exception->getSeverity());
80-
$this->assertSame($error['file'], $exception->getFile());
81-
$this->assertSame($error['line'], $exception->getLine());
82+
$this->testHandleClassNotFound(
83+
array(
84+
'type' => 1,
85+
'line' => 12,
86+
'file' => 'foo.php',
87+
'message' => 'Class \'Foo\\Bar\\UndefinedFunctionException\' not found',
88+
),
89+
"Attempted to load class \"UndefinedFunctionException\" from namespace \"Foo\Bar\".\nDid you forget a \"use\" statement for \"Symfony\Component\Debug\Exception\UndefinedFunctionException\"?",
90+
array($symfonyUniversalClassLoader, 'loadClass')
91+
);
8292
}
8393

8494
public function provideClassNotFoundData()
8595
{
96+
$prefixes = array('Symfony\Component\Debug\Exception\\' => realpath(__DIR__.'/../../Exception'));
97+
98+
$symfonyAutoloader = new SymfonyClassLoader();
99+
$symfonyAutoloader->addPrefixes($prefixes);
100+
101+
$debugClassLoader = new DebugClassLoader($symfonyAutoloader);
102+
86103
return array(
87104
array(
88105
array(
@@ -129,26 +146,6 @@ public function provideClassNotFoundData()
129146
),
130147
"Attempted to load class \"UndefinedFunctionException\" from namespace \"Foo\Bar\".\nDid you forget a \"use\" statement for \"Symfony\Component\Debug\Exception\UndefinedFunctionException\"?",
131148
),
132-
);
133-
}
134-
135-
public function provideLegacyClassNotFoundData()
136-
{
137-
$this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED);
138-
139-
$prefixes = array('Symfony\Component\Debug\Exception\\' => realpath(__DIR__.'/../../Exception'));
140-
141-
$symfonyAutoloader = new SymfonyClassLoader();
142-
$symfonyAutoloader->addPrefixes($prefixes);
143-
144-
if (class_exists('Symfony\Component\ClassLoader\UniversalClassLoader')) {
145-
$symfonyUniversalClassLoader = new SymfonyUniversalClassLoader();
146-
$symfonyUniversalClassLoader->registerPrefixes($prefixes);
147-
} else {
148-
$symfonyUniversalClassLoader = $symfonyAutoloader;
149-
}
150-
151-
return array(
152149
array(
153150
array(
154151
'type' => 1,
@@ -167,7 +164,7 @@ public function provideLegacyClassNotFoundData()
167164
'message' => 'Class \'Foo\\Bar\\UndefinedFunctionException\' not found',
168165
),
169166
"Attempted to load class \"UndefinedFunctionException\" from namespace \"Foo\Bar\".\nDid you forget a \"use\" statement for \"Symfony\Component\Debug\Exception\UndefinedFunctionException\"?",
170-
array($symfonyUniversalClassLoader, 'loadClass'),
167+
array($debugClassLoader, 'loadClass'),
171168
),
172169
array(
173170
array(

0 commit comments

Comments
 (0)