-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[DependencyInjection] Add #[AutowireInline]
attribute to allow service definition at the class level
#52820
Conversation
6ca710b
to
c547abc
Compare
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. |
Sure I was unsure about the naming anyway though 🙈. About the interface: 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 the Combining the factory thing into the 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! |
b9abcbc
to
48fc63b
Compare
There was a problem hiding this 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.
src/Symfony/Component/DependencyInjection/Attribute/AutowireInline.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/DependencyInjection/Attribute/AutowireInline.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/DependencyInjection/Attribute/AutowireInline.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/DependencyInjection/Attribute/AutowireInline.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/DependencyInjection/Attribute/AutowireInline.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/DependencyInjection/Attribute/AutowireInline.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php
Outdated
Show resolved
Hide resolved
48fc63b
to
f21a70b
Compare
Alright 👍 I have removed the interface and based the |
f21a70b
to
917a698
Compare
src/Symfony/Component/DependencyInjection/Attribute/AutowireInline.php
Outdated
Show resolved
Hide resolved
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) |
917a698
to
86b2806
Compare
5955b18
to
6a7e3fc
Compare
About my remaining concerns ( WDYT? Up to give it a try? |
Sure! Would you expect a definition be like this: class AutowireInlineAttributesBar {
public function __construct(Foo $foo, string $someString)
{
}
}
class AutowireInlineAttributes
{
public function __construct(
#[AutowireInline(AutowireInlineAttributesBar::class, [
'$foo' => Foo::class,
'$someString' => 'testString',
])]
public AutowireInlineAttributesBar $inlined,
) {
}
} Where the class Besides that if I got you right I would have to create a class called Is this assumption correct? |
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 |
There was a problem hiding this 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.
src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/DependencyInjection/Compiler/PassConfig.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes_80.php
Outdated
Show resolved
Hide resolved
...ony/Component/DependencyInjection/Tests/Compiler/ResolveAutowireInlineAttributesPassTest.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/DependencyInjection/Compiler/ResolveAutowireInlineAttributesPass.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/DependencyInjection/Compiler/ResolveAutowireInlineAttributesPass.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/DependencyInjection/Compiler/ResolveAutowireInlineAttributesPass.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/DependencyInjection/Attribute/AutowireInline.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/DependencyInjection/Compiler/ResolveAutowireInlineAttributesPass.php
Outdated
Show resolved
Hide resolved
* @author Ismail Özgün Turan <[email protected]> | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_PARAMETER)] | ||
class AutowireInline extends Autowire |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a better description here feel free to request further suggestments to it 👍.
0ad67af
to
5144cea
Compare
964fe78
to
19b1076
Compare
There was a problem hiding this 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 🙏
@@ -494,7 +494,9 @@ public function removeDefinition(string $id): void | |||
{ | |||
if (isset($this->definitions[$id])) { | |||
unset($this->definitions[$id]); | |||
$this->removedIds[$id] = true; | |||
if ('.' !== ($id[0] ?? '-')) { |
There was a problem hiding this comment.
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.
…ice definition at the class level
19b1076
to
b9a838e
Compare
Thank you @DaDeather. |
For the idea behind this feature see the issue that contains examples #52819
Example usage: