Skip to content

[DependencyInjection] Allow injecting the current env into php config closures #47906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ CHANGELOG
* Deprecate calling `ContainerAwareTrait::setContainer()` without arguments
* Deprecate using numeric parameter names
* Add support for tagged iterators/locators `exclude` option to the xml and yaml loaders/dumpers
* Allow injecting `string $env` into php config closures

6.1
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ private function executeCallback(callable $callback, ContainerConfigurator $cont
case self::class:
$arguments[] = $this;
break;
case 'string':
if (null !== $this->env && 'env' === $parameter->getName()) {
$arguments[] = $this->env;
break;
}
// no break
default:
try {
$configBuilder = $this->configBuilder($type);
Expand Down Expand Up @@ -163,7 +169,7 @@ private function configBuilder(string $namespace): ConfigBuilderInterface
return new $namespace();
}

// If it does not start with Symfony\Config\ we dont know how to handle this
// If it does not start with Symfony\Config\ we don't know how to handle this
if (!str_starts_with($namespace, 'Symfony\\Config\\')) {
throw new InvalidArgumentException(sprintf('Could not find or generate class "%s".', $namespace));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
parameters:
acme.configs: [{ color: blue }]

services:
service_container:
class: Symfony\Component\DependencyInjection\ContainerInterface
public: true
synthetic: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use Symfony\Component\DependencyInjection\Tests\Fixtures\AcmeConfig;

return function (AcmeConfig $config, string $env) {
if ('prod' === $env) {
$config->color('blue');
} else {
$config->color('red');
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public function provideConfig()
yield ['config_builder'];
yield ['expression_factory'];
yield ['closure'];
yield ['env_param'];
}

public function testAutoConfigureAndChildDefinition()
Expand Down