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

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[Config] Handle Service/EventSubscriberInterface in ReflectionClassRe…
…source
  • Loading branch information
nicolas-grekas committed Jan 31, 2018
commit 67e821b94f304a9f33ad91f492b0a6ebf2424843
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

namespace Symfony\Component\Config\Resource;

use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* @author Nicolas Grekas <[email protected]>
*/
Expand Down Expand Up @@ -114,7 +117,9 @@ private function computeHash()

private function generateSignature(\ReflectionClass $class)
{
yield $class->getDocComment().$class->getModifiers();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$class->getModifiers() can change value at runtime, e.g. on PHP 5.5

yield $class->getDocComment();
yield (int) $class->isFinal();
yield (int) $class->isAbstract();

if ($class->isTrait()) {
yield print_r(class_uses($class->name), true);
Expand Down Expand Up @@ -149,6 +154,16 @@ private function generateSignature(\ReflectionClass $class)
yield print_r($defaults, true);
}
}

if ($class->isSubclassOf(EventSubscriberInterface::class)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering whether this interface coming from another component should really be hardcoded here. This means that any other package providing a similar feature has no way to invalidate the container.

Could we make a reusable implementation (using a different resource probably), which would work for such cases ?

Note that I have such use case in my own project, and I will soon have it in KnpMenuBundle too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stof that's possible, but that's a new feature. Not the target here, but could be done in another PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, actually, other packages can invalidate the cache, but suffer from the issue of invalidating them too often too.

Can you work on creating such new feature for 4.1 though ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you work on creating such new feature for 4.1 though ?

not quite now, please open an issue :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done: #25984

yield EventSubscriberInterface::class;
yield print_r(\call_user_func(array($class->name, 'getSubscribedEvents')), true);
}

if ($class->isSubclassOf(ServiceSubscriberInterface::class)) {
yield ServiceSubscriberInterface::class;
yield print_r(\call_user_func(array($class->name, 'getSubscribedServices')), true);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Resource\ReflectionClassResource;
use Symfony\Component\DependencyInjection\ServiceSubscriberInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class ReflectionClassResourceTest extends TestCase
{
Expand Down Expand Up @@ -136,8 +138,52 @@ public function provideHashedSignature()
yield array(0, 14, '/** priv docblock */');
yield array(0, 15, '');
}

public function testEventSubscriber()
{
$res = new ReflectionClassResource(new \ReflectionClass(TestEventSubscriber::class));
$this->assertTrue($res->isFresh(0));

TestEventSubscriber::$subscribedEvents = array(123);
$this->assertFalse($res->isFresh(0));

$res = new ReflectionClassResource(new \ReflectionClass(TestEventSubscriber::class));
$this->assertTrue($res->isFresh(0));
}

public function testServiceSubscriber()
{
$res = new ReflectionClassResource(new \ReflectionClass(TestServiceSubscriber::class));
$this->assertTrue($res->isFresh(0));

TestServiceSubscriber::$subscribedServices = array(123);
$this->assertFalse($res->isFresh(0));

$res = new ReflectionClassResource(new \ReflectionClass(TestServiceSubscriber::class));
$this->assertTrue($res->isFresh(0));
}
}

interface DummyInterface
{
}

class TestEventSubscriber implements EventSubscriberInterface
{
public static $subscribedEvents = array();

public static function getSubscribedEvents()
{
return self::$subscribedEvents;
}
}

class TestServiceSubscriber implements ServiceSubscriberInterface
{
public static $subscribedServices = array();

public static function getSubscribedServices()
{
return self::$subscribedServices;
}
}
3 changes: 2 additions & 1 deletion src/Symfony/Component/Config/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"require-dev": {
"symfony/finder": "~3.3|~4.0",
"symfony/yaml": "~3.0|~4.0",
"symfony/dependency-injection": "~3.3|~4.0"
"symfony/dependency-injection": "~3.3|~4.0",
"symfony/event-dispatcher": "~3.3|~4.0"
},
"conflict": {
"symfony/finder": "<3.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ protected function processValue($value, $isRoot = false)
}
$class = $value->getClass();

if (!is_subclass_of($class, ServiceSubscriberInterface::class)) {
if (!class_exists($class, false)) {
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $this->currentId));
}

if (!$r = $this->container->getReflectionClass($class)) {
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $this->currentId));
}
if (!$r->isSubclassOf(ServiceSubscriberInterface::class)) {
throw new InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $this->currentId, ServiceSubscriberInterface::class));
}
$this->container->addObjectResource($class);
$class = $r->name;

$subscriberMap = array();
$declaringClass = (new \ReflectionMethod($class, 'getSubscribedServices'))->class;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,15 @@ public function process(ContainerBuilder $container)
$def = $container->getDefinition($id);

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

if (!is_subclass_of($class, $interface)) {
if (!class_exists($class, false)) {
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
}

throw new InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface));
if (!$r = $container->getReflectionClass($class)) {
throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $id));
}
if (!$r->isSubclassOf(EventSubscriberInterface::class)) {
throw new InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, EventSubscriberInterface::class));
}
$container->addObjectResource($class);
$class = $r->name;

ExtractingEventDispatcher::$subscriber = $class;
$extractingDispatcher->addSubscriber($extractingDispatcher);
Expand Down