@@ -110,17 +110,36 @@ 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+ A service locator is a PSR11 container that contains a set of services,
114+ but only instantiates them when they are actually used. Let's take a closer
115+ look at this part::
116+
117+ // ...
118+ $handler = $this->locator->get($commandClass);
119+
120+ return $handler->handle($command);
121+
122+ In the example above, the ``$handler `` service is only instantiated when the
123+ ``$this->locator->get($commandClass) `` method is called.
124+
125+ You can also type-hint the service locator argument with
126+ :class: `Symfony\\ Contracts\\ Service\\ ServiceCollectionInterface ` instead of
127+ :class: `Psr\\ Container\\ ContainerInterface `. By doing so, you'll be able to
128+ count and iterate over the services of the locator::
116129
117130 // ...
118131 $numberOfHandlers = count($this->locator);
119132 $nameOfHandlers = array_keys($this->locator->getProvidedServices());
120- // ...
121- $handler = ($this->locator)($commandClass);
122133
123- return $handler->handle($command);
134+ // you can iterate through all services of the locator
135+ foreach ($this->locator as $serviceId => $service) {
136+ // do something with the service, the service id or both
137+ }
138+
139+ .. versionadded :: 7.1
140+
141+ The :class: `Symfony\\ Contracts\\ Service\\ ServiceCollectionInterface ` was
142+ introduced in Symfony 7.1.
124143
125144Including Services
126145------------------
0 commit comments