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

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