Skip to content

Commit 628aab3

Browse files
[Config] Handle Service/EventSubscriberInterface in ReflectionClassResource
1 parent 0023f4e commit 628aab3

File tree

5 files changed

+73
-16
lines changed

5 files changed

+73
-16
lines changed

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

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

1212
namespace Symfony\Component\Config\Resource;
1313

14+
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
15+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
16+
1417
/**
1518
* @author Nicolas Grekas <p@tchwork.com>
1619
*/
@@ -149,6 +152,16 @@ private function generateSignature(\ReflectionClass $class)
149152
yield print_r($defaults, true);
150153
}
151154
}
155+
156+
if ($class->isSubclassOf(EventSubscriberInterface::class)) {
157+
yield EventSubscriberInterface::class;
158+
yield print_r(\call_user_func(array($class->name, 'getSubscribedEvents')), true);
159+
}
160+
161+
if ($class->isSubclassOf(ServiceSubscriberInterface::class)) {
162+
yield ServiceSubscriberInterface::class;
163+
yield print_r(\call_user_func(array($class->name, 'getSubscribedServices')), true);
164+
}
152165
}
153166
}
154167

src/Symfony/Component/Config/Tests/Resource/ReflectionClassResourceTest.php

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

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Config\Resource\ReflectionClassResource;
16+
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
17+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
1618

1719
class ReflectionClassResourceTest extends TestCase
1820
{
@@ -136,8 +138,53 @@ public function provideHashedSignature()
136138
yield array(0, 14, '/** priv docblock */');
137139
yield array(0, 15, '');
138140
}
141+
142+
public function testEventSubscriber()
143+
{
144+
$res = new ReflectionClassResource(new \ReflectionClass(TestEventSubscriber::class));
145+
$this->assertTrue($res->isFresh(0));
146+
147+
TestEventSubscriber::$subscribedEvents = array(123);
148+
$this->assertFalse($res->isFresh(0));
149+
150+
$res = new ReflectionClassResource(new \ReflectionClass(TestEventSubscriber::class));
151+
$this->assertTrue($res->isFresh(0));
152+
}
153+
154+
public function testServiceSubscriber()
155+
{
156+
$res = new ReflectionClassResource(new \ReflectionClass(TestServiceSubscriber::class));
157+
$this->assertTrue($res->isFresh(0));
158+
159+
TestServiceSubscriber::$subscribedServices = array(123);
160+
$this->assertFalse($res->isFresh(0));
161+
162+
$res = new ReflectionClassResource(new \ReflectionClass(TestServiceSubscriber::class));
163+
$this->assertTrue($res->isFresh(0));
164+
}
139165
}
140166

141167
interface DummyInterface
142168
{
143169
}
170+
171+
class TestEventSubscriber implements EventSubscriberInterface
172+
{
173+
public static $subscribedEvents = array();
174+
175+
public static function getSubscribedEvents()
176+
{
177+
return self::$subscribedEvents;
178+
}
179+
}
180+
181+
182+
class TestServiceSubscriber implements ServiceSubscriberInterface
183+
{
184+
public static $subscribedServices = array();
185+
186+
public static function getSubscribedServices()
187+
{
188+
return self::$subscribedServices;
189+
}
190+
}

src/Symfony/Component/Config/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"require-dev": {
2323
"symfony/finder": "~3.3|~4.0",
2424
"symfony/yaml": "~3.0|~4.0",
25-
"symfony/dependency-injection": "~3.3|~4.0"
25+
"symfony/dependency-injection": "~3.3|~4.0",
26+
"symfony/event-dispatcher": "~3.3|~4.0"
2627
},
2728
"conflict": {
2829
"symfony/finder": "<3.3",

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,13 @@ protected function processValue($value, $isRoot = false)
5656
}
5757
$class = $value->getClass();
5858

59-
if (!is_subclass_of($class, ServiceSubscriberInterface::class)) {
60-
if (!class_exists($class, false)) {
61-
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $this->currentId));
62-
}
63-
59+
if (!$r = $this->container->getReflectionClass($class)) {
60+
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $this->currentId));
61+
} elseif (!$r->isSubclassOf(ServiceSubscriberInterface::class)) {
6462
throw new InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $this->currentId, ServiceSubscriberInterface::class));
6563
}
66-
$this->container->addObjectResource($class);
64+
$class = $r->name;
65+
6766
$subscriberMap = array();
6867
$declaringClass = (new \ReflectionMethod($class, 'getSubscribedServices'))->class;
6968

src/Symfony/Component/EventDispatcher/DependencyInjection/RegisterListenersPass.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,17 +89,14 @@ public function process(ContainerBuilder $container)
8989
$def = $container->getDefinition($id);
9090

9191
// We must assume that the class value has been correctly filled, even if the service is created by a factory
92-
$class = $container->getParameterBag()->resolveValue($def->getClass());
93-
$interface = 'Symfony\Component\EventDispatcher\EventSubscriberInterface';
92+
$class = $def->getClass();
9493

95-
if (!is_subclass_of($class, $interface)) {
96-
if (!class_exists($class, false)) {
97-
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
98-
}
99-
100-
throw new InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface));
94+
if (!$r = $container->getReflectionClass($class)) {
95+
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
96+
} elseif (!$r->isSubclassOf(EventSubscriberInterface::class)) {
97+
throw new InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, EventSubscriberInterface::class));
10198
}
102-
$container->addObjectResource($class);
99+
$class = $r->name;
103100

104101
ExtractingEventDispatcher::$subscriber = $class;
105102
$extractingDispatcher->addSubscriber($extractingDispatcher);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy