minor #14735 [HttpKernel][Bundle] Check extension implements ExtensionInterface (taueres)

This PR was submitted for the master branch but it was merged into the 2.3 branch instead (closes #14735).

Discussion
----------

[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

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Commits
-------

b6e0a92 [HttpKernel][Bundle] Check extension implements ExtensionInterface
This commit is contained in:
Fabien Potencier 2015-05-25 09:42:57 +02:00
commit 51b3b83270
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
{
}