Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Contracts][DependencyInjection] Add ServiceCollectionInterface #53163

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 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"psr/http-message": "^1.0|^2.0",
"psr/link": "^1.1|^2.0",
"psr/log": "^1|^2|^3",
"symfony/contracts": "^2.5|^3.0",
"symfony/contracts": "^3.5",
"symfony/polyfill-ctype": "~1.8",
"symfony/polyfill-intl-grapheme": "~1.0",
"symfony/polyfill-intl-icu": "~1.0",
Expand Down Expand Up @@ -206,7 +206,7 @@
"url": "src/Symfony/Contracts",
"options": {
"versions": {
"symfony/contracts": "3.4.x-dev"
"symfony/contracts": "3.5.x-dev"
}
}
},
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
---

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

7.0
---
Expand Down
13 changes: 10 additions & 3 deletions src/Symfony/Component/DependencyInjection/ServiceLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Contracts\Service\ServiceCollectionInterface;
use Symfony\Contracts\Service\ServiceLocatorTrait;
use Symfony\Contracts\Service\ServiceProviderInterface;
use Symfony\Contracts\Service\ServiceSubscriberInterface;

/**
Expand All @@ -26,9 +26,9 @@
*
* @template-covariant T of mixed
*
* @implements ServiceProviderInterface<T>
* @implements ServiceCollectionInterface<T>
*/
class ServiceLocator implements ServiceProviderInterface, \Countable
class ServiceLocator implements ServiceCollectionInterface
{
use ServiceLocatorTrait {
get as private doGet;
Expand Down Expand Up @@ -82,6 +82,13 @@ public function count(): int
return \count($this->getProvidedServices());
}

public function getIterator(): \Traversable
{
foreach ($this->getProvidedServices() as $id => $config) {
yield $id => $this->get($id);
}
}

private function createNotFoundException(string $id): NotFoundExceptionInterface
{
if ($this->loading) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,17 @@ public function testProvidesServicesInformation()
'baz' => '?string',
]);
}

public function testIsCountableAndIterable()
{
$locator = $this->getServiceLocator([
'foo' => fn () => 'bar',
'bar' => fn () => 'baz',
]);

$this->assertCount(2, $locator);
$this->assertSame(['foo' => 'bar', 'bar' => 'baz'], iterator_to_array($locator));
}
}

class SomeServiceSubscriber implements ServiceSubscriberInterface
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/DependencyInjection/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"php": ">=8.2",
"psr/container": "^1.1|^2.0",
"symfony/deprecation-contracts": "^2.5|^3",
"symfony/service-contracts": "^3.3",
"symfony/service-contracts": "^3.5",
"symfony/var-exporter": "^6.4|^7.0"
},
"require-dev": {
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Contracts/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

3.5
---

* Add `ServiceCollectionInterface`

3.4
---

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Contracts/Cache/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-main": "3.4-dev"
"dev-main": "3.5-dev"
},
"thanks": {
"name": "symfony/contracts",
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Contracts/Deprecation/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-main": "3.4-dev"
"dev-main": "3.5-dev"
},
"thanks": {
"name": "symfony/contracts",
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Contracts/EventDispatcher/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-main": "3.4-dev"
"dev-main": "3.5-dev"
},
"thanks": {
"name": "symfony/contracts",
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Contracts/HttpClient/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-main": "3.4-dev"
"dev-main": "3.5-dev"
},
"thanks": {
"name": "symfony/contracts",
Expand Down
26 changes: 26 additions & 0 deletions src/Symfony/Contracts/Service/ServiceCollectionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Contracts\Service;

/**
* A ServiceProviderInterface that is also countable and iterable.
*
* @author Kevin Bond <[email protected]>
*
* @template-covariant T of mixed
*
* @extends ServiceProviderInterface<T>
* @extends \IteratorAggregate<string, T>
*/
interface ServiceCollectionInterface extends ServiceProviderInterface, \Countable, \IteratorAggregate
{
}
2 changes: 1 addition & 1 deletion src/Symfony/Contracts/Service/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-main": "3.4-dev"
"dev-main": "3.5-dev"
},
"thanks": {
"name": "symfony/contracts",
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Contracts/Translation/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-main": "3.4-dev"
"dev-main": "3.5-dev"
},
"thanks": {
"name": "symfony/contracts",
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Contracts/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"minimum-stability": "dev",
"extra": {
"branch-alias": {
"dev-main": "3.4-dev"
"dev-main": "3.5-dev"
}
}
}