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

Appearance settings

[DependencyInjection] Simplify using DI attributes with ServiceLocator/Iterator's #51916

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

Merged
merged 1 commit into from
Oct 12, 2023

Conversation

kbond
Copy link
Member

@kbond kbond commented Oct 9, 2023

Q A
Branch? 6.4
Bug fix? no
New feature? yes
Deprecations? no
Tickets n/a
License MIT

Instead of:

#[AutowireLocator([
    'subscribed' => new SubscribedService(type: 'string', attributes: new Autowire('%some.parameter%')),
])]

This PR allows:

#[AutowireLocator([
    'subscribed' => new Autowire('%some.parameter%'),
])]

@carsonbot carsonbot added this to the 6.4 milestone Oct 9, 2023
@carsonbot carsonbot changed the title [DI] Simplify using DI attributes with ServiceLocator/Iterator's [DependencyInjection] Simplify using DI attributes with ServiceLocator/Iterator's Oct 9, 2023
@kbond
Copy link
Member Author

kbond commented Oct 10, 2023

The current implementation restricts the usage to just AutowireLocator|Iterator and only with the Autowire attribute. Should I make it work with ServiceSubscriberInterface? Are there any other DI attributes it should work with (I'm thinking no as all the relevent ones extend Autowire)?

@nicolas-grekas
Copy link
Member

The current implementation restricts the usage to just AutowireLocator|Iterator and only with the Autowire attribute. Should I make it work with ServiceSubscriberInterface?

Making ServiceSubscriberInterface aware of Autowire* attributes would mean we'd have to move them to contracts. I don't think we're ready for that. Before, we might need to figure out how SubscribedService and Autowire relate to each other.

But we could make RegisterServiceSubscribersPass aware of Autowire* attributes yes.

Are there any other DI attributes it should work with (I'm thinking no as all the relevent ones extend Autowire)?

Nope as you spotted.

@nicolas-grekas nicolas-grekas force-pushed the simplify-nested-di-attribute branch from 7c0d807 to 33169fe Compare October 12, 2023 12:37
Copy link
Member

@nicolas-grekas nicolas-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 pushed some changes, let's wait for tests and GTM.


$references = [];

foreach ($services as $key => $type) {
Copy link
Member

Choose a reason for hiding this comment

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

I've moved this logic to AutowireLocator: while working on this PR, I realized that it doesn't make sense to support AutowireLocator([FooInterface::class]). People that really want a static iterable as default can use Autowire([new Autowire(FooInterface::class)]) instead.

$attributes = [];

if ($type instanceof Autowire) {
$references[$key] = $type;
Copy link
Member

Choose a reason for hiding this comment

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

no need for more here, AutowirePass will turn that into a TypedReference on its own

@@ -78,6 +79,11 @@ protected function processValue(mixed $value, bool $isRoot = false): mixed
foreach ($class::getSubscribedServices() as $key => $type) {
$attributes = [];

if (!isset($serviceMap[$key]) && $type instanceof Autowire) {
$subscriberMap[$key] = $type;
Copy link
Member

Choose a reason for hiding this comment

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

Adding support for new Autowire being returned by getSubscribedServices, for consistency, altough it's not in contracts, it might still be convenient.

@@ -63,7 +63,6 @@
"symfony/http-client-contracts": "<2.5",
"symfony/mailer": "<5.4",
"symfony/messenger": "<5.4",
"symfony/service-contracts": "<3.2",
Copy link
Member

Choose a reason for hiding this comment

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

as requested, since this is an optional feature

Copy link
Contributor

Choose a reason for hiding this comment

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

👏 thanks!

@nicolas-grekas nicolas-grekas merged commit 253968e into symfony:6.4 Oct 12, 2023
@kbond kbond deleted the simplify-nested-di-attribute branch October 12, 2023 13:20
@kbond
Copy link
Member Author

kbond commented Oct 12, 2023

Thanks for finalizing!

@keradus keradus mentioned this pull request Oct 24, 2023
nicolas-grekas added a commit that referenced this pull request Oct 25, 2023
This PR was merged into the 6.4 branch.

Discussion
----------

DX: drop unused import

appendix for #51916

Commits
-------

cfe219e DX: drop unused import
nicolas-grekas added a commit that referenced this pull request Nov 25, 2024
This PR was merged into the 6.4 branch.

Discussion
----------

[DependencyInjection] Fix PhpDoc type

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Issues        | -
| License       | MIT

Was added in #51916.

Commits
-------

b533c7d [DependencyInjection] Fix PhpDoc type
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.