vendor/easycorp/easyadmin-bundle/src/EventListener/RequestPostInitializeListener.php line 55

Open in your IDE?
  1. <?php
  2. namespace EasyCorp\Bundle\EasyAdminBundle\EventListener;
  3. use Doctrine\Bundle\DoctrineBundle\Registry;
  4. use EasyCorp\Bundle\EasyAdminBundle\Exception\EntityNotFoundException;
  5. use Symfony\Component\EventDispatcher\GenericEvent;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. /**
  9.  * Adds some custom attributes to the request object to store information
  10.  * related to EasyAdmin.
  11.  *
  12.  * @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
  13.  */
  14. class RequestPostInitializeListener
  15. {
  16.     /** @var Request|null */
  17.     private $request;
  18.     /** @var RequestStack|null */
  19.     private $requestStack;
  20.     /** @var Registry */
  21.     private $doctrine;
  22.     /**
  23.      * @param Registry          $doctrine
  24.      * @param RequestStack|null $requestStack
  25.      */
  26.     public function __construct(Registry $doctrineRequestStack $requestStack null)
  27.     {
  28.         $this->doctrine $doctrine;
  29.         $this->requestStack $requestStack;
  30.     }
  31.     /**
  32.      * BC for SF < 2.4.
  33.      * To be replaced by the usage of the request stack when 2.3 support is dropped.
  34.      *
  35.      * @param Request|null $request
  36.      */
  37.     public function setRequest(Request $request null)
  38.     {
  39.         $this->request $request;
  40.     }
  41.     /**
  42.      * Adds to the request some attributes with useful information, such as the
  43.      * current entity and the selected item, if any.
  44.      *
  45.      * @param GenericEvent $event
  46.      */
  47.     public function initializeRequest(GenericEvent $event)
  48.     {
  49.         if (null !== $this->requestStack) {
  50.             $this->request $this->requestStack->getCurrentRequest();
  51.         }
  52.         if (null === $this->request) {
  53.             return;
  54.         }
  55.         $this->request->attributes->set('easyadmin', array(
  56.             'entity' => $entity $event->getArgument('entity'),
  57.             'view' => $this->request->query->get('action''list'),
  58.             'item' => (null !== $id $this->request->query->get('id')) ? $this->findCurrentItem($entity$id) : null,
  59.         ));
  60.     }
  61.     /**
  62.      * Looks for the object that corresponds to the selected 'id' of the current entity.
  63.      *
  64.      * @param array $entityConfig
  65.      * @param mixed $itemId
  66.      *
  67.      * @return object The entity
  68.      *
  69.      * @throws EntityNotFoundException
  70.      */
  71.     private function findCurrentItem(array $entityConfig$itemId)
  72.     {
  73.         if (null === $manager $this->doctrine->getManagerForClass($entityConfig['class'])) {
  74.             throw new \RuntimeException(sprintf('There is no Doctrine Entity Manager defined for the "%s" class'$entityConfig['class']));
  75.         }
  76.         if (null === $entity $manager->getRepository($entityConfig['class'])->find($itemId)) {
  77.             throw new EntityNotFoundException(array('entity_name' => $entityConfig['name'], 'entity_id_name' => $entityConfig['primary_key_field_name'], 'entity_id_value' => $itemId));
  78.         }
  79.         return $entity;
  80.     }
  81. }
  82. class_alias('EasyCorp\Bundle\EasyAdminBundle\EventListener\RequestPostInitializeListener''JavierEguiluz\Bundle\EasyAdminBundle\EventListener\RequestPostInitializeListener'false);