Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f0b2029

Browse filesBrowse files
feature #47906 [DependencyInjection] Allow injecting the current env into php config closures (HypeMC)
This PR was merged into the 6.2 branch. Discussion ---------- [DependencyInjection] Allow injecting the current env into php config closures | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | no | New feature? | yes | Deprecations? | yes | Tickets | - | License | MIT | Doc PR | - The original idea of this PR was to allow injecting `string $env` into php config closures. Even though this can be done by injecting `ContainerConfigurator` & calling `env()` it seems kind of redundant when you don't need any features except the current env, eg when using the config builder classes: ```diff - return function (AcmeConfig $config, ContainerConfigurator $c) { + return function (AcmeConfig $config, string $env) { - if ('dev' === $c->env()) { + if ('dev' === $env) { // ... } }; ``` Injecting the `$env` variable looks a bit cleaner IMO. However, while working on this PR I discovered #41182 . Even though there's already an `$env` variable presets in the scope of php files which can be used for the same thing, it's not really IDE friendly: ![image](https://user-images.githubusercontent.com/2445045/196558741-8931a3d9-f74f-423e-9494-2d931cbac995.png) Since the original PR mentioned the that the `$env` variable was added for PHP <8 & Symfony 6.2 is >=8.1, it doesn't seem to be needed any more, so it can be deprecated. Commits ------- 4141975 [DependencyInjection] Allow injecting the current env into php config closures
2 parents 9c628d0 + 4141975 commit f0b2029
Copy full SHA for f0b2029

File tree

5 files changed

+28
-1
lines changed
Filter options

5 files changed

+28
-1
lines changed

‎src/Symfony/Component/DependencyInjection/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ CHANGELOG
1313
* Deprecate calling `ContainerAwareTrait::setContainer()` without arguments
1414
* Deprecate using numeric parameter names
1515
* Add support for tagged iterators/locators `exclude` option to the xml and yaml loaders/dumpers
16+
* Allow injecting `string $env` into php config closures
1617

1718
6.1
1819
---

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Loader/PhpFileLoader.php
+7-1Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ private function executeCallback(callable $callback, ContainerConfigurator $cont
123123
case self::class:
124124
$arguments[] = $this;
125125
break;
126+
case 'string':
127+
if (null !== $this->env && 'env' === $parameter->getName()) {
128+
$arguments[] = $this->env;
129+
break;
130+
}
131+
// no break
126132
default:
127133
try {
128134
$configBuilder = $this->configBuilder($type);
@@ -163,7 +169,7 @@ private function configBuilder(string $namespace): ConfigBuilderInterface
163169
return new $namespace();
164170
}
165171

166-
// If it does not start with Symfony\Config\ we dont know how to handle this
172+
// If it does not start with Symfony\Config\ we don't know how to handle this
167173
if (!str_starts_with($namespace, 'Symfony\\Config\\')) {
168174
throw new InvalidArgumentException(sprintf('Could not find or generate class "%s".', $namespace));
169175
}
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
parameters:
2+
acme.configs: [{ color: blue }]
3+
4+
services:
5+
service_container:
6+
class: Symfony\Component\DependencyInjection\ContainerInterface
7+
public: true
8+
synthetic: true
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
use Symfony\Component\DependencyInjection\Tests\Fixtures\AcmeConfig;
4+
5+
return function (AcmeConfig $config, string $env) {
6+
if ('prod' === $env) {
7+
$config->color('blue');
8+
} else {
9+
$config->color('red');
10+
}
11+
};

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Tests/Loader/PhpFileLoaderTest.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ public function provideConfig()
9999
yield ['config_builder'];
100100
yield ['expression_factory'];
101101
yield ['closure'];
102+
yield ['env_param'];
102103
}
103104

104105
public function testAutoConfigureAndChildDefinition()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.