Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d7c1696

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

File tree

12 files changed

+65
-21
lines changed

12 files changed

+65
-21
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: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ abstract class FileLoader extends Loader
2828
protected static $loading = [];
2929

3030
protected $locator;
31+
protected $env;
3132

3233
private $currentDir;
3334

34-
public function __construct(FileLocatorInterface $locator)
35+
public function __construct(FileLocatorInterface $locator, string $env = null)
3536
{
3637
$this->locator = $locator;
38+
$this->env = $env;
3739
}
3840

3941
/**

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

src/Symfony/Component/Routing/Loader/AnnotationFileLoader.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ class AnnotationFileLoader extends FileLoader
2929
/**
3030
* @throws \RuntimeException
3131
*/
32-
public function __construct(FileLocatorInterface $locator, AnnotationClassLoader $loader)
32+
public function __construct(FileLocatorInterface $locator, AnnotationClassLoader $loader, string $env = null)
3333
{
3434
if (!\function_exists('token_get_all')) {
3535
throw new \LogicException('The Tokenizer extension is required for the routing annotation loaders.');
3636
}
3737

38-
parent::__construct($locator);
38+
parent::__construct($locator, $env);
3939

4040
$this->loader = $loader;
4141
}

src/Symfony/Component/Routing/Loader/Configurator/RoutingConfigurator.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,15 @@ class RoutingConfigurator
2424
private $loader;
2525
private $path;
2626
private $file;
27+
private $env;
2728

28-
public function __construct(RouteCollection $collection, PhpFileLoader $loader, string $path, string $file)
29+
public function __construct(RouteCollection $collection, PhpFileLoader $loader, string $path, string $file, string $env = null)
2930
{
3031
$this->collection = $collection;
3132
$this->loader = $loader;
3233
$this->path = $path;
3334
$this->file = $file;
35+
$this->env = $env;
3436
}
3537

3638
/**
@@ -58,6 +60,21 @@ final public function collection(string $name = ''): CollectionConfigurator
5860
return new CollectionConfigurator($this->collection, $name);
5961
}
6062

63+
/**
64+
* @return static
65+
*/
66+
final public function whenEnv(string $env): self
67+
{
68+
if ($env === $this->env) {
69+
return clone $this;
70+
}
71+
72+
$clone = clone $this;
73+
$clone->collection = new RouteCollection();
74+
75+
return $clone;
76+
}
77+
6178
/**
6279
* @return static
6380
*/

0 commit comments

Comments
 (0)