Skip to content

Commit

Permalink
Allow custom route loaders in combination with SlugActionInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristof Jochmans committed Jul 10, 2015
1 parent 34217ae commit 1af085b
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions src/Kunstmaan/NodeBundle/EventListener/SlugListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,16 @@
namespace Kunstmaan\NodeBundle\EventListener;

use Kunstmaan\NodeBundle\Controller\SlugActionInterface;
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
use Kunstmaan\NodeBundle\Event\Events;
use Kunstmaan\NodeBundle\Event\SlugSecurityEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpKernel\Controller\ControllerResolver;


/**
* Class SlugListener
* @package Kunstmaan\NodeBundle\EventListener
*/
class SlugListener
{

/**
* @var EntityManager
*/
Expand All @@ -28,7 +23,6 @@ class SlugListener
*/
protected $resolver;


/**
* @var EventDispatcher
*/
Expand All @@ -53,23 +47,27 @@ public function onKernelController(FilterControllerEvent $event)
{
$request = $event->getRequest();

//check if the event has anything to do with nodeTranslations, if not this method can be skipped
// Check if the event has anything to do with nodeTranslations, if not this method can be skipped
if (!$request->attributes->has('_nodeTranslation')) {
return;
}

$nodeTranslation = $request->attributes->get('_nodeTranslation');
if (!($nodeTranslation instanceof NodeTranslation)) {
$nodeTranslation = $this->em->getRepository('KunstmaanNodeBundle:NodeTranslation')->find($nodeTranslation);
$request->attributes->set('_nodeTranslation', $nodeTranslation);
}
$entity = $nodeTranslation->getRef($this->em);

//if the entity is an instance of the SlugControllerActionInterface, change the controller
// If the entity is an instance of the SlugControllerActionInterface, change the controller
if ($entity instanceof SlugActionInterface) {
$request->attributes->set('_entity', $entity);

//do security check by firing an event that gets handled by the SlugSecurityListener
// Do security check by firing an event that gets handled by the SlugSecurityListener
$securityEvent = new SlugSecurityEvent();
$this->eventDispatcher->dispatch(Events::SLUG_SECURITY, $securityEvent);

//set the right controller
// Set the right controller
$request->attributes->set('_controller', $entity->getControllerAction());
$event->setController($this->resolver->getController($request));
}
Expand Down

0 comments on commit 1af085b

Please sign in to comment.