-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/Symfony/Component/Config/Definition/ConfigurableInterface.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/Symfony/Component/Config/Definition/Configurator/DefinitionConfigurator.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
src/Symfony/Component/Config/Definition/Loader/DefinitionFileLoader.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
42 changes: 42 additions & 0 deletions
42
src/Symfony/Component/Config/Tests/Definition/Loader/DefinitionFileLoaderTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
src/Symfony/Component/Config/Tests/Fixtures/Loader/node_simple.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/Symfony/Component/DependencyInjection/Extension/AbstractExtension.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/Symfony/Component/DependencyInjection/Extension/ConfigurableExtensionInterface.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
yceruto marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
public function prependExtension(ContainerConfigurator $container, ContainerBuilder $builder): void; | ||
|
||
/** | ||
* Loads a specific configuration. | ||
*/ | ||
public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.