Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
nicolas-grekas merged 1 commit intosymfony:5.xfromnicolas-grekas:di-env
May 12, 2021

Conversation

@nicolas-grekas
Copy link
Member

@nicolas-grekasnicolas-grekas commentedMay 12, 2021
edited
Loading

QA
Branch?5.x
Bug fix?no
New feature?yes
Deprecations?no
Tickets-
LicenseMIT
Doc PR-

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

See fixture for an example:

useSymfony\Component\DependencyInjection\Tests\Fixtures\AcmeConfig;if ('prod' !==$env) {return;}returnstaticfunction (AcmeConfig$config) {$config->color('blue');};

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

useSymfony\Component\DependencyInjection\Attribute\When;useSymfony\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 ofhttps://symfony.com/blog/new-in-symfony-5-3-configure-multiple-environments-in-a-single-file#comment-24521 by@a-menshchikov

@Nyholm
Copy link
Member

Just making an observation. This also means we can write:

<?php// config/packages/acme.phpuseSymfony\Config\AcmeConfig;returnstaticfunction (AcmeConfig$config)use ($env) {if ('prod' !==$env) {$config->color('red');    }else {$config->color('blue');    }};

Which is the same as:

<?php// config/packages/acme.phpuseSymfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;useSymfony\Config\AcmeConfig;returnstaticfunction (AcmeConfig$config,ContainerConfigurator$container) {if ('prod' !==$container->env()) {$config->color('red');    }else {$config->color('blue');    }};
nicolas-grekas reacted with thumbs up emoji

Copy link
Member

@NyholmNyholm 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. =)


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
MemberAuthor

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 useconfig/packages/dev/acme.php.

Copy link
MemberAuthor

@nicolas-grekasnicolas-grekasMay 12, 2021
edited
Loading

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.

Nyholm reacted with thumbs up emoji
Copy link
Member

@NyholmNyholm 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-grekasnicolas-grekas changed the title[DependencyInjection] inject $env in the scope of PHP-DSL files[DependencyInjection] allow PHP-DSL files to be env-conditionalMay 12, 2021
@nicolas-grekasnicolas-grekas merged commitb14d769 intosymfony:5.xMay 12, 2021
@nicolas-grekasnicolas-grekas deleted the di-env branchMay 12, 2021 06:49
@fabpotfabpot mentioned this pull requestMay 12, 2021
nicolas-grekas added a commit that referenced this pull requestOct 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 freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@NyholmNyholmNyholm approved these changes

Assignees

No one assigned

Projects

None yet

Milestone

5.3

Development

Successfully merging this pull request may close these issues.

3 participants

@nicolas-grekas@Nyholm@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp