Skip to content

[Contracts][DependencyInjection] Add ServiceCollectionInterface #19370

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
Jan 12, 2024

Conversation

alexandre-daubois
Copy link
Member

Fix #19368

Ping @kbond 🙂

Copy link
Member

@kbond kbond 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 working on this Alex!

@alexandre-daubois
Copy link
Member Author

Thanks for your suggestions Kevin! I reworked the PR, is it better now? 🙂

foreach ($this->locator as $serviceId => $service) {
// do something with the service, the service id or both
}

// ...
$handler = ($this->locator)($commandClass);
Copy link
Member

@kbond kbond Jan 3, 2024

Choose a reason for hiding this comment

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

ServiceLocator is indeed callable but this isn't part of the contracts:

public function someAction(ServiceCollectionInterface $locator)
{
    $handler = ($locator)($commandClass); // this works but static analysis will complain
}

I'd prefer removing this in favor of $this->locator->get($commandClass) but as we've shown this callable example for some time, I'm not sure... @nicolas-grekas, what do you think. I don't have the context on why ServiceLocator was made callable initially.

Copy link
Member

Choose a reason for hiding this comment

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

I would reformulate the whole paragraph. Why tell that the injected service is some implementation-specific class while only an interface is type hinted? This is a bad hint that encourages against SOLID principles.

Copy link
Member Author

Choose a reason for hiding this comment

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

Alright, I tried something totally different not mentioning "internal" implementation but only explaining the locators are countable and traversable.

which implements both the PSR-11 ``ContainerInterface`` and :class:`Symfony\\Contracts\\Service\\ServiceProviderInterface`.
It is also a callable and a countable::
The injected service is an instance of
:class:`Symfony\\Component\\DependencyInjection\\ServiceLocator`. A service
Copy link
Member

Choose a reason for hiding this comment

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

I still wouldn't mention this, it's an implementation detail.
We should tell about ServiceCollectionInterface: if ppl type-hint for it, then they will get an object that is coutable+iterable, in addition to being a PSR11 locator.

@alexandre-daubois
Copy link
Member Author

I tried to remove useless implementation details and go for the type-hint approach Nicolas suggested.

@javiereguiluz
Copy link
Member

A nice contribution! Thanks a lot Alex and thanks for your patience during the review 🙏

Thanks also to Kevin and Nicolas for the review!

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.

[Contracts][DependencyInjection] Add ServiceCollectionInterface
5 participants