vendor/easycorp/easyadmin-bundle/src/Factory/ControllerFactory.php line 48

Open in your IDE?
  1. <?php
  2. namespace EasyCorp\Bundle\EasyAdminBundle\Factory;
  3. use EasyCorp\Bundle\EasyAdminBundle\Contracts\Controller\CrudControllerInterface;
  4. use EasyCorp\Bundle\EasyAdminBundle\Contracts\Controller\DashboardControllerInterface;
  5. use EasyCorp\Bundle\EasyAdminBundle\Registry\CrudControllerRegistry;
  6. use EasyCorp\Bundle\EasyAdminBundle\Registry\DashboardControllerRegistry;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
  9. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  10. /**
  11.  * @author Lukas Lücke <[email protected]>
  12.  */
  13. final class ControllerFactory
  14. {
  15.     private $dashboardControllers;
  16.     private $crudControllers;
  17.     private $controllerResolver;
  18.     public function __construct(DashboardControllerRegistry $dashboardControllersCrudControllerRegistry $crudControllersControllerResolverInterface $controllerResolver)
  19.     {
  20.         $this->dashboardControllers $dashboardControllers;
  21.         $this->crudControllers $crudControllers;
  22.         $this->controllerResolver $controllerResolver;
  23.     }
  24.     public function getDashboardControllerInstance(string $controllerFqcnRequest $request): ?DashboardControllerInterface
  25.     {
  26.         return $this->getDashboardController($controllerFqcn$request);
  27.     }
  28.     public function getCrudControllerInstance(?string $crudControllerFqcn, ?string $crudActionRequest $request): ?CrudControllerInterface
  29.     {
  30.         if (null === $crudControllerFqcn) {
  31.             return null;
  32.         }
  33.         return $this->getCrudController($crudControllerFqcn$crudAction$request);
  34.     }
  35.     private function getDashboardController(?string $dashboardControllerFqcnRequest $request): ?DashboardControllerInterface
  36.     {
  37.         return $this->getController(DashboardControllerInterface::class, $dashboardControllerFqcn'index'$request);
  38.     }
  39.     private function getCrudController(?string $crudControllerFqcn, ?string $crudActionRequest $request): ?CrudControllerInterface
  40.     {
  41.         return $this->getController(CrudControllerInterface::class, $crudControllerFqcn$crudAction$request);
  42.     }
  43.     private function getController(string $controllerInterface, ?string $controllerFqcn, ?string $controllerActionRequest $request)
  44.     {
  45.         if (null === $controllerFqcn || null === $controllerAction) {
  46.             return null;
  47.         }
  48.         $newRequest $request->duplicate(nullnull, ['_controller' => [$controllerFqcn$controllerAction]]);
  49.         $controllerCallable $this->controllerResolver->getController($newRequest);
  50.         if (false === $controllerCallable) {
  51.             throw new NotFoundHttpException(sprintf('Unable to find the controller "%s::%s".'$controllerFqcn$controllerAction));
  52.         }
  53.         if (!\is_array($controllerCallable)) {
  54.             return null;
  55.         }
  56.         $controllerInstance $controllerCallable[0];
  57.         return is_subclass_of($controllerInstance$controllerInterface) ? $controllerInstance null;
  58.     }
  59. }