Skip to content

Commit

Permalink
Merge pull request #44 from krizon/support-invokable-controllers
Browse files Browse the repository at this point in the history
Support invokable controllers in annotation toggle listener
  • Loading branch information
wjzijderveld authored Jul 24, 2017
2 parents 459cee3 + deec1d0 commit d15c4a2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
12 changes: 9 additions & 3 deletions EventListener/ToggleListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@ public function __construct(Reader $reader, ToggleManager $toggleManager, Contex
public function onKernelController(FilterControllerEvent $event)
{
$controller = $event->getController();
$class = ClassUtils::getClass($controller[0]);
$object = new \ReflectionClass($class);
$method = $object->getMethod($controller[1]);

if (is_array($controller)) {
$class = ClassUtils::getClass($controller[0]);
$object = new \ReflectionClass($class);
$method = $object->getMethod($controller[1]);
} else {
$object = new \ReflectionClass($controller);
$method = $object->getMethod('__invoke');
}

foreach ($this->reader->getClassAnnotations($object) as $annotation) {
if ($annotation instanceof Toggle) {
Expand Down
16 changes: 16 additions & 0 deletions Tests/EventListener/Fixture/FooControllerToggleAtInvoke.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Qandidate\Bundle\ToggleBundle\Tests\EventListener\Fixture;

use Qandidate\Bundle\ToggleBundle\Annotations\Toggle;

class FooControllerToggleAtInvoke
{
/**
* @Toggle("cool-feature-on-invoke")
*/
public function __invoke()
{
return 'method.executed';
}
}
24 changes: 24 additions & 0 deletions Tests/EventListener/ToggleListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Qandidate\Bundle\ToggleBundle\Tests\EventListener\Fixture\FooControllerToggleAtClassAndMethod;

use Doctrine\Common\Annotations\AnnotationReader;
use Qandidate\Bundle\ToggleBundle\Tests\EventListener\Fixture\FooControllerToggleAtInvoke;
use Qandidate\Toggle\Context;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
Expand Down Expand Up @@ -61,6 +62,29 @@ public function testActiveToggleAnnotationAtMethod()
$this->assertTrue(true);
}

/**
* @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
public function testInactiveToggleAnnotationAtInvoke()
{
$this->listener = $this->createListener(false);
$controller = new FooControllerToggleAtInvoke();

$this->event = $this->getFilterControllerEvent($controller, $this->request);
$this->listener->onKernelController($this->event);
}

public function testActiveToggleAnnotationAtInvoke()
{
$this->listener = $this->createListener(true);
$controller = new FooControllerToggleAtInvoke();

$this->event = $this->getFilterControllerEvent($controller, $this->request);
$this->listener->onKernelController($this->event);
// If we end up here toggle is active, no exception thrown
$this->assertTrue(true);
}

protected function createToggleManager($isToggleActive)
{
$toggleManager = $this->getMockBuilder('Qandidate\Toggle\ToggleManager')
Expand Down

0 comments on commit d15c4a2

Please sign in to comment.