-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DI] Autowire arguments that are container parameters #21699
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
Comments
Unless I didn't understand well, this has been tried in #20738 and abandoned. |
Hum, that's not exactly the same - then I have an objection: the class is not usable anymore without Symfony's DIC, which is the one supposed to resolve the |
What about using named arguments like that? services:
AppBundle\Controller\:
resource: '../../src/AppBundle/{EventListener,Form/Type,Security,Twig,Utils}'
arguments:
$locales: '%app_locales%'
$sender: '%app.notifications.email_sender%' |
Oops looks like it's not possible... Maybe we should allow it then? |
@GuilhemN see https://github.com/symfony/symfony/projects/2#card-1770476 - which to be made to work should apply these conditionally |
What do you mean by "conditionally"? |
Dunno yet, this is only a project card, needs more thought :) |
Ok I'll give it a try, seems great :) |
While this may seem like a nice solution, there's a few drawbacks:
|
What about a custom annotation? Makes sense? It's something that I can't see against using annotation for this? |
@magarzon I'm not a fond of using (non-standard) annotations for DI: #21103 (comment) But while this PR is nice, I don't like having to add Symfony specific syntaxes ( |
With bold news in Symfony 3.3 like PSR-4 service autodiscovery, I think this makes more sense to current coding context now. Why? Because I cannot use this awesome PSR-4 autodiscovery on most of my services :( Instead, I have to write them manually all one by one. parameters:
twitter_name: 'VotrubaT'
site_ur: 'https://tomasvotruba.com'
source_directory: '%kernel.project_dir%/../../../source'
minimal_gap_in_days: 4 # how many days to wait before publishing another Tweet
twitter_consumer_key: "..."
twitter_consumer_secret: "..."
twitter_oauth_access_token: "..."
twitter_oauth_access_token_secret: "..."
services:
TomasVotruba\Website\TweetPublisher\PostTweetsProvider:
arguments:
- '%site_url%'
- '%source_directory%'
TomasVotruba\Website\TweetPublisher\PostsProvider:
arguments:
- '%source_directory%'
TomasVotruba\Website\TweetPublisher\TwitterApi\TwitterApiWrapper:
arguments:
- '%twitter_name%'
TomasVotruba\Website\TweetPublisher\TweetPublisher:
public: true # for bin/tweet-new-posts container fetch
arguments:
- '%minimal_gap_in_days%'
# Twitter API
TomasVotruba\Website\TweetPublisher\TwitterApi\TwitterApiFactory:
arguments:
- '%twitter_consumer_key%'
- '%twitter_consumer_secret%'
- '%twitter_oauth_access_token%'
- '%twitter_oauth_access_token_secret%' I'd love something like this
services:
_defaults:
autowire_parameters: true
TomasVotruba\Website\TweetPublisher\:
resource: ../ What do you think? |
You can already use bindings https://symfony.com/blog/new-in-symfony-3-4-local-service-binding |
Wow, thanks! π I never thought that could be used this way, even though I've read the post. Also ParamBag as service is great, I have the same bundle for ages π |
I forgot to share a compiler pass that adds this feature |
These days we're updating the Symfony Demo app to use the new service configuration proposed for Symfony 3.3. See https://github.com/symfony/symfony-demo/pull/483/files
One of the things I like the least is this config:
Having to create that boring config just to pass some container parameters destroys the purpose of this new config being RAD, etc.
Proposal
Would it make any sense to "autowire the parameters"?
Before
After
The text was updated successfully, but these errors were encountered: