Skip to content

Commit 5df9ea2

Browse files
committed
Simplifying bundle extension/config definition
1 parent 92647be commit 5df9ea2

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
@@ -10,6 +10,7 @@ CHANGELOG
1010
* Add an `Autowire` attribute to tell a parameter how to be autowired
1111
* Allow using expressions as service factories
1212
* Deprecate `ReferenceSetArgumentTrait`
13+
* Add `AbstractExtension` class for DI configuration/definition on a single file
1314

1415
6.0
1516
---

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 <[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\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 <[email protected]>
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 <[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\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 <[email protected]>
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 <[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\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 <[email protected]>
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 <[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\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)