Skip to content

[DependencyInjection] Add env() and EnvConfigurator in the PHP-DSL #40682

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
Apr 11, 2021

Conversation

fancyweb
Copy link
Contributor

@fancyweb fancyweb commented Apr 2, 2021

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

Recently, I have been using env var processors a lot. This is a proposition to improve the DX a bit when you use the PHP-DSL to configure services.

Firstly, I am "annoyed" by the fact that I can do param('my_param') but not env('MY_ENV').

Secondly, long chains of env var processors (eg: %env(default:my_param:key:path:url:MY_ENV_VAR) have two issues:

  • you must construct and read them in "reverse"
  • some env var processor are actually composed of 2 parts (key:path), you don't distinguish them easily from the rest

Before:

->arg('$myArg', '%env(default:my_param:key:path:url:MY_ENV_VAR)%')

After:

->arg(
    '$myArg',
    env('MY_ENV_VAR')
        ->url()
        ->key('path')
        ->default('my_param')
)

Custom env var processor would be callable with ->custom('my_custom_env_var_processor') or you could extend the configurator and add your own methods.

WDYT?

@Nyholm
Copy link
Member

Nyholm commented Apr 2, 2021

Thank you. I like the idea.

Having env() is also away to keep people from doing getenv().

@stof
Copy link
Member

stof commented Apr 2, 2021

this misses a way to add custom processors in the env configuration. Symfony allows extending the system. I missed the custom() method

@fancyweb
Copy link
Contributor Author

fancyweb commented Apr 2, 2021

Concerning custom env var processor and extension, WDYT of making $stack protected, to allow userland to extend the base EnvConfigurator, to allow custom methods. Obviously you would have to instantiate it manually instead of using env(). Though I really wonder if people would actually use it 🤔.

@fancyweb fancyweb force-pushed the di/env-configurator branch from 7eecb14 to 765bf13 Compare April 2, 2021 16:39
@stof
Copy link
Member

stof commented Apr 2, 2021

@fancyweb no need to make it protected (which would then forbid refactoring it). Your own method could call $this->custom('my_processor') already

@fancyweb fancyweb force-pushed the di/env-configurator branch from 765bf13 to f2bf776 Compare April 2, 2021 16:43
@fancyweb fancyweb force-pushed the di/env-configurator branch from f2bf776 to 5f0fe32 Compare April 6, 2021 11:54
@nicolas-grekas
Copy link
Member

Thank you @fancyweb.

chalasr
chalasr approved these changes Apr 11, 2021
@fancyweb fancyweb deleted the di/env-configurator branch April 12, 2021 07:27
nicolas-grekas added a commit that referenced this pull request Apr 13, 2021
…rectly on EnvConfigurator (jvasseur)

This PR was merged into the 5.3-dev branch.

Discussion
----------

[DependencyInjection] Allow calling custom processors directly on EnvConfigurator

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

This is a proposition of addition to the feature added by #40682 to allow calling custom processors in the same way we call builtin ones. This is not perfect since it doesn't allow auto-completion for these custom methods but I think this provides a cleaner API for custom processors.

Commits
-------

1d008f7 Allow calling custom processors directly on EnvConfigurator
@fabpot fabpot mentioned this pull request Apr 18, 2021
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.

8 participants