Skip to content

Commit 8a186ff

Browse files
mateuszsipnicolas-grekas
authored andcommitted
[DI][Contracts] add and implement ServiceProviderInterface
1 parent 88c5a76 commit 8a186ff

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

CHANGELOG.md

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

77
* added `HttpClient` namespace with contracts for implementing flexible HTTP clients
88
* added `EventDispatcher\EventDispatcherInterface`
9+
* added `ServiceProviderInterface`
910

1011
1.0.0
1112
-----

Service/ServiceLocatorTrait.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Psr\Container\NotFoundExceptionInterface;
1616

1717
/**
18-
* A trait to help implement PSR-11 service locators.
18+
* A trait to help implement ServiceProviderInterface.
1919
*
2020
* @author Robin Chalas <robin.chalas@gmail.com>
2121
* @author Nicolas Grekas <p@tchwork.com>
@@ -24,6 +24,7 @@ trait ServiceLocatorTrait
2424
{
2525
private $factories;
2626
private $loading = [];
27+
private $providedTypes;
2728

2829
/**
2930
* @param callable[] $factories
@@ -66,6 +67,28 @@ public function get($id)
6667
}
6768
}
6869

70+
/**
71+
* {@inheritdoc}
72+
*/
73+
public function getProvidedServices(): array
74+
{
75+
if (null === $this->providedTypes) {
76+
$this->providedTypes = [];
77+
78+
foreach ($this->factories as $name => $factory) {
79+
if (!\is_callable($factory)) {
80+
$this->providedTypes[$name] = '?';
81+
} else {
82+
$type = (new \ReflectionFunction($factory))->getReturnType();
83+
84+
$this->providedTypes[$name] = $type ? ($type->allowsNull() ? '?' : '').$type->getName() : '?';
85+
}
86+
}
87+
}
88+
89+
return $this->providedTypes;
90+
}
91+
6992
private function createNotFoundException(string $id): NotFoundExceptionInterface
7093
{
7194
if (!$alternatives = array_keys($this->factories)) {

Service/ServiceProviderInterface.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Contracts\Service;
13+
14+
use Psr\Container\ContainerInterface;
15+
16+
/**
17+
* A ServiceProviderInterface exposes the identifiers and the types of services provided by a container.
18+
*
19+
* @author Nicolas Grekas <p@tchwork.com>
20+
* @author Mateusz Sip <mateusz.sip@gmail.com>
21+
*/
22+
interface ServiceProviderInterface extends ContainerInterface
23+
{
24+
/**
25+
* Returns an associative array of service types keyed by the identifiers provided by the current container.
26+
*
27+
* Examples:
28+
*
29+
* * ['logger' => 'Psr\Log\LoggerInterface'] means the object provides a service named "logger" that implements Psr\Log\LoggerInterface
30+
* * ['foo' => '?'] means the container provides service name "foo" of unspecified type
31+
* * ['bar' => '?Bar\Baz'] means the container provides a service "bar" of type Bar\Baz|null
32+
*
33+
* @return string[] The provided service types, keyed by service names
34+
*/
35+
public function getProvidedServices(): array;
36+
}

0 commit comments

Comments
 (0)