Skip to content

Commit 5a6313b

Browse files
[Contracts][DependencyInjection] Mention that locators are traversable
1 parent 0b70c22 commit 5a6313b

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

service_container/service_subscribers_locators.rst

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,30 @@ in the service subscriber::
110110
that you have :ref:`autoconfigure <services-autoconfigure>` enabled. You
111111
can also manually add the ``container.service_subscriber`` tag.
112112

113-
The injected service is an instance of :class:`Symfony\\Component\\DependencyInjection\\ServiceLocator`
114-
which implements both the PSR-11 ``ContainerInterface`` and :class:`Symfony\\Contracts\\Service\\ServiceProviderInterface`.
115-
It is also a callable and a countable::
113+
The injected service is an instance of
114+
:class:`Symfony\\Component\\DependencyInjection\\ServiceLocator`. A service
115+
locator is a container that contains a set of services, but only instantiates
116+
them when they are actually used. The service locator is countable and
117+
traversable::
116118

117119
// ...
118120
$numberOfHandlers = count($this->locator);
119121
$nameOfHandlers = array_keys($this->locator->getProvidedServices());
122+
123+
// you can iterate through all services of the locator
124+
foreach ($this->locator as $serviceId => $service) {
125+
// do something with the service, the service id or both
126+
}
127+
120128
// ...
121-
$handler = ($this->locator)($commandClass);
129+
$handler = $this->locator->get($commandClass);
122130

123131
return $handler->handle($command);
124132

133+
.. versionadded:: 7.1
134+
135+
Service locators are traversable since Symfony 7.1.
136+
125137
Including Services
126138
------------------
127139

0 commit comments

Comments
 (0)