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

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the EasyAdminBundle.
  4.  *
  5.  * (c) Javier Eguiluz <javier.eguiluz@gmail.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace EasyCorp\Bundle\EasyAdminBundle\EventListener;
  11. use EasyCorp\Bundle\EasyAdminBundle\Configuration\ConfigManager;
  12. use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
  13. use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
  14. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  15. /**
  16.  * Sets the right controller to be executed when entities define custom
  17.  * controllers.
  18.  *
  19.  * @author Yonel Ceruto <yonelceruto@gmail.com>
  20.  */
  21. class ControllerListener
  22. {
  23.     /** @var ConfigManager */
  24.     private $configManager;
  25.     /** @var ControllerResolverInterface */
  26.     private $resolver;
  27.     public function __construct(ConfigManager $configManagerControllerResolverInterface $resolver)
  28.     {
  29.         $this->configManager $configManager;
  30.         $this->resolver $resolver;
  31.     }
  32.     /**
  33.      * Exchange default admin controller by custom entity admin controller.
  34.      *
  35.      * @param FilterControllerEvent $event
  36.      */
  37.     public function onKernelController(FilterControllerEvent $event)
  38.     {
  39.         $request $event->getRequest();
  40.         if ('easyadmin' !== $request->attributes->get('_route')) {
  41.             return;
  42.         }
  43.         $currentController $event->getController();
  44.         // if the controller is defined in a class, $currentController is an array
  45.         // otherwise do nothing because it's a Closure (rare but possible in Symfony)
  46.         if (!is_array($currentController)) {
  47.             return;
  48.         }
  49.         // this condition happens when accessing the backend homepage, which
  50.         // then redirects to the 'list' action of the first configured entity.
  51.         if (null === $entityName $request->query->get('entity')) {
  52.             return;
  53.         }
  54.         $entity $this->configManager->getEntityConfig($entityName);
  55.         // if the entity doesn't define a custom controller, do nothing
  56.         if (!isset($entity['controller'])) {
  57.             return;
  58.         }
  59.         $customController $entity['controller'];
  60.         $controllerMethod $currentController[1];
  61.         // build the full controller name depending on its type
  62.         if (class_exists($customController)) {
  63.             // 'class::method' syntax for normal controllers
  64.             $customController .= '::'.$controllerMethod;
  65.         } else {
  66.             // 'service:method' syntax for controllers as services
  67.             $customController .= ':'.$controllerMethod;
  68.         }
  69.         $request->attributes->set('_controller'$customController);
  70.         $newController $this->resolver->getController($request);
  71.         if (false === $newController) {
  72.             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));
  73.         }
  74.         $event->setController($newController);
  75.     }
  76. }
  77. class_alias('EasyCorp\Bundle\EasyAdminBundle\EventListener\ControllerListener''JavierEguiluz\Bundle\EasyAdminBundle\EventListener\ControllerListener'false);