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

[DI] Allow autowiring by type + parameter name#28234

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:masterfromnicolas-grekas:di-arg-alias
Aug 24, 2018

Conversation

@nicolas-grekas
Copy link
Member

@nicolas-grekasnicolas-grekas commentedAug 20, 2018
edited
Loading

QA
Branch?master
Bug fix?no
New feature?yes
BC breaks?no
Deprecations?no
Tests pass?yes
Fixed tickets-
LicenseMIT
Doc PRsymfony/symfony-docs#10206

In#27165, we introduced the possibility to bind by type+name:

bind:Psr\Log\LoggerInterface $myLogger:@monolog.logger.my_logger

But we forgot about aliases. For consistency, they could and should allow doing the same. More importantly, this will open up interesting use cases where bundles could provide default values for typed+named arguments (using the newContainerBuilder::registerAliasForArgument() method). E.g:

services:Psr\Cache\CacheItemPoolInterface $appCacheForecast:@app.cache.forecast

Works also for controller actions and service subscribers (using the real service id as the key).

tgalopin, dmaicher, yceruto, andreybolonin, sstok, vudaltsov, vria, apfelbox, sergiu-popa, GaryPEGEOT, and 2 more reacted with thumbs up emoji
@nicolas-grekas
Copy link
MemberAuthor

nicolas-grekas commentedAug 21, 2018
edited
Loading

Now with a newContainerBuilder::registerAliasForArgument() method used in FrameworkExtension to create such aliases for cache pool, lock resources and messenger buses.
E.g. type hintpublic function __construct(MessageBusInterface $commandBus) to get the bus declared as "command_bus" in your config.

Copy link
Member

@weaverryanweaverryan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I love this. It solves the last weird autowiring situation in an automated way: classes that have multiple services (cache, log, etc).

We'll need to updatedebug:autowiring to show this. We want to update it anyways to be a bit more "helpful" in general. We need to do that for 4.2.

$definition->addTag('cache.pool',$pool);
$container->setDefinition($name,$definition);
$container->registerAliasForArgument($name, CacheInterface::class);
$container->registerAliasForArgument($name, CacheItemPoolInterface::class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Will this work ok for the$name = '.'.$name.'.inner'; cases above?

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

good catch, moved up in the foreach!

/**
* Registers an alias for autowiring the passed id using named arguments.
*/
publicfunctionregisterAliasForArgument(string$id,string$type,string$name =null):Alias
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Can we doc the arguments? Because we normalize them into lower-camel case, I think it IS worth saying what each argument means

Copy link
MemberAuthor

@nicolas-grekasnicolas-grekasAug 21, 2018
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I added a docblock description, good enough?

Copy link
Member

@weaverryanweaverryan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

+1 for me. But, this makes updatingdebug:autowiring an absolute must for 4.2.

Copy link
Contributor

@srozesroze left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

That's super cool. Though, could you add a test for a service namedsome_service (which is claimed to work on the PhpDoc but tests are only usingsome.service)?

$container->setAlias('message_bus',$busId)->setPublic(true);
$container->setAlias(MessageBusInterface::class,$busId);
}else {
$container->registerAliasForArgument($busId, MessageBusInterface::class);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Any reason why we wouldn't register every bus, including the one that has been chosen as the default? The question also works for the lock (because in cache, all of them or registered)

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Yes: because the default service is already the one wired... by default :)

@sroze
Copy link
Contributor

I really like it!

@nicolas-grekas
Copy link
MemberAuthor

add a test for a service named some_service

done asContainerBuilderTest::testRegisterAliasForArgument()

@nicolas-grekasnicolas-grekas merged commitc0b8f53 intosymfony:masterAug 24, 2018
nicolas-grekas added a commit that referenced this pull requestAug 24, 2018
…s-grekas)This PR was merged into the 4.2-dev branch.Discussion----------[DI] Allow autowiring by type + parameter name| Q             | A| ------------- | ---| Branch?       | master| Bug fix?      | no| New feature?  | yes| BC breaks?    | no| Deprecations? | no| Tests pass?   | yes| Fixed tickets | -| License       | MIT| Doc PR        |symfony/symfony-docs#10206In#27165, we introduced the possibility to bind by type+name:```yamlbind:    Psr\Log\LoggerInterface $myLogger: @monolog.logger.my_logger```But we forgot about aliases. For consistency, they could and should allow doing the same. More importantly, this will open up interesting use cases where bundles could provide default values for typed+named arguments (using the new `ContainerBuilder::registerAliasForArgument()` method). E.g:```yamlservices:    Psr\Cache\CacheItemPoolInterface $appCacheForecast: @app.cache.forecast```Works also for controller actions and service subscribers (using the real service id as the key).Commits-------c0b8f53 [DI] Allow autowiring by type + parameter name
@nicolas-grekasnicolas-grekas deleted the di-arg-alias branchAugust 24, 2018 13:46
nicolas-grekas added a commit that referenced this pull requestOct 29, 2018
…rvices and their description (nicolas-grekas)This PR was merged into the 4.2-dev branch.Discussion----------[FrameworkBundle] make debug:autowiring list useful services and their description| Q             | A| ------------- | ---| Branch?       | 4.2| Bug fix?      | yes| New feature?  | yes| BC breaks?    | no| Deprecations? | no| Tests pass?   | yes| Fixed tickets |#27207| License       | MIT| Doc PR        | -This PRcloses#27207: we don't need "semantics" anymore. 4.2 has everything already to allow us to separate useful services from the other ones: any autowireable *alias* **is** a useful service!Add autowiring by type + parameter name (#28234) and the story is done: we can easily hint people about which features their bundles provide, without being polluted by for-wiring-only services.Here is a screenshot running this command(before I excluded the aliases for $cacheApp and $cacheSystem):![image 3](https://user-images.githubusercontent.com/243674/47437006-f41f4380-d7a7-11e8-9f76-7f23e9193ce8.png)ping@weaverryan as we drafted that together.Fixes a few issues found meanwhile.That should definitely go in 4.2.Commits-------56aab09 [FrameworkBundle] make debug:autowiring list useful services and their description
@nicolas-grekasnicolas-grekas modified the milestones:next,4.2Nov 1, 2018
This was referencedNov 3, 2018
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@dunglasdunglasdunglas approved these changes

@weaverryanweaverryanweaverryan approved these changes

+1 more reviewer

@srozesrozesroze approved these changes

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Projects

None yet

Milestone

4.2

Development

Successfully merging this pull request may close these issues.

5 participants

@nicolas-grekas@sroze@dunglas@weaverryan@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp