File tree Expand file tree Collapse file tree 3 files changed +61
-1
lines changed Expand file tree Collapse file tree 3 files changed +61
-1
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ CHANGELOG
6
6
7
7
* added ` HttpClient ` namespace with contracts for implementing flexible HTTP clients
8
8
* added ` EventDispatcher\EventDispatcherInterface `
9
+ * added ` ServiceProviderInterface `
9
10
10
11
1.0.0
11
12
-----
Original file line number Diff line number Diff line change 15
15
use Psr \Container \NotFoundExceptionInterface ;
16
16
17
17
/**
18
- * A trait to help implement PSR-11 service locators .
18
+ * A trait to help implement ServiceProviderInterface .
19
19
*
20
20
* @author Robin Chalas <robin.chalas@gmail.com>
21
21
* @author Nicolas Grekas <p@tchwork.com>
@@ -24,6 +24,7 @@ trait ServiceLocatorTrait
24
24
{
25
25
private $ factories ;
26
26
private $ loading = [];
27
+ private $ providedTypes ;
27
28
28
29
/**
29
30
* @param callable[] $factories
@@ -66,6 +67,28 @@ public function get($id)
66
67
}
67
68
}
68
69
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
+
69
92
private function createNotFoundException (string $ id ): NotFoundExceptionInterface
70
93
{
71
94
if (!$ alternatives = array_keys ($ this ->factories )) {
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments