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

[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

Merged
merged 1 commit into from
May 12, 2021

Conversation

nicolas-grekas
Copy link
Member

@nicolas-grekas nicolas-grekas commented May 12, 2021

Q A
Branch? 5.x
Bug fix? no
New feature? yes
Deprecations? no
Tickets -
License MIT
Doc PR -

This PR makes config builders compatible with conditional configuration based on the $env.

See fixture for an example:

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

if ('prod' !== $env) {
    return;
}

return static function (AcmeConfig $config) {
    $config->color('blue');
};

On PHP8, the PR allows using #[When(env: 'prod')]:

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

return #[When(env: 'prod')] function (AcmeConfig $config) {
    $config->color('blue');
};

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

@Nyholm
Copy link
Member

Nyholm commented May 12, 2021

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');
    }
};

Copy link
Member

@Nyholm Nyholm left a 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);
Copy link
Member

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?

Copy link
Member Author

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;

Copy link
Member

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.

Copy link
Member Author

@nicolas-grekas nicolas-grekas May 12, 2021

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.

Copy link
Member

@Nyholm Nyholm left a 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.

@nicolas-grekas nicolas-grekas changed the title [DependencyInjection] inject $env in the scope of PHP-DSL files [DependencyInjection] allow PHP-DSL files to be env-conditional May 12, 2021
@nicolas-grekas nicolas-grekas merged commit b14d769 into symfony:5.x May 12, 2021
@nicolas-grekas nicolas-grekas deleted the di-env branch May 12, 2021 06:49
@fabpot fabpot mentioned this pull request May 12, 2021
nicolas-grekas added a commit that referenced this pull request Oct 19, 2022
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.