Skip to content

Commit b6e0a92

Browse files
taueresfabpot
authored andcommitted
[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
1 parent cc749a6 commit b6e0a92

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

src/Symfony/Component/HttpKernel/Bundle/Bundle.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ public function getContainerExtension()
7878
if (class_exists($class)) {
7979
$extension = new $class();
8080

81+
if (!$extension instanceof ExtensionInterface) {
82+
throw new \LogicException(sprintf(
83+
'Extension %s must implement Symfony\Component\DependencyInjection\Extension\ExtensionInterface.',
84+
$class
85+
));
86+
}
87+
8188
// check naming convention
8289
$expectedAlias = Container::underscore($basename);
8390
if ($expectedAlias != $extension->getAlias()) {

src/Symfony/Component/HttpKernel/Tests/Bundle/BundleTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\HttpKernel\Tests\Bundle;
1313

14+
use Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionNotValidBundle\ExtensionNotValidBundle;
1415
use Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionPresentBundle\ExtensionPresentBundle;
1516
use Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionAbsentBundle\ExtensionAbsentBundle;
1617
use Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionPresentBundle\Command\FooCommand;
@@ -30,4 +31,14 @@ public function testRegisterCommands()
3031

3132
$this->assertNull($bundle2->registerCommands($app));
3233
}
34+
35+
/**
36+
* @expectedException \LogicException
37+
* @expectedExceptionMessage must implement Symfony\Component\DependencyInjection\Extension\ExtensionInterface
38+
*/
39+
public function testGetContainerExtensionWithInvalidClass()
40+
{
41+
$bundle = new ExtensionNotValidBundle();
42+
$bundle->getContainerExtension();
43+
}
3344
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionNotValidBundle\DependencyInjection;
13+
14+
class ExtensionNotValidExtension
15+
{
16+
public function getAlias()
17+
{
18+
return 'extension_not_valid';
19+
}
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionNotValidBundle;
13+
14+
use Symfony\Component\HttpKernel\Bundle\Bundle;
15+
16+
class ExtensionNotValidBundle extends Bundle
17+
{
18+
}

0 commit comments

Comments
 (0)