8000 [DI][Contracts] add and implement ServiceProviderInterface · symfony/symfony@77e29d6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 77e29d6

Browse files
mateuszsipnicolas-grekas
authored andcommitted
[DI][Contracts] add and implement ServiceProviderInterface
1 parent d4326b2 commit 77e29d6

File tree

6 files changed

+73
-3
lines changed

6 files changed

+73
-3
lines changed

src/Symfony/Component/DependencyInjection/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ CHANGELOG
2222
* added `%env(key:...)%` processor to fetch a specific key from an array
2323
* deprecated `ServiceSubscriberInterface`, use the same interface from the `Symfony\Contracts\Service` namespace instead
2424
* deprecated `ResettableContainerInterface`, use `Symfony\Contracts\Service\ResetInterface` instead
25+
* `ServiceLocator` implements `ServiceProviderInterface`
2526

2627
4.1.0
2728
-----

src/Symfony/Component/DependencyInjection/ServiceLocator.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,27 @@
1212
namespace Symfony\Component\DependencyInjection;
1313

1414
use Psr\Container\ContainerExceptionInterface;
15-
use Psr\Container\ContainerInterface as PsrContainerInterface;
1615
use Psr\Container\NotFoundExceptionInterface;
1716
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
1817
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
1918
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
2019
use Symfony\Contracts\Service\ServiceLocatorTrait;
20+
use Symfony\Contracts\Service\ServiceProviderInterface;
2121
use Symfony\Contracts\Service\ServiceSubscriberInterface;
2222

2323
/**
2424
* @author Robin Chalas <robin.chalas@gmail.com>
2525
* @author Nicolas Grekas <p@tchwork.com>
2626
*/
27-
class ServiceLocator implements PsrContainerInterface
27+
class ServiceLocator implements ServiceProviderInterface
2828
{
2929
use ServiceLocatorTrait {
3030
get as private doGet;
3131
}
3232

3333
private $externalId;
3434
private $container;
35+
private $providedTypes;
3536

3637
public function get($id)
3738
{
@@ -140,4 +141,19 @@ private function formatAlternatives(array $alternatives = null, $separator = 'an
140141

141142
return sprintf($format, $alternatives ? implode('", "', $alternatives) : $last, $alternatives ? sprintf(' %s "%s"', $separator, $last) : '');
142143
}
144+
145+
public function getProvidedServices(): array
146+
{
147+
if (null === $this->providedTypes) {
148+
$this->providedTypes = array();
149+
150+
foreach ($this->factories as $name => $factory) {
151+
$type = (new \ReflectionFunction($factory))->getReturnType();
152+
153+
$this->providedTypes[$name] = $type ? ($type->allowsNull() ? '?' : '').$type->getName() : '?';
154+
}
155+
}
156+
157+
return $this->providedTypes;
158+
}
143159
}

src/Symfony/Component/DependencyInjection/Tests/ServiceLocatorTest.php

Lines changed: 15 additions & 0 deletions
< 8000 td data-grid-cell-id="diff-71b3c743c45bf3fbb598f65a62de8628816e0a7734a2c1a96150554c10baed9f-88-88-2" data-line-anchor="diff-71b3c743c45bf3fbb598f65a62de8628816e0a7734a2c1a96150554c10baed9fR88" data-selected="false" role="gridcell" style="background-color:var(--bgColor-default);padding-right:24px" tabindex="-1" valign="top" class="focusable-grid-cell diff-text-cell right-side-diff-cell left-side">
}
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,21 @@ public function testInvoke()
8686
$this->assertSame('baz', $locator('bar'));
8787
$this->assertNull($locator('dummy'), '->__invoke() should return null on invalid service');
8888
89+
90+
public function testProvidesServicesInformation()
91+
{
92+
$locator = new ServiceLocator(array(
93+
'foo' => function () { return 'bar'; },
94+
'bar' => function (): string { return 'baz'; },
95+
'baz' => function (): ?string { return 'zaz'; },
96+
));
97+
98+
$this->assertSame($locator->getProvidedServices(), array(
99+
'foo' => '?',
100+
'bar' => 'string',
101+
'baz' => '?string',
102+
));
103+
}
89104
}
90105

91106
class SomeServiceSubscriber implements ServiceSubscriberInterface

src/Symfony/Component/DependencyInjection/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"require": {
1919
"php": "^7.1.3",
2020
"psr/container": "^1.0",
21-
"symfony/contracts": "^1.0"
21+
"symfony/contracts": "^1.1"
2222
},
2323
"require-dev": {
2424
"symfony/yaml": "~3.4|~4.0",

src/Symfony/Contracts/CHANGELOG.md

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

77
* added `HttpClient` namespace with contracts for implementing flexible HTTP clients
8+
* added `ServiceProviderInterface`
89

910
1.0.0
1011
-----
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 identifiers and types of services provided by a container.
18+
*
19 98D9 +
* @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 names provided by this object.
26+
*
27+
* Examples:
28+
*
29+
* * array('logger' => 'Psr\Log\LoggerInterface') means the object provides service implementing Psr\Log\LoggerInterface
30+
* under "logger" name
31+
* * array('foo' => '?') means that object provides service of unknown type under 'foo' name
32+
* * array('bar' => '?Bar\Baz') means that object provides service implementing Bar\Baz or null under 'bar' name
33+
*
34+
* @return string[] The provided service types, keyed by service names
35+
*/
36+
public function getProvidedServices(): array;
37+
}

0 commit comments

Comments
 (0)
0