vendor/easycorp/easyadmin-bundle/src/EventListener/ControllerListener.php line 37

Open in your IDE?
  1. <?php
  2. namespace EasyCorp\Bundle\EasyAdminBundle\EventListener;
  3. use EasyCorp\Bundle\EasyAdminBundle\Configuration\ConfigManager;
  4. use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
  5. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  6. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  7. use Symfony\Component\HttpKernel\Kernel;
  8. /**
  9.  * Sets the right controller to be executed when entities define custom
  10.  * controllers.
  11.  *
  12.  * @author Yonel Ceruto <yonelceruto@gmail.com>
  13.  */
  14. class ControllerListener
  15. {
  16.     /** @var ConfigManager */
  17.     private $configManager;
  18.     /** @var ControllerResolverInterface */
  19.     private $resolver;
  20.     public function __construct(ConfigManager $configManagerControllerResolverInterface $resolver)
  21.     {
  22.         $this->configManager $configManager;
  23.         $this->resolver $resolver;
  24.     }
  25.     /**
  26.      * Exchange default admin controller by custom entity admin controller.
  27.      *
  28.      * @param FilterControllerEvent $event
  29.      *
  30.      * @throws NotFoundHttpException
  31.      */
  32.     public function onKernelController(FilterControllerEvent $event)
  33.     {
  34.         $request $event->getRequest();
  35.         if ('easyadmin' !== $request->attributes->get('_route')) {
  36.             return;
  37.         }
  38.         $currentController $event->getController();
  39.         // if the controller is defined in a class, $currentController is an array
  40.         // otherwise do nothing because it's a Closure (rare but possible in Symfony)
  41.         if (!\is_array($currentController)) {
  42.             return;
  43.         }
  44.         // this condition happens when accessing the backend homepage, which
  45.         // then redirects to the 'list' action of the first configured entity.
  46.         if (null === $entityName $request->query->get('entity')) {
  47.             return;
  48.         }
  49.         $entity $this->configManager->getEntityConfig($entityName);
  50.         // if the entity doesn't define a custom controller, do nothing
  51.         if (!isset($entity['controller'])) {
  52.             return;
  53.         }
  54.         // build the full controller name using the 'class::method' syntax
  55.         $controllerMethod $currentController[1];
  56.         $customController $entity['controller'].'::'.$controllerMethod;
  57.         $request->attributes->set('_controller'$customController);
  58.         $newController $this->resolver->getController($request);
  59.         if (false === $newController) {
  60.             throw new NotFoundHttpException(\sprintf('Unable to find the controller for path "%s". Check the "controller" configuration of the "%s" entity in your EasyAdmin backend.'$request->getPathInfo(), $entityName));
  61.         }
  62.         $event->setController($newController);
  63.     }
  64. }