Skip to content

Commit 6196c85

Browse files
[DependencyInjection][Config] Remove support for the fluent PHP config format
1 parent 6ce6680 commit 6196c85

File tree

8 files changed

+5
-177
lines changed

8 files changed

+5
-177
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ CHANGELOG
1313
* Remove `!tagged` tag, use `!tagged_iterator` instead
1414
* Add argument `$target` to `ContainerBuilder::registerAliasForArgument()`
1515
* Remove support for the XML configuration format
16+
* Remove the fluent PHP format for semantic configuration, instantiate builders inline with the config array as argument and return them instead
1617

1718
7.4
1819
---

Extension/ExtensionTrait.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Component\DependencyInjection\Extension;
1313

14-
use Symfony\Component\Config\Builder\ConfigBuilderGenerator;
1514
use Symfony\Component\Config\FileLocator;
1615
use Symfony\Component\Config\Loader\DelegatingLoader;
1716
use Symfony\Component\Config\Loader\LoaderResolver;
@@ -51,12 +50,11 @@ private function executeConfiguratorCallback(ContainerBuilder $container, \Closu
5150

5251
private function createContainerLoader(ContainerBuilder $container, string $env, bool $prepend): DelegatingLoader
5352
{
54-
$buildDir = $container->getParameter('kernel.build_dir');
5553
$locator = new FileLocator();
5654
$resolver = new LoaderResolver([
5755
new YamlFileLoader($container, $locator, $env, $prepend),
5856
new IniFileLoader($container, $locator, $env),
59-
class_exists(ConfigBuilderGenerator::class) ? new PhpFileLoader($container, $locator, $env, new ConfigBuilderGenerator($buildDir), $prepend) : new PhpFileLoader($container, $locator, $env, $prepend),
57+
new PhpFileLoader($container, $locator, $env, $prepend),
6058
new GlobFileLoader($container, $locator, $env),
6159
new DirectoryLoader($container, $locator, $env),
6260
new ClosureLoader($container, $env),

Loader/PhpFileLoader.php

Lines changed: 1 addition & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,12 @@
1111

1212
namespace Symfony\Component\DependencyInjection\Loader;
1313

14-
use Symfony\Component\Config\Builder\ConfigBuilderGenerator;
15-
use Symfony\Component\Config\Builder\ConfigBuilderGeneratorInterface;
16-
use Symfony\Component\Config\Builder\ConfigBuilderInterface;
17-
use Symfony\Component\Config\FileLocatorInterface;
1814
use Symfony\Component\Config\Loader\LoaderResolver;
1915
use Symfony\Component\DependencyInjection\Attribute\When;
2016
use Symfony\Component\DependencyInjection\Attribute\WhenNot;
21-
use Symfony\Component\DependencyInjection\Container;
2217
use Symfony\Component\DependencyInjection\ContainerBuilder;
2318
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
2419
use Symfony\Component\DependencyInjection\Exception\LogicException;
25-
use Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface;
26-
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
2720
use Symfony\Component\DependencyInjection\Loader\Configurator\App;
2821
use Symfony\Component\DependencyInjection\Loader\Configurator\AppReference;
2922
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
@@ -40,24 +33,6 @@ class PhpFileLoader extends FileLoader
4033
{
4134
protected bool $autoRegisterAliasesForSinglyImplementedInterfaces = false;
4235

43-
private ?ConfigBuilderGeneratorInterface $generator;
44-
45-
public function __construct(
46-
ContainerBuilder $container,
47-
FileLocatorInterface $locator,
48-
?string $env = null,
49-
ConfigBuilderGeneratorInterface|bool|null $generator = null,
50-
bool $prepend = false,
51-
) {
52-
if (\is_bool($generator)) {
53-
$prepend = $generator;
54-
$generator = null;
55-
}
56-
$this->generator = $generator;
57-
58-
parent::__construct($container, $locator, $env, $prepend);
59-
}
60-
6136
public function load(mixed $resource, ?string $type = null): mixed
6237
{
6338
// the container and loader variables are exposed to the included file below
@@ -160,7 +135,6 @@ private function callConfigurator(callable $callback, ContainerConfigurator $con
160135
{
161136
$callback = $callback(...);
162137
$arguments = [];
163-
$configBuilders = [];
164138
$r = new \ReflectionFunction($callback);
165139

166140
$excluded = true;
@@ -217,14 +191,7 @@ private function callConfigurator(callable $callback, ContainerConfigurator $con
217191
}
218192
// no break
219193
default:
220-
try {
221-
$configBuilder = $this->configBuilder($type);
222-
} catch (InvalidArgumentException|\LogicException $e) {
223-
throw new \InvalidArgumentException(\sprintf('Could not resolve argument "%s" for "%s".', $type.' $'.$parameter->getName(), $path), 0, $e);
224-
}
225-
trigger_deprecation('symfony/dependency-injection', '7.4', 'Using fluent builders for semantic configuration is deprecated, instantiate the "%s" class with the config array as argument and return it instead in "%s".', $type, $path);
226-
$configBuilders[] = $configBuilder;
227-
$arguments[] = $configBuilder;
194+
throw new \InvalidArgumentException(\sprintf('Could not resolve argument "%s" for "%s".', $type.' $'.$parameter->getName(), $path));
228195
}
229196
}
230197

@@ -234,62 +201,5 @@ private function callConfigurator(callable $callback, ContainerConfigurator $con
234201
} finally {
235202
--$this->importing;
236203
}
237-
238-
foreach ($configBuilders as $configBuilder) {
239-
$this->loadExtensionConfig($configBuilder->getExtensionAlias(), ContainerConfigurator::processValue($configBuilder->toArray()));
240-
}
241-
}
242-
243-
/**
244-
* @param string $namespace FQCN string for a class implementing ConfigBuilderInterface
245-
*/
246-
private function configBuilder(string $namespace): ConfigBuilderInterface
247-
{
248-
if (!class_exists(ConfigBuilderGenerator::class)) {
249-
throw new \LogicException('You cannot use the config builder as the Config component is not installed. Try running "composer require symfony/config".');
250-
}
251-
252-
if (null === $this->generator) {
253-
throw new \LogicException('You cannot use the ConfigBuilders without providing a class implementing ConfigBuilderGeneratorInterface.');
254-
}
255-
256-
// If class exists and implements ConfigBuilderInterface
257-
if (class_exists($namespace) && is_subclass_of($namespace, ConfigBuilderInterface::class)) {
258-
return new $namespace();
259-
}
260-
261-
// If it does not start with Symfony\Config\ we don't know how to handle this
262-
if (!str_starts_with($namespace, 'Symfony\\Config\\')) {
263-
throw new InvalidArgumentException(\sprintf('Could not find or generate class "%s".', $namespace));
264-
}
265-
266-
// Try to get the extension alias
267-
$alias = Container::underscore(substr($namespace, 15, -6));
268-
269-
if (str_contains($alias, '\\')) {
270-
throw new InvalidArgumentException('You can only use "root" ConfigBuilders from "Symfony\\Config\\" namespace. Nested classes like "Symfony\\Config\\Framework\\CacheConfig" cannot be used.');
271-
}
272-
273-
if (!$this->container->hasExtension($alias)) {
274-
$extensions = array_filter(array_map(fn (ExtensionInterface $ext) => $ext->getAlias(), $this->container->getExtensions()));
275-
throw new InvalidArgumentException(UndefinedExtensionHandler::getErrorMessage($namespace, null, $alias, $extensions));
276-
}
277-
278-
$extension = $this->container->getExtension($alias);
279-
if (!$extension instanceof ConfigurationExtensionInterface) {
280-
throw new \LogicException(\sprintf('You cannot use the config builder for "%s" because the extension does not implement "%s".', $namespace, ConfigurationExtensionInterface::class));
281-
}
282-
283-
$configuration = $extension->getConfiguration([], $this->container);
284-
$loader = $this->generator->build($configuration);
285-
286-
return $loader();
287204
}
288205
}
289-
290-
/**
291-
* @internal
292-
*/
293-
final class ProtectedPhpFileLoader extends PhpFileLoader
294-
{
295-
}

Tests/Extension/AbstractExtensionTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ protected function createContainerBuilder(): ContainerBuilder
182182
{
183183
return new ContainerBuilder(new ParameterBag([
184184
'kernel.environment' => 'test',
185-
'kernel.build_dir' => 'test',
186185
]));
187186
}
188187
}

Tests/Fixtures/AcmeConfig.php

Lines changed: 0 additions & 48 deletions
This file was deleted.

Tests/Fixtures/AcmeConfig/NestedConfig.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

Tests/Fixtures/config/nested_bundle_config.php

Lines changed: 0 additions & 7 deletions
This file was deleted.

Tests/Loader/PhpFileLoaderTest.php

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
use PHPUnit\Framework\Attributes\DataProvider;
1818
use PHPUnit\Framework\TestCase;
19-
use Symfony\Component\Config\Builder\ConfigBuilderGenerator;
2019
use Symfony\Component\Config\FileLocator;
2120
use Symfony\Component\DependencyInjection\Argument\ServiceLocatorArgument;
2221
use Symfony\Component\DependencyInjection\ContainerBuilder;
@@ -59,7 +58,7 @@ public function testPrependExtensionConfigWithLoadMethod()
5958
$container = new ContainerBuilder();
6059
$container->registerExtension(new \AcmeExtension());
6160
$container->prependExtensionConfig('acme', ['foo' => 'bar']);
62-
$loader = new PhpFileLoader($container, new FileLocator(\dirname(__DIR__).'/Fixtures/config'), 'prod', prepend: true);
61+
$loader = new PhpFileLoader($container, new FileLocator(\dirname(__DIR__).'/Fixtures/config'), 'prod', true);
6362
$loader->load('config_builder.php');
6463

6564
$expected = [
@@ -75,7 +74,7 @@ public function testPrependExtensionConfigWithImportMethod()
7574
$container = new ContainerBuilder();
7675
$container->registerExtension(new \AcmeExtension());
7776
$container->prependExtensionConfig('acme', ['foo' => 'bar']);
78-
$loader = new PhpFileLoader($container, new FileLocator(\dirname(__DIR__).'/Fixtures/config'), 'prod', prepend: true);
77+
$loader = new PhpFileLoader($container, new FileLocator(\dirname(__DIR__).'/Fixtures/config'), 'prod', true);
7978
$loader->import('config_builder.php');
8079

8180
$expected = [
@@ -242,20 +241,6 @@ public function testEnumeration()
242241
$this->assertSame([FooUnitEnum::BAR], $definition->getArguments());
243242
}
244243

245-
#[IgnoreDeprecations]
246-
#[Group('legacy')]
247-
public function testNestedBundleConfigNotAllowed()
248-
{
249-
$fixtures = realpath(__DIR__.'/../Fixtures');
250-
$container = new ContainerBuilder();
251-
$loader = new PhpFileLoader($container, new FileLocator(), 'prod', new ConfigBuilderGenerator(sys_get_temp_dir()));
252-
253-
$this->expectException(\InvalidArgumentException::class);
254-
$this->expectExceptionMessageMatches('/^'.preg_quote('Could not resolve argument "Symfony\\Config\\AcmeConfig\\NestedConfig $config"', '/').'/');
255-
256-
$loader->load($fixtures.'/config/nested_bundle_config.php');
257-
}
258-
259244
public function testWhenEnv()
260245
{
261246
$this->expectNotToPerformAssertions();

0 commit comments

Comments
 (0)