feature #13616 [HttpKernel] Add entry point to more easily create/configure the DI extension (egeloen)
This PR was submitted for the 2.7 branch but it was merged into the 2.8 branch instead (closes #13616).
Discussion
----------
[HttpKernel] Add entry point to more easily create/configure the DI extension
| Q | A
| ------------- | ---
| Bug fix? | no
| New feature? | yes
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | ~
| License | MIT
| Doc PR | ~
Hey!
I would like to pass some dependencies to the DI extension through the bundle. In the current implementation, I need to duplicate the whole `getContainerExtension` in order to keep the extension alias check. This PR proposes to introduce an entry point for more easily do it.
What do you think?
Commits
-------
9bae1ae
[HttpKernel] Add entry point to more easily create/configure the DI extension
This commit is contained in:
commit
95ccd3bb34
@ -72,17 +72,17 @@ abstract class Bundle extends ContainerAware implements BundleInterface
|
|||||||
public function getContainerExtension()
|
public function getContainerExtension()
|
||||||
{
|
{
|
||||||
if (null === $this->extension) {
|
if (null === $this->extension) {
|
||||||
$class = $this->getContainerExtensionClass();
|
$extension = $this->createContainerExtension();
|
||||||
if (class_exists($class)) {
|
|
||||||
$extension = new $class();
|
|
||||||
|
|
||||||
if (!$extension instanceof ExtensionInterface) {
|
if (!$extension instanceof ExtensionInterface) {
|
||||||
throw new \LogicException(sprintf('Extension %s must implement Symfony\Component\DependencyInjection\Extension\ExtensionInterface.', $class));
|
throw new \LogicException(sprintf('Extension %s must implement Symfony\Component\DependencyInjection\Extension\ExtensionInterface.', get_class($extension)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (null !== $extension) {
|
||||||
// check naming convention
|
// check naming convention
|
||||||
$basename = preg_replace('/Bundle$/', '', $this->getName());
|
$basename = preg_replace('/Bundle$/', '', $this->getName());
|
||||||
$expectedAlias = Container::underscore($basename);
|
$expectedAlias = Container::underscore($basename);
|
||||||
|
|
||||||
if ($expectedAlias != $extension->getAlias()) {
|
if ($expectedAlias != $extension->getAlias()) {
|
||||||
throw new \LogicException(sprintf(
|
throw new \LogicException(sprintf(
|
||||||
'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.',
|
'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.',
|
||||||
@ -212,4 +212,16 @@ abstract class Bundle extends ContainerAware implements BundleInterface
|
|||||||
|
|
||||||
return $this->getNamespace().'\\DependencyInjection\\'.$basename.'Extension';
|
return $this->getNamespace().'\\DependencyInjection\\'.$basename.'Extension';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates the bundle's container extension.
|
||||||
|
*
|
||||||
|
* @return ExtensionInterface|null
|
||||||
|
*/
|
||||||
|
protected function createContainerExtension()
|
||||||
|
{
|
||||||
|
if (class_exists($class = $this->getContainerExtensionClass())) {
|
||||||
|
return new $class();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,16 @@ use Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionPresentBundle\Command\F
|
|||||||
|
|
||||||
class BundleTest extends \PHPUnit_Framework_TestCase
|
class BundleTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
|
public function testGetContainerExtension()
|
||||||
|
{
|
||||||
|
$bundle = new ExtensionPresentBundle();
|
||||||
|
|
||||||
|
$this->assertInstanceOf(
|
||||||
|
'Symfony\Component\HttpKernel\Tests\Fixtures\ExtensionPresentBundle\DependencyInjection\ExtensionPresentExtension',
|
||||||
|
$bundle->getContainerExtension()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public function testRegisterCommands()
|
public function testRegisterCommands()
|
||||||
{
|
{
|
||||||
$cmd = new FooCommand();
|
$cmd = new FooCommand();
|
||||||
|
Reference in New Issue
Block a user