Skip to content

Commit 11dfaa4

Browse files
committed
[DependencyInjection][Routing] Access environment in PHP config
1 parent dd919a7 commit 11dfaa4

File tree

8 files changed

+10
-81
lines changed

8 files changed

+10
-81
lines changed

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
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

Lines changed: 3 additions & 12 deletions
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

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

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

Lines changed: 0 additions & 9 deletions
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 3 additions & 10 deletions
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

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

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

Lines changed: 0 additions & 10 deletions
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)