Skip to content

Commit 5887cc6

Browse files
committed
feature #43701 [HttpKernel] Simplifying Bundle/Extension config definition (yceruto)
This PR was merged into the 6.1 branch. Discussion ---------- [HttpKernel] Simplifying Bundle/Extension config definition | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #40259, #42647, symfony/symfony#43080 | License | MIT | Doc PR | - This PR aims to simplify DI extension/configuration definitions at the Bundle level (based on @Nyholm symfony/symfony#40259 (comment)) Currently, the services and configuration definitions have to deal with some conventions: * Create the `DependencyInjection/` directory * Create the `DependencyInjection/Configuration.php` class to define the bundle config. * Create the `DependencyInjection/FooExtension.php` extension class and extend from `Extension` * In the `ExtensionInterface::load()` method to implement we have to: * Process the bundle configuration yourself `Configuration`, `Processor`, etc. * Create the specific `*FileLoader` & `FileLocator` instances to import services definition (have to deal with bundle path) * Prepend/append configs for other extensions requires implementing `PrependExtensionInterface`. * Redefine `Bundle::$name` to change the extension alias. Although it may not be a big problem to follow all these conventions (indeed, we have been doing it for years) it's true that there are limitations and it requires extra work to achieve them. Note: The following improvements don't pretend to deprecate the actual convention (at least so far) but simplify it with some benefits. --- To start using the following improvements your bundle must extend from the new abstract class `AbstractBundle` to autoconfigure all hooks and make this possible inside a bundle class. **The first improvement** offers the possibility to configure your bundle DI extension within the bundle class itself using `loadExtension()` method and the fluent `ContainerConfigurator` helper: ```php class FooBundle extends AbstractBundle { public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void { $container->parameters() ->set('foo', $config['foo']); $container->import('../config/services.php'); if ('bar' === $config['foo']) { $container->services() ->set(Parser::class); } } } ``` This new method `loadExtension()` (a same goal that `ExtensionInterface::load()`) contains now all new benefits you currently love for service definition/import/etc. Keep in mind that this configurator still works with a temporal container, so you can't access any extension config at this point (as before). And, the `$config` argument is the bundle's `Configuration` that you usually process on `ExtensionInterface::load()` but here it's given to you already merged and processed (ready to use). --- **The next improvement** comes when you want to prepend/append an extension config before all extensions are loaded & merged, then use the `prependExtension()` method: ```php class FooBundle extends AbstractBundle { public function prependExtension(ContainerConfigurator $container, ContainerBuilder $builder): void { // prepend $builder->prependExtensionConfig('framework', [ 'cache' => ['prefix_seed' => 'foo/bar'], ]); // append $container->extension('framework', [ 'cache' => ['prefix_seed' => 'foo/bar'], ]) // append from file $container->import('../config/packages/cache.php'); } } ``` This is the improved alternative to `PrependExtensionInterface` that you normally implement on extension classes. But using this method has bonus points, you can now use the `ContainerConfigurator` to append an extension config from an external file in any format (including the new PHP fluent-config feature). --- **Another improvement** is about `Configuration` definition. Here you can manage it directly within the bundle class using the `configuration()` method with new possibilities: ```php class FooBundle extends AbstractBundle { public function configure(DefinitionConfigurator $definition): void { // loads config definition from a file $definition->import('../config/definition.php'); // loads config definition from multiple files (when it's too long you can split it) $definition->import('../config/definition/*.php'); // defines config directly when it's short $definition->rootNode() ->children() ->scalarNode('foo')->defaultValue('bar')->end() ->end() ; } } ``` You don't have to create the `TreeBuilder` instance yourself anymore and remember the proper extension alias. Instead, you will use a new `DefinitionConfigurator` with the possibility to import configuration definitions from an external PHP file, and this config file can now live outside the `src/` directory of the bundle if desired: ```php // Acme/FooBundle/config/definition.php use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator; return static function (DefinitionConfigurator $definition) { $definition->rootNode() ->children() ->scalarNode('foo')->defaultValue('bar')->end() ->end() ; }; ``` And why not, you could also split your definition into several files if it's too long, or simply define the config directly in the method if it's short. --- **Last but not least** you can change the extension alias by redefining a new property that now belongs to the MicroBundle class: ```php class AcmeFooBundle extends AbstractBundle { protected string $extensionAlias = 'foo'; // alias used during the extension config loading // ... } ``` The default alias will be determined from your bundle name (in this case `acme_foo`), so the new way allows you to change that alias without either touching your bundle name or overriding any method. --- Note: The same feature has been implemented in a new `AbstractExtension` class for those applications applying the bundle-less approach and want to define configuration through an extension. Combining all these benefits I believe we gain a more simplified bundle structure while decreasing the learning curve. Commits ------- 7e8cf5df10 Simplifying bundle extension/config definition
2 parents 47c2c57 + 5df9ea2 commit 5887cc6

File tree

10 files changed

+372
-2
lines changed

10 files changed

+372
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ CHANGELOG
1111
* Allow using expressions as service factories
1212
* Add argument type `closure` to help passing closures to services
1313
* Deprecate `ReferenceSetArgumentTrait`
14+
* Add `AbstractExtension` class for DI configuration/definition on a single file
1415

1516
6.0
1617
---

Extension/AbstractExtension.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\DependencyInjection\Extension;
13+
14+
use Symfony\Component\Config\Definition\Configuration;
15+
use Symfony\Component\Config\Definition\ConfigurationInterface;
16+
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
17+
use Symfony\Component\DependencyInjection\ContainerBuilder;
18+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
19+
20+
/**
21+
* An Extension that provides configuration hooks.
22+
*
23+
* @author Yonel Ceruto <yonelceruto@gmail.com>
24+
*/
25+
abstract class AbstractExtension extends Extension implements ConfigurableExtensionInterface, PrependExtensionInterface
26+
{
27+
use ExtensionTrait;
28+
29+
public function configure(DefinitionConfigurator $definition): void
30+
{
31+
}
32+
33+
public function prependExtension(ContainerConfigurator $container, ContainerBuilder $builder): void
34+
{
35+
}
36+
37+
public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void
38+
{
39+
}
40+
41+
public function getConfiguration(array $config, ContainerBuilder $container): ?ConfigurationInterface
42+
{
43+
return new Configuration($this, $container, $this->getAlias());
44+
}
45+
46+
final public function prepend(ContainerBuilder $container): void
47+
{
48+
$callback = function (ContainerConfigurator $configurator) use ($container) {
49+
$this->prependExtension($configurator, $container);
50+
};
51+
52+
$this->executeConfiguratorCallback($container, $callback, $this);
53+
}
54+
55+
final public function load(array $configs, ContainerBuilder $container): void
56+
{
57+
$config = $this->processConfiguration($this->getConfiguration([], $container), $configs);
58+
59+
$callback = function (ContainerConfigurator $configurator) use ($config, $container) {
60+
$this->loadExtension($config, $configurator, $container);
61+
};
62+
63+
$this->executeConfiguratorCallback($container, $callback, $this);
64+
}
65+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\DependencyInjection\Extension;
13+
14+
use Symfony\Component\Config\Definition\ConfigurableInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
17+
18+
/**
19+
* @author Yonel Ceruto <yonelceruto@gmail.com>
20+
*/
21+
interface ConfigurableExtensionInterface extends ConfigurableInterface
22+
{
23+
/**
24+
* Allow an extension to prepend the extension configurations.
25+
*/
26+
public function prependExtension(ContainerConfigurator $container, ContainerBuilder $builder): void;
27+
28+
/**
29+
* Loads a specific configuration.
30+
*/
31+
public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void;
32+
}

Extension/ExtensionTrait.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\DependencyInjection\Extension;
13+
14+
use Symfony\Component\Config\Builder\ConfigBuilderGenerator;
15+
use Symfony\Component\Config\FileLocator;
16+
use Symfony\Component\Config\Loader\DelegatingLoader;
17+
use Symfony\Component\Config\Loader\LoaderResolver;
18+
use Symfony\Component\DependencyInjection\ContainerBuilder;
19+
use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
20+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
21+
use Symfony\Component\DependencyInjection\Loader\DirectoryLoader;
22+
use Symfony\Component\DependencyInjection\Loader\GlobFileLoader;
23+
use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
24+
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
25+
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
26+
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
27+
28+
/**
29+
* @author Yonel Ceruto <yonelceruto@gmail.com>
30+
*/
31+
trait ExtensionTrait
32+
{
33+
private function executeConfiguratorCallback(ContainerBuilder $container, \Closure $callback, ConfigurableExtensionInterface $subject): void
34+
{
35+
$env = $container->getParameter('kernel.environment');
36+
$loader = $this->createContainerLoader($container, $env);
37+
$file = (new \ReflectionObject($subject))->getFileName();
38+
$bundleLoader = $loader->getResolver()->resolve($file);
39+
if (!$bundleLoader instanceof PhpFileLoader) {
40+
throw new \LogicException('Unable to create the ContainerConfigurator.');
41+
}
42+
$bundleLoader->setCurrentDir(\dirname($file));
43+
$instanceof = &\Closure::bind(function &() { return $this->instanceof; }, $bundleLoader, $bundleLoader)();
44+
45+
try {
46+
$callback(new ContainerConfigurator($container, $bundleLoader, $instanceof, $file, $file, $env));
47+
} finally {
48+
$instanceof = [];
49+
$bundleLoader->registerAliasesForSinglyImplementedInterfaces();
50+
}
51+
}
52+
53+
private function createContainerLoader(ContainerBuilder $container, string $env): DelegatingLoader
54+
{
55+
$buildDir = $container->getParameter('kernel.build_dir');
56+
$locator = new FileLocator();
57+
$resolver = new LoaderResolver([
58+
new XmlFileLoader($container, $locator, $env),
59+
new YamlFileLoader($container, $locator, $env),
60+
new IniFileLoader($container, $locator, $env),
61+
new PhpFileLoader($container, $locator, $env, new ConfigBuilderGenerator($buildDir)),
62+
new GlobFileLoader($container, $locator, $env),
63+
new DirectoryLoader($container, $locator, $env),
64+
new ClosureLoader($container, $env),
65+
]);
66+
67+
return new DelegatingLoader($resolver);
68+
}
69+
}
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
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\DependencyInjection\Tests\Extension;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Config\Definition\ConfigurableInterface;
16+
use Symfony\Component\Config\Definition\Configuration;
17+
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
18+
use Symfony\Component\Config\Definition\Processor;
19+
use Symfony\Component\DependencyInjection\ContainerBuilder;
20+
use Symfony\Component\DependencyInjection\Extension\AbstractExtension;
21+
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
22+
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
23+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
24+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
25+
26+
class AbstractExtensionTest extends TestCase
27+
{
28+
public function testConfiguration()
29+
{
30+
$extension = new class() extends AbstractExtension {
31+
public function configure(DefinitionConfigurator $definition): void
32+
{
33+
// load one
34+
$definition->import('../Fixtures/config/definition/foo.php');
35+
36+
// load multiples
37+
$definition->import('../Fixtures/config/definition/multiple/*.php');
38+
39+
// inline
40+
$definition->rootNode()
41+
->children()
42+
->scalarNode('ping')->defaultValue('inline')->end()
43+
->end();
44+
}
45+
};
46+
47+
$expected = [
48+
'foo' => 'one',
49+
'bar' => 'multi',
50+
'baz' => 'multi',
51+
'ping' => 'inline',
52+
];
53+
54+
self::assertSame($expected, $this->processConfiguration($extension));
55+
}
56+
57+
public function testPrependAppendExtensionConfig()
58+
{
59+
$extension = new class() extends AbstractExtension {
60+
public function prependExtension(ContainerConfigurator $container, ContainerBuilder $builder): void
61+
{
62+
// append config
63+
$container->extension('third', ['foo' => 'append']);
64+
65+
// prepend config
66+
$builder->prependExtensionConfig('third', ['foo' => 'prepend']);
67+
}
68+
};
69+
70+
$container = $this->processPrependExtension($extension);
71+
72+
$expected = [
73+
['foo' => 'prepend'],
74+
['foo' => 'bar'],
75+
['foo' => 'append'],
76+
];
77+
78+
self::assertSame($expected, $container->getExtensionConfig('third'));
79+
}
80+
81+
public function testLoadExtension()
82+
{
83+
$extension = new class() extends AbstractExtension {
84+
public function configure(DefinitionConfigurator $definition): void
85+
{
86+
$definition->import('../Fixtures/config/definition/foo.php');
87+
}
88+
89+
public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void
90+
{
91+
$container->parameters()
92+
->set('foo_param', $config)
93+
;
94+
95+
$container->services()
96+
->set('foo_service', \stdClass::class)
97+
;
98+
99+
$container->import('../Fixtures/config/services.php');
100+
}
101+
102+
public function getAlias(): string
103+
{
104+
return 'micro';
105+
}
106+
};
107+
108+
$container = $this->processLoadExtension($extension, [['foo' => 'bar']]);
109+
110+
self::assertSame(['foo' => 'bar'], $container->getParameter('foo_param'));
111+
self::assertTrue($container->hasDefinition('foo_service'));
112+
self::assertTrue($container->hasDefinition('bar_service'));
113+
}
114+
115+
protected function processConfiguration(ConfigurableInterface $configurable): array
116+
{
117+
$configuration = new Configuration($configurable, null, 'micro');
118+
119+
return (new Processor())->process($configuration->getConfigTreeBuilder()->buildTree(), []);
120+
}
121+
122+
protected function processPrependExtension(PrependExtensionInterface $extension): ContainerBuilder
123+
{
124+
$thirdExtension = new class() extends AbstractExtension {
125+
public function configure(DefinitionConfigurator $definition): void
126+
{
127+
$definition->import('../Fixtures/config/definition/foo.php');
128+
}
129+
130+
public function getAlias(): string
131+
{
132+
return 'third';
133+
}
134+
};
135+
136+
$container = $this->createContainerBuilder();
137+
$container->registerExtension($thirdExtension);
138+
$container->loadFromExtension('third', ['foo' => 'bar']);
139+
140+
$extension->prepend($container);
141+
142+
return $container;
143+
}
144+
145+
protected function processLoadExtension(ExtensionInterface $extension, array $configs): ContainerBuilder
146+
{
147+
$container = $this->createContainerBuilder();
148+
149+
$extension->load($configs, $container);
150+
151+
return $container;
152+
}
153+
154+
protected function createContainerBuilder(): ContainerBuilder
155+
{
156+
return new ContainerBuilder(new ParameterBag([
157+
'kernel.environment' => 'test',
158+
'kernel.build_dir' => 'test',
159+
]));
160+
}
161+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
4+
5+
return static function (DefinitionConfigurator $definition) {
6+
$definition->rootNode()
7+
->children()
8+
->scalarNode('foo')->defaultValue('one')->end()
9+
->end()
10+
;
11+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
4+
5+
return static function (DefinitionConfigurator $definition) {
6+
$definition->rootNode()
7+
->children()
8+
->scalarNode('bar')->defaultValue('multi')->end()
9+
->end()
10+
;
11+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
4+
5+
return static function (DefinitionConfigurator $definition) {
6+
$definition->rootNode()
7+
->children()
8+
->scalarNode('baz')->defaultValue('multi')->end()
9+
->end()
10+
;
11+
};

Tests/Fixtures/config/services.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
4+
5+
return static function (ContainerConfigurator $c) {
6+
$c->services()
7+
->set('bar_service', stdClass::class)
8+
;
9+
};

0 commit comments

Comments
 (0)