-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DependencyInjection] allow PHP-DSL files to be env-conditional #41182
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
Conversation
Just making an observation. This also means we can write: <?php
// config/packages/acme.php
use Symfony\Config\AcmeConfig;
return static function (AcmeConfig $config) use ($env) {
if ('prod' !== $env) {
$config->color('red');
} else {
$config->color('blue');
}
}; Which is the same as: <?php
// config/packages/acme.php
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Config\AcmeConfig;
return static function (AcmeConfig $config, ContainerConfigurator $container) {
if ('prod' !== $container->env()) {
$config->color('red');
} else {
$config->color('blue');
}
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I updated the PR to allow using #[When(env: prod')] on PHP 8:
Wow, that was simpler than I expected. =)
return include $path; | ||
}, $this, ProtectedPhpFileLoader::class); | ||
|
||
try { | ||
$callback = $load($path); | ||
$callback = $load($path, $this->env); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to inject then $env? Or should we just do the PHP8 attributes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need it for ppl on PHP < 7 at least yes;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since Symfony 4.0 we have the solution to use config/packages/dev/acme.php
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, but that's unrelated feature. Also, we already inject a few variables in the scope. Adding $env next to them just makes sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's move forward. Im happy to discuss removing $env
in 6.0 later.
…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:  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
This PR makes config builders compatible with conditional configuration based on the $env.
See fixture for an example:
On PHP8, the PR allows using
#[When(env: 'prod')]
:Without this patch, such a config file cannot be used if AcmeBundle is not loaded in the current $env.
This is a follow up of https://symfony.com/blog/new-in-symfony-5-3-configure-multiple-environments-in-a-single-file#comment-24521 by @a-menshchikov