From 9a2c61780bf62ae46e5b45526a8d236b238d94aa Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Tue, 10 Jul 2012 20:27:54 +0200 Subject: [PATCH] [ClassLoader] fixed order of interfaces in generated class collection caches (closes #4841) --- .../ClassLoader/ClassCollectionLoader.php | 17 +++++++++++++---- .../Tests/ClassCollectionLoaderTest.php | 4 ++++ .../Fixtures/ClassesWithParents/CInterface.php | 4 +++- .../Fixtures/ClassesWithParents/GInterface.php | 7 +++++++ 4 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 src/Symfony/Component/ClassLoader/Tests/Fixtures/ClassesWithParents/GInterface.php diff --git a/src/Symfony/Component/ClassLoader/ClassCollectionLoader.php b/src/Symfony/Component/ClassLoader/ClassCollectionLoader.php index f010e53431d2..7f5bdd1e4754 100644 --- a/src/Symfony/Component/ClassLoader/ClassCollectionLoader.php +++ b/src/Symfony/Component/ClassLoader/ClassCollectionLoader.php @@ -283,12 +283,21 @@ private static function getClassHierarchy(\ReflectionClass $class) } } + return array_merge(self::getInterfaces($class), $classes); + } + + private static function getInterfaces(\ReflectionClass $class) + { + $classes = array(); + foreach ($class->getInterfaces() as $interface) { - if ($interface->isUserDefined() && !isset(self::$seen[$interface->getName()])) { - self::$seen[$interface->getName()] = true; + $classes = array_merge($classes, self::getInterfaces($interface)); + } - array_unshift($classes, $interface); - } + if ($class->isUserDefined() && $class->isInterface() && !isset(self::$seen[$class->getName()])) { + self::$seen[$class->getName()] = true; + + $classes[] = $class; } return $classes; diff --git a/src/Symfony/Component/ClassLoader/Tests/ClassCollectionLoaderTest.php b/src/Symfony/Component/ClassLoader/Tests/ClassCollectionLoaderTest.php index 58d96d8513d5..90eca1dedad1 100644 --- a/src/Symfony/Component/ClassLoader/Tests/ClassCollectionLoaderTest.php +++ b/src/Symfony/Component/ClassLoader/Tests/ClassCollectionLoaderTest.php @@ -13,6 +13,7 @@ use Symfony\Component\ClassLoader\ClassCollectionLoader; +require_once __DIR__.'/Fixtures/ClassesWithParents/GInterface.php'; require_once __DIR__.'/Fixtures/ClassesWithParents/CInterface.php'; require_once __DIR__.'/Fixtures/ClassesWithParents/B.php'; require_once __DIR__.'/Fixtures/ClassesWithParents/A.php'; @@ -25,6 +26,7 @@ class ClassCollectionLoaderTest extends \PHPUnit_Framework_TestCase public function testClassReordering(array $classes) { $expected = array( + 'ClassesWithParents\\GInterface', 'ClassesWithParents\\CInterface', 'ClassesWithParents\\B', 'ClassesWithParents\\A', @@ -45,6 +47,7 @@ public function getDifferentOrders() array(array( 'ClassesWithParents\\A', 'ClassesWithParents\\CInterface', + 'ClassesWithParents\\GInterface', 'ClassesWithParents\\B', )), array(array( @@ -81,6 +84,7 @@ public function testClassWithTraitsReordering(array $classes) require_once __DIR__.'/Fixtures/ClassesWithParents/E.php'; $expected = array( + 'ClassesWithParents\\GInterface', 'ClassesWithParents\\CInterface', 'ClassesWithParents\\CTrait', 'ClassesWithParents\\ATrait', diff --git a/src/Symfony/Component/ClassLoader/Tests/Fixtures/ClassesWithParents/CInterface.php b/src/Symfony/Component/ClassLoader/Tests/Fixtures/ClassesWithParents/CInterface.php index 12074f68f8e2..8eec389be46e 100644 --- a/src/Symfony/Component/ClassLoader/Tests/Fixtures/ClassesWithParents/CInterface.php +++ b/src/Symfony/Component/ClassLoader/Tests/Fixtures/ClassesWithParents/CInterface.php @@ -2,4 +2,6 @@ namespace ClassesWithParents; -interface CInterface {} +interface CInterface extends GInterface +{ +} diff --git a/src/Symfony/Component/ClassLoader/Tests/Fixtures/ClassesWithParents/GInterface.php b/src/Symfony/Component/ClassLoader/Tests/Fixtures/ClassesWithParents/GInterface.php new file mode 100644 index 000000000000..208a19d6d80f --- /dev/null +++ b/src/Symfony/Component/ClassLoader/Tests/Fixtures/ClassesWithParents/GInterface.php @@ -0,0 +1,7 @@ +