8000 Dont modify ContainerConfigurator.php · symfony/symfony@6381b37 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6381b37

Browse files
committed
Dont modify ContainerConfigurator.php
1 parent 5ba0573 commit 6381b37

File tree

2 files changed

+51
-50
lines changed

2 files changed

+51
-50
lines changed

src/Symfony/Component/DependencyInjection/Loader/Configurator/ContainerConfigurator.php

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -41,70 +41,25 @@ class ContainerConfigurator extends AbstractConfigurator
4141
private $file;
4242
private $anonymousCount = 0;
4343
private $env;
44-
private $buildDir;
45-
private $generator;
4644

47-
public function __construct(ContainerBuilder $container, PhpFileLoader $loader, array &$instanceof, string $path, string $file, string $env = null, ?string $buildDir = null, ConfigBuilderGeneratorInterface $generator = null)
45+
public function __construct(ContainerBuilder $container, PhpFileLoader $loader, array &$instanceof, string $path, string $file, string $env = null)
4846
{
4947
$this->container = $container;
5048
$this->loader = $loader;
5149
$this->instanceof = &$instanceof;
5250
$this->path = $path;
5351
$this->file = $file;
5452
$this->env = $env;
55-
$this->buildDir = $buildDir;
56-
$this->generator = $generator;
5753
}
5854

5955
final public function extension(string $namespace, array $config)
60-
{
61-
$this->throwIfExtensionDoesNotExist($namespace);
62-
63-
$this->container->loadFromExtension($namespace, static::processValue($config));
64-
}
65-
66-
/**
67-
* @param string $namespace example extension alias ("framework") or FQCN string
68-
* for a class implementing ConfigBuilderInterface
69-
*/
70-
final public function configBuilder(string $namespace): ConfigBuilderInterface
71-
{
72-
if (!class_exists(ConfigBuilderGenerator::class)) {
73-
throw new \LogicException('You cannot use the config builder as the Config component is not installed. Try running "composer require symfony/config".');
74-
}
75-
76-
if (null === $this->buildDir) {
77-
throw new \LogicException('You cannot use the config builder without providing a path to the directory where to store the generated ConfigBuilders.');
78-
}
79-
80-
if (class_exists($namespace) && is_subclass_of($namespace, ConfigBuilderInterface::class)) {
81-
// If class exists and implements ConfigBuilderInterface
82-
return new $namespace();
83-
} elseif ('Symfony\\Config\\' === substr($namespace, 0, 15)) {
84-
// Else, see if it starts with Symfony\Config\ and try to get the extension alias.
85-
$namespace = Container::underscore(substr($namespace, 15, -6));
86-
}
87-
88-
$this->throwIfExtensionDoesNotExist($namespace);
89-
90-
$extension = $this->container->getExtension($namespace);
91-
if (!$extension instanceof ConfigurationExtensionInterface) {
92-
throw new \LogicException(sprintf('You cannot use the config builder for "%s" because the extension does not implement "%s".', $namespace, ConfigurationExtensionInterface::class));
93-
}
94-
95-
$configuration = $extension->getConfiguration([], $this->container);
96-
$this->generator = $this->generator ?? new ConfigBuilderGenerator();
97-
$loader = $this->generator->build($configuration, $this->buildDir);
98-
99-
return $loader();
100-
}
101-
102-
private function throwIfExtensionDoesNotExist(string $namespace): void
10356
{
10457
if (!$this->container->hasExtension($namespace)) {
10558
$extensions = array_filter(array_map(function (ExtensionInterface $ext) { return $ext->getAlias(); }, $this->container->getExtensions()));
10659
throw new InvalidArgumentException(sprintf('There is no extension able to load the configuration for "%s" (in "%s"). Looked for namespace "%s", found "%s".', $namespace, $this->file, $namespace, $extensions ? implode('", "', $extensions) : 'none'));
10760
}
61+
62+
$this->container->loadFromExtension($namespace, static::processValue($config));
10863
}
10964

11065
final public function import(string $resource, string $type = null, $ignoreErrors = false)

src/Symfony/Component/DependencyInjection/Loader/PhpFileLoader.php

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@
1111

1212
namespace Symfony\Component\DependencyInjection\Loader;
1313

14+
use Symfony\Component\Config\Builder\ConfigBuilderGenerator;
15+
use Symfony\Component\Config\Builder\ConfigBuilderGeneratorInterface;
1416
use Symfony\Component\Config\Builder\ConfigBuilderInterface;
1517
use Symfony\Component\Config\FileLocatorInterface;
1618
use Symfony\Component\DependencyInjection\Container;
1719
use Symfony\Component\DependencyInjection\ContainerBuilder;
1820
use Symfony\Component\DependencyInjection\ContainerInterface;
1921
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
22+
use Symfony\Component\DependencyInjection\Extension\ConfigurationExtensionInterface;
23+
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
2024
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
2125

2226
/**
@@ -31,11 +35,13 @@ class PhpFileLoader extends FileLoader
3135
{
3236
protected $autoRegisterAliasesForSinglyImplementedInterfaces = false;
3337
private $buildDir;
38+
private $generator;
3439

35-
public function __construct(ContainerBuilder $container, FileLocatorInterface $locator, string $env = null, string $buildDir = null)
40+
public function __construct(ContainerBuilder $container, FileLocatorInterface $locator, string $env = null, string $buildDir = null, ConfigBuilderGeneratorInterface $generator = null)
3641
{
3742
parent::__construct($container, $locator, $env);
3843
$this->buildDir = $buildDir;
44+
$this->$generator = $generator;
3945
}
4046

4147
/**
@@ -116,7 +122,7 @@ private function executeCallback(callable $callback, ContainerConfigurator $cont
116122
break;
117123
default:
118124
try {
119-
$configBuilder = $containerConfigurator->configBuilder($type);
125+
$configBuilder = $this->configBuilder($type);
120126
} catch (InvalidArgumentException $e) {
121127
throw new \InvalidArgumentException(sprintf('Could not resolve argument "%s" for "%s".', $type.' '.$parameter->getName(), $path), 0, $e);
122128
}
@@ -132,6 +138,46 @@ private function executeCallback(callable $callback, ContainerConfigurator $cont
132138
$containerConfigurator->extension($configBuilder->getExtensionAlias(), $configBuilder->toArray());
133139
}
134140
}
141+
142+
143+
/**
144+
* @param string $namespace example extension alias ("framework") or FQCN string
145+
* for a class implementing ConfigBuilderInterface
146+
*/
147+
private function configBuilder(string $namespace): ConfigBuilderInterface
148+
{
149+
if (!class_exists(ConfigBuilderGenerator::class)) {
150+
throw new \LogicException('You cannot use the config builder as the Config component is not installed. Try running "composer require symfony/config".');
151+
}
152+
153+
if (null === $this->buildDir) {
154+
throw new \LogicException('You cannot use the config builder without providing a path to the directory where to store the generated ConfigBuilders.');
155+
}
156+
157+
if (class_exists($namespace) && is_subclass_of($namespace, ConfigBuilderInterface::class)) {
158+
// If class exists and implements ConfigBuilderInterface
159+
return new $namespace();
160+
} elseif ('Symfony\\Config\\' === substr($namespace, 0, 15)) {
161+
// Else, see if it starts with Symfony\Config\ and try to get the extension alias.
162+
$namespace = Container::underscore(substr($namespace, 15, -6));
163+
}
164+
165+
if (!$this->container->hasExtension($namespace)) {
166+
$extensions = array_filter(array_map(function (ExtensionInterface $ext) { return $ext->getAlias(); }, $this->container->getExtensions()));
167+
throw new InvalidArgumentException(sprintf('There is no extension able to load the configuration for "%s" (in "%s"). Looked for namespace "%s", found "%s".', $namespace, $this->file, $namespace, $extensions ? implode('", "', $extensions) : 'none'));
168+
}
169+
170+
$extension = $this->container->getExtension($namespace);
171+
if (!$extension instanceof ConfigurationExtensionInterface) {
172+
throw new \LogicException(sprintf('You cannot use the config builder for "%s" because the extension does not implement "%s".', $namespace, ConfigurationExtensionInterface::class));
173+
}
174+
175+
$configuration = $extension->getConfiguration([], $this->container);
176+
$this->generator = $this->generator ?? new ConfigBuilderGenerator();
177+
$loader = $this->generator->build($configuration, $this->buildDir);
178+
179+
return $loader();
180+
}
135181
}
136182

137183
/**

0 commit comments

Comments
 (0)
0