Skip to content

Commit f4493e6

Browse files
committed
[HttpKernel] Add entry point to more easily create/configure the DI extension
1 parent ac389b6 commit f4493e6

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ public function build(ContainerBuilder $container)
7272
public function getContainerExtension()
7373
{
7474
if (null === $this->extension) {
75-
$class = $this->getContainerExtensionClass();
76-
if (class_exists($class)) {
77-
$extension = new $class();
75+
$extension = $this->createContainerExtension();
7876

77+
if (null !== $extension) {
7978
// check naming convention
8079
$basename = preg_replace('/Bundle$/', '', $this->getName());
8180
$expectedAlias = Container::underscore($basename);
81+
8282
if ($expectedAlias != $extension->getAlias()) {
8383
throw new \LogicException(sprintf(
8484
'Users will expect the alias of the default extension of a bundle to be the underscored version of the bundle name ("%s"). You can override "Bundle::getContainerExtension()" if you want to use "%s" or another alias.',
@@ -208,4 +208,16 @@ protected function getContainerExtensionClass()
208208

209209
return $this->getNamespace().'\\DependencyInjection\\'.$basename.'Extension';
210210
}
211+
212+
/**
213+
* Creates the bundle's container extension.
214+
*
215+
* @return ExtensionInterface|null
216+
*/
217+
protected function createContainerExtension()
218+
{
219+
if (class_exists($class = $this->getContainerExtensionClass())) {
220+
return new $class();
221+
}
222+
}
211223
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@
1717

1818
class BundleTest extends \PHPUnit_Framework_TestCase
1919
{
20+
public function testGetContainerExtension()
21+
{
22+
$bundle = new ExtensionPresentBundle();
23+
24+
$this->assertInstanceOf(
25+
'Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionPresentBundle\DependencyInjection\ExtensionPresentExtension',
26+
$bundle->getContainerExtension()
27+
);
28+
}
29+
2030
public function testRegisterCommands()
2131
{
2232
$cmd = new FooCommand();

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy