Skip to content

Commit 5b97a21

Browse files
committed
[DependencyInjection] Allow injecting the current env into php config closures
1 parent 261cc00 commit 5b97a21

File tree

5 files changed

+28
-1
lines changed

5 files changed

+28
-1
lines changed

CHANGELOG.md

Lines 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
---

Loader/PhpFileLoader.php

Lines 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
}
Lines 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

Tests/Fixtures/config/env_param.php

Lines 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+
};

Tests/Loader/PhpFileLoaderTest.php

Lines 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)