Skip to content
This repository has been archived by the owner on Aug 22, 2023. It is now read-only.

Commit

Permalink
Allow access to the feature from the event
Browse files Browse the repository at this point in the history
  • Loading branch information
ajgarlag committed Dec 9, 2020
1 parent 197e86b commit d5cf6be
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 13 deletions.
15 changes: 14 additions & 1 deletion src/Event/ContextResolveEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,31 @@
*/
class ContextResolveEvent extends Event
{
/**
* The feature
*
* @var string
*/
private $feature;

/**
* The context
*
* @var Context
*/
private $context;

public function __construct(Context $context = null)
public function __construct(string $feature, Context $context = null)
{
$this->feature = $feature;
$this->context = $context ?? new Context();
}

public function getFeature(): string
{
return $this->feature;
}

public function getContext(): Context
{
return $this->context;
Expand Down
17 changes: 12 additions & 5 deletions src/Listener/AnnotationSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,17 @@ public function onKernelController(ControllerEvent $event)
return;
}

$context = null;
if (null !== $this->eventDispatcher) {
$contextEvent = $this->eventDispatcher->dispatch(new ContextResolveEvent());
$context = $contextEvent->getContext();
}


$object = new ReflectionClass($controller[0]);
foreach ($this->reader->getClassAnnotations($object) as $annotation) {
if ($annotation instanceof Feature) {
$context = null;
if (null !== $this->eventDispatcher) {
$contextEvent = $this->eventDispatcher->dispatch(new ContextResolveEvent($annotation->name));
$context = $contextEvent->getContext();
}

if (!$this->manager->isActive($annotation->name, $context)) {
throw new NotFoundHttpException('Feature for this class is not active.');
}
Expand All @@ -104,6 +106,11 @@ public function onKernelController(ControllerEvent $event)

$method = $object->getMethod($controller[1]);
foreach ($this->reader->getMethodAnnotations($method) as $annotation) {
$context = null;
if (null !== $this->eventDispatcher) {
$contextEvent = $this->eventDispatcher->dispatch(new ContextResolveEvent($annotation->name));
$context = $contextEvent->getContext();
}
if ($annotation instanceof Feature) {
if (!$this->manager->isActive($annotation->name, $context)) {
throw new NotFoundHttpException('Feature for this method is not active.');
Expand Down
11 changes: 5 additions & 6 deletions src/Listener/RoutingMetadataSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,13 @@ public function onKernelController(ControllerEvent $event)
return;
}

$context = null;
if (null !== $this->eventDispatcher) {
$contextEvent = $this->eventDispatcher->dispatch(new ContextResolveEvent());
$context = $contextEvent->getContext();
}

$featureNames = (array) $event->getRequest()->attributes->get(static::FEATURE_KEY);
foreach ($featureNames as $featureName) {
$context = null;
if (null !== $this->eventDispatcher) {
$contextEvent = $this->eventDispatcher->dispatch(new ContextResolveEvent($featureName));
$context = $contextEvent->getContext();
}
if (!$this->manager->isActive($featureName, $context)) {
throw new NotFoundHttpException('Feature for this class is not active.');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Twig/ToggleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function isActive($name, array $contextValues = [])
}

if (null !== $this->eventDispatcher) {
$contextEvent = $this->eventDispatcher->dispatch(new ContextResolveEvent($context));
$contextEvent = $this->eventDispatcher->dispatch(new ContextResolveEvent($name, $context));
$context = $contextEvent->getContext();
}

Expand Down
1 change: 1 addition & 0 deletions tests/Listener/AnnotationSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ public function testEventIsDispatchedWhenEventDispatcherExists()
$manager->method('isActive')->with('feature_abc', $context)->willReturn(true);

$listener = function (ContextResolveEvent $event) use ($context) {
$this->assertSame('feature_abc', $event->getFeature());
$this->assertNotSame($event->getContext(), $context);
$event->setContext($context);
$this->assertSame($event->getContext(), $context);
Expand Down
1 change: 1 addition & 0 deletions tests/Listener/RoutingMetadataSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ public function testEventIsDispatchedWhenEventDispatcherExists()
->willReturn(true);

$listener = function (ContextResolveEvent $event) use ($context) {
$this->assertSame('feature_abc', $event->getFeature());
$this->assertNotSame($event->getContext(), $context);
$event->setContext($context);
$this->assertSame($event->getContext(), $context);
Expand Down
1 change: 1 addition & 0 deletions tests/Twig/ToggleExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public function testEventIsDispatchedWhenEventDispatcherExists()
$context = new Context();

$listener = function (ContextResolveEvent $event) use ($context) {
$this->assertSame('feature_foo', $event->getFeature());
$this->assertNotSame($event->getContext(), $context);
$event->setContext($context);
$this->assertSame($event->getContext(), $context);
Expand Down

0 comments on commit d5cf6be

Please sign in to comment.