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] Add#[AutowireInline] attribute to allow service definition at the class level#52820

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

Conversation

@DaDeather
Copy link
Contributor

@DaDeatherDaDeather commentedNov 30, 2023
edited
Loading

QA
Branch7.1
Bug fixno
New featureyes
Deprecationsno
IssuesFix#52819
LicenseMIT

For the idea behind this feature see the issue that contains examples#52819

Example usage:

class SomeSourceAwareLogger{publicfunction__construct(privatereadonlyLoggerInterface$logger,privatereadonlystring$someSource,    ) {    }}class SomeSourceAwareLoggerFactory{publicfunction__construct(privatereadonlyLoggerInterface$logger,    ) {    }publicfunctioncreate(string$someSource):SomeSourceAwareLogger    {returnnewSomeSourceAwareLogger($this->logger,$someSource);    }publicstaticfunctionstaticCreate(LoggerInterface$logger,string$someSource):SomeSourceAwareLogger    {returnnewSomeSourceAwareLogger($logger,$someSource);    }}// -----------class SomeClass1{publicfunction__construct(        #[AutowireInline(class: SomeSourceAwareLogger::class, args: [newReference(LoggerInterface::class),'bar'])]publicSomeSourceAwareLogger$someSourceAwareLogger,    ) {    }}// AND/ORclass SomeClass2{publicfunction__construct(        #[AutowireInline(            class: SomeSourceAwareLogger::class,            factory: [SomeSourceAwareLoggerFactory::class,'staticCreate'],            args: [newReference(LoggerInterface::class),'someParam'],        )]publicSomeSourceAwareLogger$someSourceAwareLogger,    ) {    }}// AND/ORclass SomeClass3{publicfunction__construct(        #[AutowireInline(            class: SomeSourceAwareLogger::class,            factory: [newReference(SomeSourceAwareLoggerFactory::class),'create'],            args: ['someParam'],        )]publicSomeSourceAwareLogger$someSourceAwareLogger,    ) {    }}

Jeroeny, valtzu, sstok, and BafS reacted with thumbs up emoji
@carsonbotcarsonbot added this to the6.4 milestoneNov 30, 2023
@DaDeatherDaDeatherforce-pushed the52819-dependency-injection-new-inline-attributes branch 2 times, most recently from6ca710b toc547abcCompareNovember 30, 2023 11:19
@nicolas-grekas
Copy link
Member

Thanks for proposing and for submitting!

I'm wondering if we couldn't get rid of the interface and rename the proposed argument to e.g.AutowireInline?
To remove the interface, we could maybe make AutowireCallable extend AutowireInline.
Also, instead of a dedicated attribute for services built by factory, can't we add afactory argument to the AutowireInline attribute?

@DaDeather
Copy link
ContributorAuthor

Thanks for proposing and for submitting!

I'm wondering if we couldn't get rid of the interface and rename the proposed argument to e.g.AutowireInline? To remove the interface, we could maybe make AutowireCallable extend AutowireInline. Also, instead of a dedicated attribute for services built by factory, can't we add afactory argument to the AutowireInline attribute?

Sure I was unsure about the naming anyway though 🙈.

About the interface:
Wouldn't it rather make sense to keep it to allow extending / allowing custom Attributes that may create definitions that could be used for the Autowiring to be handled?

I'd rather see it as a benefit there allowing one to implement the interface and go for it instead of extending the base classes like theAutowireInline (after renaming). Unless this may be undesired anyway 🤷. Just being curious here.

Combining the factory thing into theAutowireInline seems fine as well since both are inlined 👍.

I made the adjustments according to your comment unless the one with the Interface. Just let me know if I still should remove it / rename it or whatever 👍.

And thanks for your fast feedback on this!

@DaDeatherDaDeatherforce-pushed the52819-dependency-injection-new-inline-attributes branch 6 times, most recently fromb9abcbc to48fc63bCompareDecember 1, 2023 07:42
Copy link
Member

@nicolas-grekasnicolas-grekas left a comment

Choose a reason for hiding this comment

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

I'm not convinced by the interface, I'd prefer removing it.
I'm generally not convinced by interfaces for value objects and attributes are value objects.
Extending the class makes sense to me in this case.

@DaDeatherDaDeatherforce-pushed the52819-dependency-injection-new-inline-attributes branch from48fc63b tof21a70bCompareDecember 1, 2023 09:15
@DaDeather
Copy link
ContributorAuthor

I'm not convinced by the interface, I'd prefer removing it. I'm generally not convinced by interfaces for value objects and attributes are value objects. Extending the class makes sense to me in this case.

Alright 👍 I have removed the interface and based theAutowireCallable on the newAutowireInline as you proposed before.

@DaDeatherDaDeatherforce-pushed the52819-dependency-injection-new-inline-attributes branch fromf21a70b to917a698CompareDecember 1, 2023 09:21
@DaDeatherDaDeather changed the title[DependencyInjection] add InlineService and InlineFactory attributes to allow service configuration on class level[DependencyInjection] add AutowireInline attribute to allow service configuration on class levelDec 1, 2023
@stof
Copy link
Member

stof commentedDec 1, 2023
edited
Loading

Please update the PR description to show usages of the feature. This makes it a lot easier for the documentation team (and also for reviewers)

@DaDeatherDaDeatherforce-pushed the52819-dependency-injection-new-inline-attributes branch 3 times, most recently from5955b18 to6a7e3fcCompareFebruary 27, 2024 05:49
@nicolas-grekas
Copy link
Member

About my remaining concerns (ResolveNamedArgumentsPass,ResolveChildDefinitionPass and other pass not running), I'm wondering if we shouldn't add a newResolveAutowireInlineAttributesPass that would scan autowired services for this attribute and would register definitions for each of them. Then, based on some naming convention (likely built onContainer::hash($definition)),AutowirePass would just reference these definitions instead of callingAutowireInline::buildDefinition().

WDYT? Up to give it a try?

@DaDeather
Copy link
ContributorAuthor

About my remaining concerns (ResolveNamedArgumentsPass,ResolveChildDefinitionPass and other pass not running), I'm wondering if we shouldn't add a newResolveAutowireInlineAttributesPass that would scan autowired services for this attribute and would register definitions for each of them. Then, based on some naming convention (likely built onContainer::hash($definition)),AutowirePass would just reference these definitions instead of callingAutowireInline::buildDefinition().

WDYT? Up to give it a try?

Sure!
Could you maybe give a few hints how / where to implement this correctly?
I'm struggling a bit with the input style and the expected outcome here and therefore maybe didn't understand what's really expected here.

Would you expect a definition be like this:

class AutowireInlineAttributesBar {publicfunction__construct(Foo$foo,string$someString)    {    }}class AutowireInlineAttributes{publicfunction__construct(        #[AutowireInline(AutowireInlineAttributesBar::class, ['$foo' => Foo::class,'$someString' =>'testString',        ])]publicAutowireInlineAttributesBar$inlined,    ) {    }}

Where the classFoo then would be a autowired as the provided service name and thesomeString injected as a string or what would you rather expect here to happen?

Besides that if I got you right I would have to create a class calledResolveAutowireInlineAttributesPass which then checks for a passedDefinition to be$definition->isAutowired() === true and check for it to have anyAutowireInline attributes to be processed (calling->buildDefinition(...) and registering those via$this->container->setDefinition(ContainerBuilder::hash($autowireInlineAttributeInstance), $definition);.

Is this assumption correct?

@nicolas-grekas
Copy link
Member

You've got it right I think :)

@DaDeather
Copy link
ContributorAuthor

DaDeather commentedMar 7, 2024
edited
Loading

You've got it right I think :)

I've prepared something that doesn't quite work 🤷 as mentioned before maybe I'm missing something here. Could you maybe take a look so that we may correct the issues there or my misunderstanding 🙈?

I would be grateful 👍dd16390

@nicolas-grekas

Copy link
Member

@nicolas-grekasnicolas-grekas left a comment

Choose a reason for hiding this comment

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

Thanks for the update, the path looks good to me.
Here are some comments to go to the next step.

* @author Ismail Özgün Turan <oezguen.turan@dadadev.com>
*/
#[\Attribute(\Attribute::TARGET_PARAMETER)]
class AutowireInlineextends Autowire
Copy link
Member

@chalasrchalasrMar 20, 2024
edited
Loading

Choose a reason for hiding this comment

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

AutowireInline looks pretty cryptic, any newcomer wouldn't be able to get what the attribute is for by just looking at its name nor its description as it currently is. I think we either need to find a super self-explanatory name (I've no clue) or extend the description so that it tells what's the purpose of the attribute and when it should be used (inline service definition only means something to advanced Symfony's DIC hackers)

kbond reacted with thumbs up emoji

Choose a reason for hiding this comment

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

Naming things... :)
"inline" refers to "inline_service()" in the PHP-DSL

AutowireInlineService? but verbose
AutowireNew? AutowireObject? AutowireInstance? or keep AutowireInline?

of course, a top notch description is also desirable

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

I added a better descriptionhere feel free to request further suggestments to it 👍.

@DaDeatherDaDeatherforce-pushed the52819-dependency-injection-new-inline-attributes branch 3 times, most recently from0ad67af to5144ceaCompareApril 2, 2024 08:02
@nicolas-grekasnicolas-grekasforce-pushed the52819-dependency-injection-new-inline-attributes branch 2 times, most recently from964fe78 to19b1076CompareApril 22, 2024 13:48
@nicolas-grekasnicolas-grekas added Ready ❄️ Feature FreezeImportant Pull Requests to finish before the next Symfony "feature freeze" and removed Status: Needs Work labelsApr 22, 2024
Copy link
Member

@nicolas-grekasnicolas-grekas left a comment

Choose a reason for hiding this comment

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

PR is ready.
I added a second commit to make the implementation a bit more generic.
Reviews welcome 🙏

DaDeather reacted with heart emoji
if (isset($this->definitions[$id])) {
unset($this->definitions[$id]);
$this->removedIds[$id] =true;
if ('.' !== ($id[0] ??'-')) {

Choose a reason for hiding this comment

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

I added this rule so that we don't use internal service ids in error messages. This matters to this PR because the added code creates new removed-ids, which means fixtures have to be updated with (what I consider as) noise.

@nicolas-grekasnicolas-grekasforce-pushed the52819-dependency-injection-new-inline-attributes branch from19b1076 tob9a838eCompareMay 2, 2024 08:54
@fabpot
Copy link
Member

Thank you@DaDeather.

DaDeather reacted with thumbs up emoji

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@nicolas-grekasnicolas-grekasnicolas-grekas approved these changes

@stofstofstof left review comments

@chalasrchalasrchalasr left review comments

@dunglasdunglasAwaiting requested review from dunglasdunglas is a code owner

@xabbuhxabbuhAwaiting requested review from xabbuh

@lyrixxlyrixxAwaiting requested review from lyrixx

@ycerutoycerutoAwaiting requested review from yceruto

@welcoMatticwelcoMatticAwaiting requested review from welcoMattic

@kbondkbondAwaiting requested review from kbond

@OskarStarkOskarStarkAwaiting requested review from OskarStark

@jderussejderusseAwaiting requested review from jderusse

Assignees

No one assigned

Labels

DependencyInjectionReady❄️ Feature FreezeImportant Pull Requests to finish before the next Symfony "feature freeze"Status: Reviewed

Projects

None yet

Milestone

7.1

Development

Successfully merging this pull request may close these issues.

[DependencyInjection] Add new attributes to allow inlined services while using autowiring

6 participants

@DaDeather@nicolas-grekas@stof@fabpot@chalasr@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp