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

Skip to content

Commit 422ce7f

Browse files
[DI] leverage Contracts\Service
1 parent 4cd386b commit 422ce7f

14 files changed

+47
-97
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
5050
use Symfony\Component\DependencyInjection\Reference;
5151
use Symfony\Component\DependencyInjection\ServiceLocator;
52-
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
5352
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
5453
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
5554
use Symfony\Component\Finder\Finder;
@@ -98,6 +97,7 @@
9897
use Symfony\Component\Yaml\Command\LintCommand as BaseYamlLintCommand;
9998
use Symfony\Component\Yaml\Yaml;
10099
use Symfony\Contracts\Service\ResetInterface;
100+
use Symfony\Contracts\Service\ServiceSubscriberInterface;
101101

102102
/**
103103
* FrameworkExtension.

src/Symfony/Bundle/FrameworkBundle/Test/KernelTestCase.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\DependencyInjection\ContainerInterface;
16-
use Symfony\Component\DependencyInjection\ResettableContainerInterface;
1716
use Symfony\Component\HttpKernel\KernelInterface;
17+
use Symfony\Contracts\Service\ResetInterface;
1818

1919
/**
2020
* KernelTestCase is the base class for tests needing a Kernel.
@@ -119,7 +119,7 @@ protected static function ensureKernelShutdown()
119119
if (null !== static::$kernel) {
120120
$container = static::$kernel->getContainer();
121121
static::$kernel->shutdown();
122-
if ($container instanceof ResettableContainerInterface) {
122+
if ($container instanceof ResetInterface) {
123123
$container->reset();
124124
}
125125
}

src/Symfony/Component/Config/Resource/ReflectionClassResource.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111

1212
namespace Symfony\Component\Config\Resource;
1313

14-
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
14+
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface as LegacyServiceSubscriberInterface;
1515
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16+
use Symfony\Contracts\Service\ServiceSubscriberInterface;
1617

1718
/**
1819
* @author Nicolas Grekas <[email protected]>
@@ -157,7 +158,10 @@ private function generateSignature(\ReflectionClass $class)
157158
yield print_r(\call_user_func(array($class->name, 'getSubscribedEvents')), true);
158159
}
159160

160-
if (interface_exists(ServiceSubscriberInterface::class, false) && $class->isSubclassOf(ServiceSubscriberInterface::class)) {
161+
if (interface_exists(LegacyServiceSubscriberInterface::class, false) && $class->isSubclassOf(LegacyServiceSubscriberInterface::class)) {
162+
yield LegacyServiceSubscriberInterface::class;
163+
yield print_r(\call_user_func(array($class->name, 'getSubscribedServices')), true);
164+
} elseif (interface_exists(ServiceSubscriberInterface::class, false) && $class->isSubclassOf(ServiceSubscriberInterface::class)) {
161165
yield ServiceSubscriberInterface::class;
162166
yield print_r(\call_user_func(array($class->name, 'getSubscribedServices')), true);
163167
}

src/Symfony/Component/DependencyInjection/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 `ServiceLocatorArgument` for creating optimized service-locators
8+
* deprecated `ServiceSubscriberInterface`, use the same interface from the `Symfony\Contracts\Service` namespace instead
89

910
4.1.0
1011
-----

src/Symfony/Component/DependencyInjection/Compiler/RegisterServiceSubscribersPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
use Symfony\Component\DependencyInjection\Definition;
1616
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
1717
use Symfony\Component\DependencyInjection\Reference;
18-
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
1918
use Symfony\Component\DependencyInjection\TypedReference;
19+
use Symfony\Contracts\Service\ServiceSubscriberInterface;
2020

2121
/**
2222
* Compiler pass to register tagged services that require a service locator.

src/Symfony/Component/DependencyInjection/ResettableContainerInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
* not needed anymore.
2020
*
2121
* @author Christophe Coevoet <[email protected]>
22+
*
23+
* @deprecated since Symfony 4.2, use `ResetInterface` instead.
2224
*/
2325
interface ResettableContainerInterface extends ContainerInterface, ResetInterface
2426
{

src/Symfony/Component/DependencyInjection/ServiceLocator.php

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,62 +11,37 @@
1111

1212
namespace Symfony\Component\DependencyInjection;
1313

14+
use Psr\Container\ContainerExceptionInterface;
1415
use Psr\Container\ContainerInterface as PsrContainerInterface;
16+
use Psr\Container\NotFoundExceptionInterface;
1517
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
1618
use Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException;
1719
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
20+
use Symfony\Contracts\Service\ServiceLocatorTrait;
21+
use Symfony\Contracts\Service\ServiceSubscriberInterface;
1822

1923
/**
2024
* @author Robin Chalas <[email protected]>
2125
* @author Nicolas Grekas <[email protected]>
2226
*/
2327
class ServiceLocator implements PsrContainerInterface
2428
{
25-
private $factories;
26-
private $loading = array();
27-
private $externalId;
28-
private $container;
29-
30-
/**
31-
* @param callable[] $factories
32-
*/
33-
public function __construct(array $factories)
34-
{
35-
$this->factories = $factories;
29+
use ServiceLocatorTrait {
30+
get as doGet;
3631
}
3732

38-
/**
39-
* {@inheritdoc}
40-
*/
41-
public function has($id)
42-
{
43-
return isset($this->factories[$id]);
44-
}
33+
private $externalId;
34+
private $container;
4535

46-
/**
47-
* {@inheritdoc}
48-
*/
4936
public function get($id)
5037
{
51-
if (!isset($this->factories[$id])) {
52-
throw new ServiceNotFoundException($id, end($this->loading) ?: null, null, array(), $this->createServiceNotFoundMessage($id));
53-
}
54-
55-
if (isset($this->loading[$id])) {
56-
$ids = array_values($this->loading);
57-
$ids = \array_slice($this->loading, array_search($id, $ids));
58-
$ids[] = $id;
59-
60-
throw new ServiceCircularReferenceException($id, $ids);
38+
if (!$this->externalId) {
39+
return $this->doGet($id);
6140
}
6241

63-
$this->loading[$id] = $id;
6442
try {
65-
return $this->factories[$id]();
43+
return $this->doGet($id);
6644
} catch (RuntimeException $e) {
67-
if (!$this->externalId) {
68-
throw $e;
69-
}
7045
$what = sprintf('service "%s" required by "%s"', $id, $this->externalId);
7146
$message = preg_replace('/service "\.service_locator\.[^"]++"/', $what, $e->getMessage());
7247

@@ -79,8 +54,6 @@ public function get($id)
7954
$r->setValue($e, $message);
8055

8156
throw $e;
82-
} finally {
83-
unset($this->loading[$id]);
8457
}
8558
}
8659

@@ -101,14 +74,16 @@ public function withContext($externalId, Container $container)
10174
return $locator;
10275
}
10376

104-
private function createServiceNotFoundMessage($id)
77+
private function createNotFoundException(string $id): NotFoundExceptionInterface
10578
{
10679
if ($this->loading) {
107-
return sprintf('The service "%s" has a dependency on a non-existent service "%s". This locator %s', end($this->loading), $id, $this->formatAlternatives());
80+
$msg = sprintf('The service "%s" has a dependency on a non-existent service "%s". This locator %s', end($this->loading), $id, $this->formatAlternatives());
81+
82+
return new ServiceNotFoundException($id, end($this->loading) ?: null, null, array(), $msg);
10883
}
10984

110-
$class = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS, 3);
111-
$class = isset($class[2]['object']) ? \get_class($class[2]['object']) : null;
85+
$class = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS, 4);
86+
$class = isset($class[3]['object']) ? \get_class($class[3]['object']) : null;
11287
$externalId = $this->externalId ?: $class;
11388

11489
$msg = sprintf('Service "%s" not found: ', $id);
@@ -143,7 +118,12 @@ private function createServiceNotFoundMessage($id)
143118
$msg .= 'Try using dependency injection instead.';
144119
}
145120

146-
return $msg;
121+
return new ServiceNotFoundException($id, end($this->loading) ?: null, null, array(), $msg);
122+
}
123+
124+
private function createCircularReferenceException(string $id, array $path): ContainerExceptionInterface
125+
{
126+
return new ServiceCircularReferenceException($id, $path);
147127
}
148128

149129
private function formatAlternatives(array $alternatives = null, $separator = 'and')

src/Symfony/Component/DependencyInjection/ServiceSubscriberInterface.php

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,13 @@
1111

1212
namespace Symfony\Component\DependencyInjection;
1313

14+
use Symfony\Contracts\Service\ServiceSubscriberInterface as BaseServiceSubscriberInterface;
15+
1416
/**
15-
* A ServiceSubscriber exposes its dependencies via the static {@link getSubscribedServices} method.
16-
*
17-
* The getSubscribedServices method returns an array of service types required by such instances,
18-
* optionally keyed by the service names used internally. Service types that start with an interrogation
19-
* mark "?" are optional, while the other ones are mandatory service dependencies.
20-
*
21-
* The injected service locators SHOULD NOT allow access to any other services not specified by the method.
22-
*
23-
* It is expected that ServiceSubscriber instances consume PSR-11-based service locators internally.
24-
* This interface does not dictate any injection method for these service locators, although constructor
25-
* injection is recommended.
17+
* {@inheritdoc}
2618
*
27-
* @author Nicolas Grekas <[email protected]>
19+
* @deprecated since Symfony 4.2, use Symfony\Contracts\Service\ServiceSubscriberInterface instead.
2820
*/
29-
interface ServiceSubscriberInterface
21+
interface ServiceSubscriberInterface extends BaseServiceSubscriberInterface
3022
{
31-
/**
32-
* Returns an array of service types required by such instances, optionally keyed by the service names used internally.
33-
*
34-
* For mandatory dependencies:
35-
*
36-
* * array('logger' => 'Psr\Log\LoggerInterface') means the objects use the "logger" name
37-
* internally to fetch a service which must implement Psr\Log\LoggerInterface.
38-
* * array('Psr\Log\LoggerInterface') is a shortcut for
39-
* * array('Psr\Log\LoggerInterface' => 'Psr\Log\LoggerInterface')
40-
*
41-
* otherwise:
42-
*
43-
* * array('logger' => '?Psr\Log\LoggerInterface') denotes an optional dependency
44-
* * array('?Psr\Log\LoggerInterface') is a shortcut for
45-
* * array('Psr\Log\LoggerInterface' => '?Psr\Log\LoggerInterface')
46-
*
47-
* @return array The required service types, optionally keyed by service names
48-
*/
49-
public static function getSubscribedServices();
5023
}

src/Symfony/Component/DependencyInjection/Tests/Compiler/RegisterServiceSubscribersPassTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class RegisterServiceSubscribersPassTest extends TestCase
3535
{
3636
/**
3737
* @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException
38-
* @expectedExceptionMessage Service "foo" must implement interface "Symfony\Component\DependencyInjection\ServiceSubscriberInterface".
38+
* @expectedExceptionMessage Service "foo" must implement interface "Symfony\Contracts\Service\ServiceSubscriberInterface".
3939
*/
4040
public function testInvalidClass()
4141
{

src/Symfony/Component/DependencyInjection/Tests/Fixtures/TestServiceSubscriber.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
44

5-
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
5+
use Symfony\Contracts\Service\ServiceSubscriberInterface;
66

77
class TestServiceSubscriber implements ServiceSubscriberInterface
88
{

src/Symfony/Component/DependencyInjection/Tests/Fixtures/TestServiceSubscriberParent.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
44

5-
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
5+
use Symfony\Contracts\Service\ServiceSubscriberInterface;
66
use Symfony\Contracts\Service\ServiceSubscriberTrait;
77

88
class TestServiceSubscriberParent implements ServiceSubscriberInterface

src/Symfony/Component/DependencyInjection/Tests/Fixtures/includes/classes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use Symfony\Component\DependencyInjection\Definition;
44
use Symfony\Component\DependencyInjection\LazyProxy\PhpDumper\DumperInterface as ProxyDumper;
5-
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
5+
use Symfony\Contracts\Service\ServiceSubscriberInterface;
66

77
function sc_configure($instance)
88
{

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Symfony\Component\DependencyInjection\Container;
1616
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
1717
use Symfony\Component\DependencyInjection\ServiceLocator;
18-
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
18+
use Symfony\Contracts\Service\ServiceSubscriberInterface;
1919

2020
class ServiceLocatorTest extends TestCase
2121
{
@@ -143,7 +143,7 @@ public function testRuntimeException()
143143
}
144144
}
145145

146-
class SomeServiceSubscriber implements ServiceSubscriberinterface
146+
class SomeServiceSubscriber implements ServiceSubscriberInterface
147147
{
148148
public $container;
149149

src/Symfony/Component/HttpKernel/Tests/EventListener/TestSessionListenerTest.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace Symfony\Component\HttpKernel\Tests\EventListener;
1313

1414
use PHPUnit\Framework\TestCase;
15-
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
1615
use Symfony\Component\HttpFoundation\Request;
1716
use Symfony\Component\HttpFoundation\Response;
1817
use Symfony\Component\HttpFoundation\Session\SessionInterface;
@@ -144,15 +143,6 @@ public function testUnstartedSessionIsNotSave()
144143
$this->filterResponse(new Request());
145144
}
146145

147-
public function testDoesNotImplementServiceSubscriberInterface()
148-
{
149-
$this->assertTrue(interface_exists(ServiceSubscriberInterface::class));
150-
$this->assertTrue(class_exists(SessionListener::class));
151-
$this->assertTrue(class_exists(TestSessionListener::class));
152-
$this->assertFalse(is_subclass_of(SessionListener::class, ServiceSubscriberInterface::class), 'Implementing ServiceSubscriberInterface would create a dep on the DI component, which eg Silex cannot afford');
153-
$this->assertFalse(is_subclass_of(TestSessionListener::class, ServiceSubscriberInterface::class, 'Implementing ServiceSubscriberInterface would create a dep on the DI component, which eg Silex cannot afford'));
154-
}
155-
156146
public function testDoesNotThrowIfRequestDoesNotHaveASession()
157147
{
158148
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')->getMock();

0 commit comments

Comments
 (0)