Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[DependencyInjection] addAutowire 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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
nicolas-grekas left a comment• edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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 callingYaml::parse() before callingYamlFileLoader::resolveServices()?
src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
nicolas-grekas commentedMar 7, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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. |
kbond commentedMar 7, 2022
I've made this change and also added theraw What about controller action arguments? Separate PR? |
nicolas-grekas left a comment
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
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
nicolas-grekas left a comment
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?
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
735a6ee tod3a1794Comparekbond commentedMar 7, 2022
Made these changes. I am happy with it as well - let's close them! Few questions:
|
nicolas-grekas commentedMar 7, 2022
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. |
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/autowiring_classes_80.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
45d49dc toac210cfComparekbond commentedMar 7, 2022
Ok, I have the controller argument support working but implementation might need some work. |
src/Symfony/Component/HttpKernel/DependencyInjection/RegisterControllerArgumentLocatorsPass.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
...omponent/HttpKernel/Tests/DependencyInjection/RegisterControllerArgumentLocatorsPassTest.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
src/Symfony/Component/HttpKernel/DependencyInjection/RegisterControllerArgumentLocatorsPass.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
63094c4 tod9b0417Compared9b0417 to4155854Compare
nicolas-grekas left a comment• edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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 pushedsome tweaks)
4155854 to4306dbfCompare4306dbf tod43fe42Comparefabpot commentedMar 18, 2022
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 | todoPer 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):```phpclass 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` attributeSymfony PRs:symfony/symfony#45657 &symfony/symfony#45783Closes#16625.Closes#16636Commits-------bf8be7e Move the autowire attribute sectionsf32aec1 [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.
Choose 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. Seehttps://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 rightTARGET_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.
Seehttps://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. ;)
nicolas-grekasJun 20, 2022 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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.
Seehttps://3v4l.org/gDeYA what I mean.
CallingnewInstance() 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 callnewInstance() 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 withproperty injection.
Introduced insymfony#45657symfony/dependency-injection 6.4+ is required, so the class always exists
Introduced insymfony#45657symfony/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 | MITThis condition was introduced in#45657The 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 insymfony/symfony#45657symfony/dependency-injection 6.4+ is required, so the class always exists
Uh oh!
There was an error while loading.Please reload this page.
Replaces#45573 껬 with a single new
Autowireattribute:Works with controller arguments as well: