Skip to content

Commit 855f82d

Browse files
committed
Simplifying bundle extension/config definition
1 parent 25a5eb2 commit 855f82d

File tree

14 files changed

+533
-2
lines changed

14 files changed

+533
-2
lines changed

src/Symfony/Component/Config/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
6.1
5+
---
6+
7+
* Add `DefinitionFileLoader` class to load a TreeBuilder definition from an external file
8+
* Add `DefinitionConfigurator` helper
9+
410
6.0
511
---
612

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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\Config\Definition\Configurator;
13+
14+
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
15+
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
16+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
17+
use Symfony\Component\Config\Definition\Loader\DefinitionFileLoader;
18+
19+
/**
20+
* @author Yonel Ceruto <yonelceruto@gmail.com>
21+
*/
22+
class DefinitionConfigurator
23+
{
24+
private TreeBuilder $treeBuilder;
25+
private DefinitionFileLoader $loader;
26+
private string $path;
27+
private string $file;
28+
29+
public function __construct(TreeBuilder $treeBuilder, DefinitionFileLoader $loader, string $path, string $file)
30+
{
31+
$this->treeBuilder = $treeBuilder;
32+
$this->loader = $loader;
33+
$this->path = $path;
34+
$this->file = $file;
35+
}
36+
37+
final public function import(string $resource, string $type = null, bool $ignoreErrors = false): void
38+
{
39+
$this->loader->setCurrentDir(\dirname($this->path));
40+
$this->loader->import($resource, $type, $ignoreErrors, $this->file);
41+
}
42+
43+
final public function rootNode(): NodeDefinition|ArrayNodeDefinition
44+
{
45+
return $this->treeBuilder->getRootNode();
46+
}
47+
48+
final public function setPathSeparator(string $separator): void
49+
{
50+
$this->treeBuilder->setPathSeparator($separator);
51+
}
52+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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\Config\Definition\Loader;
13+
14+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
15+
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
16+
use Symfony\Component\Config\FileLocatorInterface;
17+
use Symfony\Component\Config\Loader\FileLoader;
18+
use Symfony\Component\DependencyInjection\ContainerBuilder;
19+
20+
/**
21+
* @author Yonel Ceruto <yonelceruto@gmail.com>
22+
*/
23+
class DefinitionFileLoader extends FileLoader
24+
{
25+
private TreeBuilder $treeBuilder;
26+
private ?ContainerBuilder $container;
27+
28+
public function __construct(TreeBuilder $treeBuilder, FileLocatorInterface $locator, ContainerBuilder $container = null)
29+
{
30+
$this->treeBuilder = $treeBuilder;
31+
$this->container = $container;
32+
33+
parent::__construct($locator);
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
public function load(mixed $resource, string $type = null): mixed
40+
{
41+
// the loader variable are exposed to the included file below
42+
$loader = $this;
43+
44+
$path = $this->locator->locate($resource);
45+
$this->setCurrentDir(\dirname($path));
46+
$this->container?->fileExists($path);
47+
48+
// the closure forbids access to the private scope in the included file
49+
$load = \Closure::bind(static function ($file) use ($loader) {
50+
return include $file;
51+
}, null, ProtectedDefinitionFileLoader::class);
52+
53+
$callback = $load($path);
54+
55+
if (\is_object($callback) && \is_callable($callback)) {
56+
$this->executeCallback($callback, new DefinitionConfigurator($this->treeBuilder, $this, $path, $resource), $path);
57+
}
58+
59+
return null;
60+
}
61+
62+
/**
63+
* {@inheritdoc}
64+
*/
65+
public function supports(mixed $resource, string $type = null): bool
66+
{
67+
if (!\is_string($resource)) {
68+
return false;
69+
}
70+
71+
if (null === $type && 'php' === pathinfo($resource, \PATHINFO_EXTENSION)) {
72+
return true;
73+
}
74+
75+
return 'php' === $type;
76+
}
77+
78+
private function executeCallback(callable $callback, DefinitionConfigurator $configurator, string $path): void
79+
{
80+
if (!$callback instanceof \Closure) {
81+
$callback = \Closure::fromCallable($callback);
82+
}
83+
84+
$arguments = [];
85+
$r = new \ReflectionFunction($callback);
86+
87+
foreach ($r->getParameters() as $parameter) {
88+
$reflectionType = $parameter->getType();
89+
90+
if (!$reflectionType instanceof \ReflectionNamedType) {
91+
throw new \InvalidArgumentException(sprintf('Could not resolve argument "$%s" for "%s". You must typehint it (for example with "%s").', $parameter->getName(), $path, DefinitionConfigurator::class));
92+
}
93+
94+
$arguments[] = match ($reflectionType->getName()) {
95+
DefinitionConfigurator::class => $configurator,
96+
TreeBuilder::class => $this->treeBuilder,
97+
FileLoader::class, self::class => $this,
98+
};
99+
}
100+
101+
$callback(...$arguments);
102+
}
103+
}
104+
105+
/**
106+
* @internal
107+
*/
108+
final class ProtectedDefinitionFileLoader extends DefinitionFileLoader
109+
{
110+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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\Config\Tests\Definition\Loader;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Config\Definition\BaseNode;
16+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
17+
use Symfony\Component\Config\Definition\Loader\DefinitionFileLoader;
18+
use Symfony\Component\Config\FileLocator;
19+
20+
class DefinitionFileLoaderTest extends TestCase
21+
{
22+
public function testSupports()
23+
{
24+
$loader = new DefinitionFileLoader(new TreeBuilder('test'), new FileLocator());
25+
26+
$this->assertTrue($loader->supports('foo.php'), '->supports() returns true if the resource is loadable');
27+
$this->assertFalse($loader->supports('foo.foo'), '->supports() returns false if the resource is not loadable');
28+
$this->assertTrue($loader->supports('with_wrong_ext.yml', 'php'), '->supports() returns true if the resource with forced type is loadable');
29+
}
30+
31+
public function testLoad()
32+
{
33+
$loader = new DefinitionFileLoader($treeBuilder = new TreeBuilder('test'), new FileLocator());
34+
$loader->load(__DIR__.'/../../Fixtures/Loader/node_simple.php');
35+
36+
$children = $treeBuilder->buildTree()->getChildren();
37+
38+
$this->assertArrayHasKey('foo', $children);
39+
$this->assertInstanceOf(BaseNode::class, $children['foo']);
40+
$this->assertSame('test.foo', $children['foo']->getPath(), '->load() loads a PHP file resource');
41+
}
42+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
4+
5+
return static function (TreeBuilder $treeBuilder) {
6+
$treeBuilder->getRootNode()
7+
->children()
8+
->scalarNode('foo')->end()
9+
->end();
10+
};
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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\HttpKernel\Bundle;
13+
14+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
15+
use Symfony\Component\Config\Definition\ConfigurationInterface;
16+
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
17+
use Symfony\Component\Config\Definition\Loader\DefinitionFileLoader;
18+
use Symfony\Component\Config\FileLocator;
19+
use Symfony\Component\DependencyInjection\ContainerBuilder;
20+
21+
/**
22+
* @author Yonel Ceruto <yonelceruto@gmail.com>
23+
*
24+
* @internal
25+
*/
26+
class BundleConfiguration implements ConfigurationInterface
27+
{
28+
private MicroBundle $bundle;
29+
private ContainerBuilder $container;
30+
private string $alias;
31+
32+
public function __construct(MicroBundle $bundle, ContainerBuilder $container, string $alias)
33+
{
34+
$this->bundle = $bundle;
35+
$this->container = $container;
36+
$this->alias = $alias;
37+
}
38+
39+
public function getConfigTreeBuilder(): TreeBuilder
40+
{
41+
$treeBuilder = new TreeBuilder($this->alias);
42+
$file = (new \ReflectionObject($this->bundle))->getFileName();
43+
$loader = new DefinitionFileLoader($treeBuilder, new FileLocator(\dirname($file)), $this->container);
44+
$configurator = new DefinitionConfigurator($treeBuilder, $loader, $file, $file);
45+
46+
$this->bundle->configuration($configurator);
47+
48+
return $treeBuilder;
49+
}
50+
}

0 commit comments

Comments
 (0)