8000 [HttpKernel] Simplifying Bundle/Extension config definition by yceruto · Pull Request #43701 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[HttpKernel] Simplifying Bundle/Extension config definition #43701

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Symfony/Component/Config/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ CHANGELOG

* Allow using environment variables in `EnumNode`
* Add Node's information in generated Config
* Add `DefinitionFileLoader` class to load a TreeBuilder definition from an external file
* Add `DefinitionConfigurator` helper

6.0
---
Expand Down
25 changes: 25 additions & 0 deletions src/Symfony/Component/Config/Definition/ConfigurableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Config\Definition;

use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;

/**
* @author Yonel Ceruto <yonelceruto@gmail.com>
*/
interface ConfigurableInterface
{
/**
* Generates the configuration tree builder.
*/
public function configure(DefinitionConfigurator $definition): void;
}
45 changes: 45 additions & 0 deletions src/Symfony/Component/Config/Definition/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Config\Definition;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
use Symfony\Component\Config\Definition\Loader\DefinitionFileLoader;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ 8000 ContainerBuilder;

/**
* @author Yonel Ceruto <yonelceruto@gmail.com>
*
* @final
*/
class Configuration implements ConfigurationInterface
{
public function __construct(
private ConfigurableInterface $subject,
private ?ContainerBuilder $container,
private string $alias,
) {
}

public function getConfigTreeBuilder(): TreeBuilder
{
$treeBuilder = new TreeBuilder($this->alias);
$file = (new \ReflectionObject($this->subject))->getFileName();
$loader = new DefinitionFileLoader($treeBuilder, new FileLocator(\dirname($file)), $this->container);
$configurator = new DefinitionConfigurator($treeBuilder, $loader, $file, $file);

$this->subject->configure($configurator);

return $treeBuilder;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Config\Definition\Configurator;

use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\Loader\DefinitionFileLoader;

/**
* @author Yonel Ceruto <yonelceruto@gmail.com>
*/
class DefinitionConfigurator
{
public function __construct(
private TreeBuilder $treeBuilder,
private DefinitionFileLoader $loader,
private string $path,
private string $file,
) {
}

public function import(string $resource, string $type = null, bool $ignoreErrors = false): void
{
$this->loader->setCurrentDir(\dirname($this->path));
$this->loader->import($resource, $type, $ignoreErrors, $this->file);
}

public function rootNode(): NodeDefinition|ArrayNodeDefinition
{
return $this->treeBuilder->getRootNode();
}

public function setPathSeparator(string $separator): void
{
$this->treeBuilder->setPathSeparator($separator);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Config\Definition\Loader;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
use Symfony\Component\Config\FileLocatorInterface;
use Symfony\Component\Config\Loader\FileLoader;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* DefinitionFileLoader loads config definitions from a PHP file.
*
* The PHP file is required.
*
* @author Yonel Ceruto <yonelceruto@gmail.com>
*/
class DefinitionFileLoader extends FileLoader
{
public function __construct(
private TreeBuilder $treeBuilder,
FileLocatorInterface $locator,
private ?ContainerBuilder $container = null,
) {
parent::__construct($locator);
}

/**
* {@inheritdoc}
*/
public function load(mixed $resource, string $type = null): mixed
{
// the loader variable is exposed to the included file below
$loader = $this;

$path = $this->locator->locate($resource);
$this->setCurrentDir(\dirname($path));
$this->container?->fileExists($path);

// the closure forbids access to the private scope in the included file
$load = \Closure::bind(static function ($file) use ($loader) {
return include $file;
}, null, ProtectedDefinitionFileLoader::class);

$callback = $load($path);

if (\is_object($callback) && \is_callable($callback)) {
$this->executeCallback($callback, new DefinitionConfigurator($this->treeBuilder, $this, $path, $resource), $path);
}

return null;
}

/**
* {@inheritdoc}
*/
public function supports(mixed $resource, string $type = null): bool
{
if (!\is_string($resource)) {
return false;
}

if (null === $type && 'php' === pathinfo($resource, \PATHINFO_EXTENSION)) {
return true;
}

return 'php' === $type;
}

private function executeCallback(callable $callback, DefinitionConfigurator $configurator, string $path): void
{
$callback = $callback(...);

$arguments = [];
$r = new \ReflectionFunction($callback);

foreach ($r->getParameters() as $parameter) {
$reflectionType = $parameter->getType();

if (!$reflectionType instanceof \ReflectionNamedType) {
throw new \InvalidArgumentException(sprintf('Could not resolve argument "$%s" for "%s". You must typehint it (for example with "%s").', $parameter->getName(), $path, DefinitionConfigurator::class));
}

$arguments[] = match ($reflectionType->getName()) {
DefinitionConfigurator::class => $configurator,
TreeBuilder::class => $this->treeBuilder,
FileLoader::class, self::class => $this,
};
}

$callback(...$arguments);
}
}

/**
* @internal
*/
final class ProtectedDefinitionFileLoader extends DefinitionFileLoader
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Config\Tests\Definition\Loader;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\BaseNode;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\Loader\DefinitionFileLoader;
use Symfony\Component\Config\FileLocator;

class DefinitionFileLoaderTest extends TestCase
{
public function testSupports()
{
$loader = new DefinitionFileLoader(new TreeBuilder('test'), new FileLocator());

$this->assertTrue($loader->supports('foo.php'), '->supports() returns true if the resource is loadable');
$this->assertFalse($loader->supports('foo.foo'), '->supports() returns false if the resource is not loadable');
$this->assertTrue($loader->supports('with_wrong_ext.yml', 'php'), '->supports() returns true if the resource with forced type is loadable');
}

public function testLoad()
{
$loader = new DefinitionFileLoader($treeBuilder = new TreeBuilder('test'), new FileLocator());
$loader->load(__DIR__.'/../../Fixtures/Loader/node_simple.php');

$children = $treeBuilder->buildTree()->getChildren();

$this->assertArrayHasKey('foo', $children);
$this->assertInstanceOf(BaseNode::class, $children['foo']);
$this->assertSame('test.foo', $children['foo']->getPath(), '->load() loads a PHP file resource');
}
}
10 changes: 10 additions & 0 deletions src/Symfony/Component/Config/Tests/Fixtures/Loader/node_simple.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

use Symfony\Component\Config\Definition\Builder\TreeBuilder;

return static function (TreeBuilder $treeBuilder) {
$treeBuilder->getRootNode()
->children()
->scalarNode('foo')->end()
->end();
};
1 change: 1 addition & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CHANGELOG
* Add an `Autowire` attribute to tell a parameter how to be autowired
* Allow using expressions as service factories
* Deprecate `ReferenceSetArgumentTrait`
* Add `AbstractExtension` class for DI configuration/definition on a single file

6.0
---
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Extension;

use Symfony\Component\Config\Definition\Configuration;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

/**
* An Extension that provides configuration hooks.
*
* @author Yonel Ceruto <yonelceruto@gmail.com>
*/
abstract class AbstractExtension extends Extension implements ConfigurableExtensionInterface, PrependExtensionInterface
{
use ExtensionTrait;

public function configure(DefinitionConfigurator $definition): void
{
}

public function prependExtension(ContainerConfigurator $container, ContainerBuilder $builder): void
{
}

public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void
{
}

public function getConfiguration(array $config, ContainerBuilder $container): ?ConfigurationInterface
{
return new Configuration($this, $container, $this->getAlias());
}

final public function prepend(ContainerBuilder $container): void
{
$callback = function (ContainerConfigurator $configurator) use ($container) {
$this->prependExtension($configurator, $container);
};

$this->executeConfiguratorCallback($container, $callback, $this);
}

final public function load(array $configs, ContainerBuilder $container): void
{
$config = $this->processConfiguration($this->getConfiguration([], $container), $configs);

$callback = function (ContainerConfigurator $configurator) use ($config, $container) {
$this->loadExtension($config, $configurator, $container);
};

$this->executeConfiguratorCallback($container, $callback, $this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\DependencyInjection\Extension;

use Symfony\Component\Config\Definition\ConfigurableInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

/**
* @author Yonel Ceruto <yonelceruto@gmail.com>
*/
interface ConfigurableExtensionInterface extends ConfigurableInterface
{
/**
* Allow an extension to prepend the extension configurations.
*/
public function prependExtension(ContainerConfigurator $container, ContainerBuilder $builder): void;

/**
* Loads a specific configuration.
*/
public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void;
}
Loading
0