[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
This commit is contained in:
Sergio Santoro 2015-05-24 01:17:47 +02:00 committed by Fabien Potencier
parent cc749a67f6
commit b6e0a9246d
4 changed files with 56 additions and 0 deletions

View File

@ -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()) {

View File

@ -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();
}
}

View File

@ -0,0 +1,20 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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';
}
}

View File

@ -0,0 +1,18 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* 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
{
}