-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DependencyInjection] add Autowire
parameter attribute
#45657
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
Conversation
2824eb9
to
fc249f1
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.
What about calling Yaml::parse()
before calling YamlFileLoader::resolveServices()
?
src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php
Outdated
Show resolved
Hide resolved
I'm withdrawing this comment, bad idea :) Another idea, using named arguments:
I think that's my preference: no custom yaml-like strings and more explicit than standalone attributes. |
fc249f1
to
2fa653b
Compare
I've made this change and also added the raw What about controller action arguments? Separate PR? |
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.
What about controller action arguments? Separate PR?
same PR to me, it's the same topic
src/Symfony/Component/DependencyInjection/Attribute/Autowire.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/DependencyInjection/Attribute/Autowire.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/DependencyInjection/Attribute/Autowire.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php
Outdated
Show resolved
Hide resolved
2fa653b
to
c58e6ea
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.
Here are some minor comments. I like it. How about you? Let's close the two previous ones?
src/Symfony/Component/DependencyInjection/Attribute/Autowire.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/DependencyInjection/Attribute/Autowire.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/DependencyInjection/Attribute/Autowire.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/DependencyInjection/Attribute/Autowire.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/DependencyInjection/Attribute/Autowire.php
Outdated
Show resolved
Hide resolved
735a6ee
to
d3a1794
Compare
Made these changes. I am happy with it as well - let's close them! Few questions:
|
That'd make sense yes
expressions/parameters should be allowed. There is a hack using 'current' as factory right now, you'll see it in the pass. |
src/Symfony/Component/DependencyInjection/Attribute/Autowire.php
Outdated
Show resolved
Hide resolved
d3a1794
to
d360af6
Compare
src/Symfony/Component/DependencyInjection/Attribute/Autowire.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes_80.php
Outdated
Show resolved
Hide resolved
45d49dc
to
ac210cf
Compare
Ok, I have the controller argument support working but implementation might need some work. |
ac210cf
to
000de70
Compare
src/Symfony/Component/HttpKernel/DependencyInjection/RegisterControllerArgumentLocatorsPass.php
Outdated
Show resolved
Hide resolved
...omponent/HttpKernel/Tests/DependencyInjection/RegisterControllerArgumentLocatorsPassTest.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/HttpKernel/DependencyInjection/RegisterControllerArgumentLocatorsPass.php
Outdated
Show resolved
Hide resolved
63094c4
to
d9b0417
Compare
d9b0417
to
4155854
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 like it, thanks!
(I pushed some tweaks)
4155854
to
4306dbf
Compare
4306dbf
to
d43fe42
Compare
Thank you @kbond. |
…ementation (kbond) This PR was merged into the 6.1 branch. Discussion ---------- [DependencyInjection] adjust `Autowire` attribute implementation | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | #45657 (comment) | License | MIT | Doc PR | todo Per discussion with @nicolas-grekas, we've decided to improve the DX of the attribute a bit. The implementation from #45657 still works as described there. This allows for the first `Autowire` constructor argument to be used for services/expressions by adding a _familiar_ prefix (`@` for services, `@=` for expressions): ```php class MyService { public function __construct( #[Autowire('@some_service')] private $service1, #[Autowire('@=service("App\\Mail\\MailerConfiguration").getMailerMethod()') private $service2, #[Autowire('%env(json:file:resolve:AUTH_FILE)%')] private $parameter1, ) {} } ``` Commits ------- c86aa7a [DependencyInjection] adjust `Autowire` attribute implementation
…attribute (kbond) This PR was merged into the 6.1 branch. Discussion ---------- [DependencyInjection][HttpKernel] document `Autowire` attribute Symfony PRs: symfony/symfony#45657 & symfony/symfony#45783 Closes #16625. Closes #16636 Commits ------- bf8be7e Move the autowire attribute sections f32aec1 [DI][HttpKernel] document `Autowire` attribute
* | ||
* @author Kevin Bond <kevinbond@gmail.com> | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_PARAMETER)] |
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.
Do you think this #[\Attribute(\Attribute::TARGET_PARAMETER, \Attribute::TARGET_PROPERTY)]
can be used here? When using Attribute with promoted property constructor.
There was a problem hiding this comment.
10000Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#[\Attribute(\Attribute::TARGET_PARAMETER]
can be used on promoted properties. See https://github.com/symfony/symfony/pull/45657/files#diff-a4f7e7528218aa6d151b1e68834393c6ae958540ec31ffa49ce3207998e77dd2R33
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.
why would we? TARGET_PARAMETER
is enough to target a promoted argument
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.
Yes you're right TARGET_PARAMETER
is enough here. Thank you for your quick response :)
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 facing the same problem.
TARGET_PARAMETER
is working as well with properties for constructor, however not working especially with reflection mechanism on reflection property.
See https://3v4l.org/gDeYA what I mean.
Also PHPStorm suggests not to use this attribute on property promoted:
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.
Could this be a bug in PHP or a missing detail in the implementation? The opposite is a problem too: https://3v4l.org/tBjj2. Somewhat related PHPStan issue: phpstan/phpstan#4418.
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.
As far as I understand, PHPStan literally "silence" this situation, right?
For me isn't looks like a PHP bug, in my opinion we should add TARGET_PROPERTY flag. ;)
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.
To me it's a limitation of the engine and the engine should be improved: from a descriptive pov, both variants give all the possible hints to make things clear. The engine is not clever enough yet.
The alternative is to not use CPP but it would be a shame to accept this state of things :) Patching the attribute would be wrong: Autowire should NOT be allowed on properties.
Could you please open a bug report about this on php-src?
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.
See https://3v4l.org/gDeYA what I mean.
Calling newInstance()
for arbitrary attributes is a footgun. Attributes could point to undefined classes and the attribute constructor could have side effects. The reflection API by design provides you with enough information to decide whether you need an actual instance of an attribute. Only call newInstance()
on attributes you know and that your code actually needs to work with.
I agree with @nicolas-grekas that there's nothing we should do on our side.
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 agree as well. At least until a time where the attribute can actually be used with property injection.
Introduced in symfony#45657 symfony/dependency-injection 6.4+ is required, so the class always exists
Introduced in symfony#45657 symfony/dependency-injection 6.4+ is required, so the class always exists
…f DI `Autowire` class (GromNaN) This PR was merged into the 7.3 branch. Discussion ---------- [HttpKernel] Remove always-true condition on existence of DI `Autowire` class | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | no | New feature? | no | Deprecations? | no | Issues | - | License | MIT This condition was introduced in #45657 The class `RegisterControllerArgumentLocatorsPass` requires the DI component, which cannot be < 6.4 due to a [conflict rule in composer.json](https://github.com/symfony/symfony/blob/79ea49c772ce4b39f414cde5648ad347c3bbcfd7/src/Symfony/Component/HttpKernel/composer.json#L33). So the `Autoconfigure` class always exists. Commits ------- 3975043 Remove always-true condition
Introduced in symfony/symfony#45657 symfony/dependency-injection 6.4+ is required, so the class always exists
Replaces #45573 & #44780 with a single new
Autowire
attribute:Works with controller arguments as well: