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

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\RequestStack;
  7. /**
  8.  * Adds some custom attributes to the request object to store information
  9.  * related to EasyAdmin.
  10.  *
  11.  * @author Maxime Steinhausser <maxime.steinhausser@gmail.com>
  12.  */
  13. class RequestPostInitializeListener
  14. {
  15.     /** @var RequestStack|null */
  16.     private $requestStack;
  17.     /** @var Registry */
  18.     private $doctrine;
  19.     /**
  20.      * @param Registry          $doctrine
  21.      * @param RequestStack|null $requestStack
  22.      */
  23.     public function __construct(Registry $doctrineRequestStack $requestStack null)
  24.     {
  25.         $this->doctrine $doctrine;
  26.         $this->requestStack $requestStack;
  27.     }
  28.     /**
  29.      * Adds to the request some attributes with useful information, such as the
  30.      * current entity and the selected item, if any.
  31.      *
  32.      * @param GenericEvent $event
  33.      */
  34.     public function initializeRequest(GenericEvent $event)
  35.     {
  36.         $request null;
  37.         if (null !== $this->requestStack) {
  38.             $request $this->requestStack->getCurrentRequest();
  39.         }
  40.         if (null === $request) {
  41.             return;
  42.         }
  43.         $request->attributes->set('easyadmin', [
  44.             'entity' => $entity $event->getArgument('entity'),
  45.             'view' => $request->query->get('action''list'),
  46.             'item' => (null !== $id $request->query->get('id')) ? $this->findCurrentItem($entity$id) : null,
  47.         ]);
  48.     }
  49.     /**
  50.      * Looks for the object that corresponds to the selected 'id' of the current entity.
  51.      *
  52.      * @param array $entityConfig
  53.      * @param mixed $itemId
  54.      *
  55.      * @return object The entity
  56.      *
  57.      * @throws EntityNotFoundException
  58.      * @throws \RuntimeException
  59.      */
  60.     private function findCurrentItem(array $entityConfig$itemId)
  61.     {
  62.         if (null === $manager $this->doctrine->getManagerForClass($entityConfig['class'])) {
  63.             throw new \RuntimeException(sprintf('There is no Doctrine Entity Manager defined for the "%s" class'$entityConfig['class']));
  64.         }
  65.         if (null === $entity $manager->getRepository($entityConfig['class'])->find($itemId)) {
  66.             throw new EntityNotFoundException(['entity_name' => $entityConfig['name'], 'entity_id_name' => $entityConfig['primary_key_field_name'], 'entity_id_value' => $itemId]);
  67.         }
  68.         return $entity;
  69.     }
  70. }