Skip to content

Commit 1f44487

Browse files
[FrameworkBundle] allow container/routing configurators to vary by env/debug
1 parent 4d91b8f commit 1f44487

File tree

17 files changed

+77
-26
lines changed

17 files changed

+77
-26
lines changed

src/Symfony/Bundle/FrameworkBundle/Kernel/MicroKernelTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public function registerContainerConfiguration(LoaderInterface $loader)
152152
};
153153

154154
try {
155-
$this->configureContainer(new ContainerConfigurator($container, $kernelLoader, $instanceof, $file, $file), $loader);
155+
$this->configureContainer(new ContainerConfigurator($container, $kernelLoader, $instanceof, $file, $file, $this->getEnvironment()), $loader);
156156
} finally {
157157
$instanceof = [];
158158
$kernelLoader->registerAliasesForSinglyImplementedInterfaces();
@@ -193,7 +193,7 @@ public function loadRoutes(LoaderInterface $loader)
193193
return $routes->build();
194194
}
195195

196-
$this->configureRoutes(new RoutingConfigurator($collection, $kernelLoader, $file, $file));
196+
$this->configureRoutes(new RoutingConfigurator($collection, $kernelLoader, $file, $file, $this->getEnvironment()));
197197

198198
foreach ($collection as $route) {
199199
$controller = $route->getDefault('_controller');

src/Symfony/Bundle/FrameworkBundle/Resources/config/routing.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,30 +49,35 @@
4949
->set('routing.loader.xml', XmlFileLoader::class)
5050
->args([
5151
service('file_locator'),
52+
'%kernel.environment%',
5253
])
5354
->tag('routing.loader')
5455

5556
->set('routing.loader.yml', YamlFileLoader::class)
5657
->args([
5758
service('file_locator'),
59+
'%kernel.environment%',
5860
])
5961
->tag('routing.loader')
6062

6163
->set('routing.loader.php', PhpFileLoader::class)
6264
->args([
6365
service('file_locator'),
66+
'%kernel.environment%',
6467
])
6568
->tag('routing.loader')
6669

6770
->set('routing.loader.glob', GlobFileLoader::class)
6871
->args([
6972
service('file_locator'),
73+
'%kernel.environment%',
7074
])
7175
->tag('routing.loader')
7276

7377
->set('routing.loader.directory', DirectoryLoader::class)
7478
->args([
7579
service('file_locator'),
80+
'%kernel.environment%',
7681
])
7782
->tag('routing.loader')
7883

src/Symfony/Component/Config/Loader/FileLoader.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ abstract class FileLoader extends Loader
3131

3232
private $currentDir;
3333

34-
public function __construct(FileLocatorInterface $locator)
34+
public function __construct(FileLocatorInterface $locator, string $env = null)
3535
{
3636
$this->locator = $locator;
37+
$this->env = $env;
3738
}
3839

3940
/**

src/Symfony/Component/Config/Loader/Loader.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
abstract class Loader implements LoaderInterface
2222
{
2323
protected $resolver;
24+
protected $env;
25+
26+
public function __construct(string $env = null)
27+
{
28+
$this->env = $env;
29+
}
2430

2531
/**
2632
* {@inheritdoc}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,18 @@ class ClosureLoader extends Loader
2525
{
2626
private $container;
2727

28-
public function __construct(ContainerBuilder $container)
28+
public function __construct(ContainerBuilder $container, string $env = null)
2929
{
3030
$this->container = $container;
31+
$this->env = $env;
3132
}
3233

3334
/**
3435
* {@inheritdoc}
3536
*/
3637
public function load($resource, string $type = null)
3738
{
38-
$resource($this->container);
39+
$resource($this->container, $this->env);
3940
}
4041

4142
/**

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,16 @@ class ContainerConfigurator extends AbstractConfigurator
3535
private $path;
3636
private $file;
3737
private $anonymousCount = 0;
38+
private $env;
3839

39-
public function __construct(ContainerBuilder $container, PhpFileLoader $loader, array &$instanceof, string $path, string $file)
40+
public function __construct(ContainerBuilder $container, PhpFileLoader $loader, array &$instanceof, string $path, string $file, string $env = null)
4041
{
4142
$this->container = $container;
4243
$this->loader = $loader;
4344
$this->instanceof = &$instanceof;
4445
$this->path = $path;
4546
$this->file = $file;
47+
$this->env = $env;
4648
}
4749

4850
final public function extension(string $namespace, array $config)
@@ -71,6 +73,23 @@ final public function services(): ServicesConfigurator
7173
return new ServicesConfigurator($this->container, $this->loader, $this->instanceof, $this->path, $this->anonymousCount);
7274
}
7375

76+
/**
77+
* @return static
78+
*/
79+
final public function whenEnv(string $env): self
80+
{
81+
if ($env === $this->env) {
82+
return clone $this;
83+
}
84+
85+
$instanceof = $this->instanceof;
86+
$clone = clone $this;
87+
$clone->container = new ContainerBuilder(clone $this->container->getParameterBag());
88+
$clone->instanceof = &$instanceof;
89+
90+
return $clone;
91+
}
92+
7493
/**
7594
* @return static
7695
*/

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ abstract class FileLoader extends BaseFileLoader
3939
protected $singlyImplemented = [];
4040
protected $autoRegisterAliasesForSinglyImplementedInterfaces = true;
4141

42-
public function __construct(ContainerBuilder $container, FileLocatorInterface $locator)
42+
public function __construct(ContainerBuilder $container, FileLocatorInterface $locator, string $env = null)
4343
{
4444
$this->container = $container;
4545

46-
parent::__construct($locator);
46+
parent::__construct($locator, $env);
4747
}
4848

4949
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function load($resource, string $type = null)
4747
$callback = $load($path);
4848

4949
if (\is_object($callback) && \is_callable($callback)) {
50-
$callback(new ContainerConfigurator($this->container, $this, $this->instanceof, $path, $resource), $this->container, $this);
50+
$callback(new ContainerConfigurator($this->container, $this, $this->instanceof, $path, $resource, $this->env), $this->container, $this);
5151
}
5252
} finally {
5353
$this->instanceof = [];

src/Symfony/Component/DependencyInjection/composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
},
2525
"require-dev": {
2626
"symfony/yaml": "^4.4|^5.0",
27-
"symfony/config": "^5.1",
27+
"symfony/config": "^5.3",
2828
"symfony/expression-language": "^4.4|^5.0"
2929
},
3030
"suggest": {
@@ -35,7 +35,7 @@
3535
"symfony/proxy-manager-bridge": "Generate service proxies to lazy load them"
3636
},
3737
"conflict": {
38-
"symfony/config": "<5.1",
38+
"symfony/config": "<5.3",
3939
"symfony/finder": "<4.4",
4040
"symfony/proxy-manager-bridge": "<4.4",
4141
"symfony/yaml": "<4.4"

src/Symfony/Component/HttpKernel/Kernel.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -751,14 +751,15 @@ protected function dumpContainer(ConfigCache $cache, ContainerBuilder $container
751751
*/
752752
protected function getContainerLoader(ContainerInterface $container)
753753
{
754+
$env = $this->getEnvironment();
754755
$locator = new FileLocator($this);
755756
$resolver = new LoaderResolver([
756-
new XmlFileLoader($container, $locator),
757-
new YamlFileLoader($container, $locator),
758-
new IniFileLoader($container, $locator),
759-
new PhpFileLoader($container, $locator),
760-
new GlobFileLoader($container, $locator),
761-
new DirectoryLoader($container, $locator),
757+
new XmlFileLoader($container, $locator, $env),
758+
new YamlFileLoader($container, $locator, $env),
759+
new IniFileLoader($container, $locator, $env),
760+
new PhpFileLoader($container, $locator, $env),
761+
new GlobFileLoader($container, $locator, $env),
762+
new DirectoryLoader($container, $locator, $env),
762763
new ClosureLoader($container),
763764
]);
764765

0 commit comments

Comments
 (0)