Skip to content

Commit 8463c3a

Browse files
committed
[DependencyInjection] Add ServiceCollectionInterface
1 parent 2791972 commit 8463c3a

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add argument `$prepend` to `ContainerConfigurator::extension()` to prepend the configuration instead of appending it
8+
* Have `ServiceLocator` implement `ServiceCollectionInterface`
89

910
7.0
1011
---

ServiceLocator.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
1717
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
1818
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
19+
use Symfony\Contracts\Service\ServiceCollectionInterface;
1920
use Symfony\Contracts\Service\ServiceLocatorTrait;
20-
use Symfony\Contracts\Service\ServiceProviderInterface;
2121
use Symfony\Contracts\Service\ServiceSubscriberInterface;
2222

2323
/**
@@ -26,9 +26,9 @@
2626
*
2727
* @template-covariant T of mixed
2828
*
29-
* @implements ServiceProviderInterface<T>
29+
* @implements ServiceCollectionInterface<T>
3030
*/
31-
class ServiceLocator implements ServiceProviderInterface, \Countable
31+
class ServiceLocator implements ServiceCollectionInterface
3232
{
3333
use ServiceLocatorTrait {
3434
get as private doGet;
@@ -82,6 +82,13 @@ public function count(): int
8282
return \count($this->getProvidedServices());
8383
}
8484

85+
public function getIterator(): \Traversable
86+
{
87+
foreach ($this->getProvidedServices() as $id => $config) {
88+
yield $id => $this->get($id);
89+
}
90+
}
91+
8592
private function createNotFoundException(string $id): NotFoundExceptionInterface
8693
{
8794
if ($this->loading) {

Tests/ServiceLocatorTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,17 @@ public function testProvidesServicesInformation()
101101
'baz' => '?string',
102102
]);
103103
}
104+
105+
public function testIsCountableAndIterable()
106+
{
107+
$locator = $this->getServiceLocator([
108+
'foo' => fn () => 'bar',
109+
'bar' => fn () => 'baz',
110+
]);
111+
112+
$this->assertCount(2, $locator);
113+
$this->assertSame(['foo' => 'bar', 'bar' => 'baz'], iterator_to_array($locator));
114+
}
104115
}
105116

106117
class SomeServiceSubscriber implements ServiceSubscriberInterface

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"php": ">=8.2",
2020
"psr/container": "^1.1|^2.0",
2121
"symfony/deprecation-contracts": "^2.5|^3",
22-
"symfony/service-contracts": "^3.3",
22+
"symfony/service-contracts": "^3.5",
2323
"symfony/var-exporter": "^6.4|^7.0"
2424
},
2525
"require-dev": {

0 commit comments

Comments
 (0)