From b6e0a9246d4d524ec701428e4e04faa100497f2f Mon Sep 17 00:00:00 2001 From: Sergio Santoro Date: Sun, 24 May 2015 01:17:47 +0200 Subject: [PATCH] [HttpKernel][Bundle] Check extension implements ExtensionInterface - Avoid fatal errors on line 89 (calling getAlias on objects of unknown type). - Help developers solve problems with their extensions --- .../Component/HttpKernel/Bundle/Bundle.php | 7 +++++++ .../HttpKernel/Tests/Bundle/BundleTest.php | 11 ++++++++++ .../ExtensionNotValidExtension.php | 20 +++++++++++++++++++ .../ExtensionNotValidBundle.php | 18 +++++++++++++++++ 4 files changed, 56 insertions(+) create mode 100644 src/Symfony/Component/HttpKernel/Tests/Fixtures/ExtensionNotValidBundle/DependencyInjection/ExtensionNotValidExtension.php create mode 100644 src/Symfony/Component/HttpKernel/Tests/Fixtures/ExtensionNotValidBundle/ExtensionNotValidBundle.php diff --git a/src/Symfony/Component/HttpKernel/Bundle/Bundle.php b/src/Symfony/Component/HttpKernel/Bundle/Bundle.php index 51070c5963..43adbb0bfd 100644 --- a/src/Symfony/Component/HttpKernel/Bundle/Bundle.php +++ b/src/Symfony/Component/HttpKernel/Bundle/Bundle.php @@ -78,6 +78,13 @@ abstract class Bundle extends ContainerAware implements BundleInterface if (class_exists($class)) { $extension = new $class(); + if (!$extension instanceof ExtensionInterface) { + throw new \LogicException(sprintf( + 'Extension %s must implement Symfony\Component\DependencyInjection\Extension\ExtensionInterface.', + $class + )); + } + // check naming convention $expectedAlias = Container::underscore($basename); if ($expectedAlias != $extension->getAlias()) { diff --git a/src/Symfony/Component/HttpKernel/Tests/Bundle/BundleTest.php b/src/Symfony/Component/HttpKernel/Tests/Bundle/BundleTest.php index 1a1b30097c..c9059a74a1 100644 --- a/src/Symfony/Component/HttpKernel/Tests/Bundle/BundleTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/Bundle/BundleTest.php @@ -11,6 +11,7 @@ namespace Symfony\Component\HttpKernel\Tests\Bundle; +use Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionNotValidBundle\ExtensionNotValidBundle; use Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionPresentBundle\ExtensionPresentBundle; use Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionAbsentBundle\ExtensionAbsentBundle; use Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionPresentBundle\Command\FooCommand; @@ -30,4 +31,14 @@ class BundleTest extends \PHPUnit_Framework_TestCase $this->assertNull($bundle2->registerCommands($app)); } + + /** + * @expectedException \LogicException + * @expectedExceptionMessage must implement Symfony\Component\DependencyInjection\Extension\ExtensionInterface + */ + public function testGetContainerExtensionWithInvalidClass() + { + $bundle = new ExtensionNotValidBundle(); + $bundle->getContainerExtension(); + } } diff --git a/src/Symfony/Component/HttpKernel/Tests/Fixtures/ExtensionNotValidBundle/DependencyInjection/ExtensionNotValidExtension.php b/src/Symfony/Component/HttpKernel/Tests/Fixtures/ExtensionNotValidBundle/DependencyInjection/ExtensionNotValidExtension.php new file mode 100644 index 0000000000..0fd64316fb --- /dev/null +++ b/src/Symfony/Component/HttpKernel/Tests/Fixtures/ExtensionNotValidBundle/DependencyInjection/ExtensionNotValidExtension.php @@ -0,0 +1,20 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionNotValidBundle\DependencyInjection; + +class ExtensionNotValidExtension +{ + public function getAlias() + { + return 'extension_not_valid'; + } +} diff --git a/src/Symfony/Component/HttpKernel/Tests/Fixtures/ExtensionNotValidBundle/ExtensionNotValidBundle.php b/src/Symfony/Component/HttpKernel/Tests/Fixtures/ExtensionNotValidBundle/ExtensionNotValidBundle.php new file mode 100644 index 0000000000..34e2920392 --- /dev/null +++ b/src/Symfony/Component/HttpKernel/Tests/Fixtures/ExtensionNotValidBundle/ExtensionNotValidBundle.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionNotValidBundle; + +use Symfony\Component\HttpKernel\Bundle\Bundle; + +class ExtensionNotValidBundle extends Bundle +{ +}