Skip to content

Commit 48ae511

Browse files
minor #40808 [DependencyInjection][Routing] Access environment in PHP config (Nyholm)
This PR was merged into the 5.3-dev branch. Discussion ---------- [DependencyInjection][Routing] Access environment in PHP config | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | no | New feature? | yes | Deprecations? | | Tickets | | License | MIT | Doc PR | This will allow me to write config like: ```php use Symfony\Config\FrameworkConfig; use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; return static function (FrameworkConfig $framework, ContainerConfigurator $container) { if ('test' === $container->env()) { $framework->test(true); $framework->session()->storageFactoryId('session.storage.mock_file'); } }; ``` This PR will also revert parts of #40214. It is much simpler to maintain and write PHP config without `->when()`. Instead we add `ContainerConfigurator::env(): ?string` and `RoutingConfigurator::env(): ?string`. ```php use App\Controller\BlogController; use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator; return function (RoutingConfigurator $routes) { if ($routes->env() === 'dev') { $routes->add('debug-stats', '/blog-debyg') ->controller([BlogController::class, 'debug']) ; } $routes->add('blog_list', '/blog') ->controller([BlogController::class, 'list']) ; }; ``` Commits ------- 11dfaa4 [DependencyInjection][Routing] Access environment in PHP config
2 parents 1a4bd73 + 11dfaa4 commit 48ae511

File tree

8 files changed

+10
-81
lines changed

8 files changed

+10
-81
lines changed

src/Symfony/Component/DependencyInjection/CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ CHANGELOG
1010
* Add `#[AsTaggedItem]` attribute for defining the index and priority of classes found in tagged iterators/locators
1111
* Add autoconfigurable attributes
1212
* Add support for autowiring tagged iterators and locators via attributes on PHP 8
13-
* Add support for per-env configuration in loaders
13+
* Add support for per-env configuration in XML and Yaml loaders
1414
* Add `ContainerBuilder::willBeAvailable()` to help with conditional configuration
1515
* Add support an integer return value for default_index_method
1616
* Add `env()` and `EnvConfigurator` in the PHP-DSL
1717
* Add support for `ConfigBuilder` in the `PhpFileLoader`
18+
* Add `ContainerConfigurator::env()` to get the current environment
1819

1920
5.2.0
2021
-----

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

+3-12
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,11 @@ final public function services(): ServicesConfigurator
7474
}
7575

7676
/**
77-
* @return static
77+
* Get the current environment to be able to write conditional configuration.
7878
*/
79-
final public function when(string $env): self
79+
final public function env(): ?string
8080
{
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;
81+
return $this->env;
9182
}
9283

9384
/**

src/Symfony/Component/DependencyInjection/Tests/Fixtures/config/when-env.php

-20
This file was deleted.

src/Symfony/Component/DependencyInjection/Tests/Loader/PhpFileLoaderTest.php

-9
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,6 @@ public function testStack()
144144
$this->assertEquals($expected, $container->get('stack_d'));
145145
}
146146

147-
public function testWhenEnv()
148-
{
149-
$container = new ContainerBuilder();
150-
$loader = new PhpFileLoader($container, new FileLocator(realpath(__DIR__.'/../Fixtures').'/config'), 'some-env');
151-
$loader->load('when-env.php');
152-
153-
$this->assertSame(['foo' => 234, 'bar' => 345], $container->getParameterBag()->all());
154-
}
155-
156147
public function testEnvConfigurator()
157148
{
158149
$container = new ContainerBuilder();

src/Symfony/Component/Routing/CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ CHANGELOG
55
---
66

77
* Already encoded slashes are not decoded nor double-encoded anymore when generating URLs
8-
* Add support for per-env configuration in loaders
8+
* Add support for per-env configuration in XML and Yaml loaders
99
* Deprecate creating instances of the `Route` annotation class by passing an array of parameters
10+
* Add `RoutingConfigurator::env()` to get the current environment
1011

1112
5.2.0
1213
-----

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

+3-10
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,11 @@ final public function collection(string $name = ''): CollectionConfigurator
6161
}
6262

6363
/**
64-
* @return static
64+
* Get the current environment to be able to write conditional configuration.
6565
*/
66-
final public function when(string $env): self
66+
final public function env(): ?string
6767
{
68-
if ($env === $this->env) {
69-
return clone $this;
70-
}
71-
72-
$clone = clone $this;
73-
$clone->collection = new RouteCollection();
74-
75-
return $clone;
68+
return $this->env;
7669
}
7770

7871
/**

src/Symfony/Component/Routing/Tests/Fixtures/when-env.php

-18
This file was deleted.

src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php

-10
Original file line numberDiff line numberDiff line change
@@ -284,14 +284,4 @@ public function testImportingRoutesWithSingleHostInImporter()
284284

285285
$this->assertEquals($expectedRoutes('php'), $routes);
286286
}
287-
288-
public function testWhenEnv()
289-
{
290-
$loader = new PhpFileLoader(new FileLocator([__DIR__.'/../Fixtures']), 'some-env');
291-
$routes = $loader->load('when-env.php');
292-
293-
$this->assertSame(['b', 'a'], array_keys($routes->all()));
294-
$this->assertSame('/b', $routes->get('b')->getPath());
295-
$this->assertSame('/a1', $routes->get('a')->getPath());
296-
}
297287
}

0 commit comments

Comments
 (0)