vendor/easycorp/easyadmin-bundle/src/Factory/AdminContextFactory.php line 90

Open in your IDE?
  1. <?php
  2. namespace EasyCorp\Bundle\EasyAdminBundle\Factory;
  3. use EasyCorp\Bundle\EasyAdminBundle\Cache\CacheWarmer;
  4. use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
  5. use EasyCorp\Bundle\EasyAdminBundle\Config\Option\EA;
  6. use EasyCorp\Bundle\EasyAdminBundle\Config\Option\TextDirection;
  7. use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
  8. use EasyCorp\Bundle\EasyAdminBundle\Contracts\Controller\CrudControllerInterface;
  9. use EasyCorp\Bundle\EasyAdminBundle\Contracts\Controller\DashboardControllerInterface;
  10. use EasyCorp\Bundle\EasyAdminBundle\Dto\ActionConfigDto;
  11. use EasyCorp\Bundle\EasyAdminBundle\Dto\AssetsDto;
  12. use EasyCorp\Bundle\EasyAdminBundle\Dto\CrudDto;
  13. use EasyCorp\Bundle\EasyAdminBundle\Dto\DashboardDto;
  14. use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
  15. use EasyCorp\Bundle\EasyAdminBundle\Dto\FilterConfigDto;
  16. use EasyCorp\Bundle\EasyAdminBundle\Dto\I18nDto;
  17. use EasyCorp\Bundle\EasyAdminBundle\Dto\SearchDto;
  18. use EasyCorp\Bundle\EasyAdminBundle\Registry\CrudControllerRegistry;
  19. use EasyCorp\Bundle\EasyAdminBundle\Registry\TemplateRegistry;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  22. use Symfony\Component\Security\Core\User\UserInterface;
  23. use function Symfony\Component\String\u;
  24. use function Symfony\Component\Translation\t;
  25. use Symfony\Contracts\Translation\TranslatableInterface;
  26. /**
  27.  * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  28.  */
  29. final class AdminContextFactory
  30. {
  31.     private string $cacheDir;
  32.     private ?TokenStorageInterface $tokenStorage;
  33.     private MenuFactory $menuFactory;
  34.     private CrudControllerRegistry $crudControllers;
  35.     private EntityFactory $entityFactory;
  36.     public function __construct(string $cacheDir, ?TokenStorageInterface $tokenStorageMenuFactory $menuFactoryCrudControllerRegistry $crudControllersEntityFactory $entityFactory)
  37.     {
  38.         $this->cacheDir $cacheDir;
  39.         $this->tokenStorage $tokenStorage;
  40.         $this->menuFactory $menuFactory;
  41.         $this->crudControllers $crudControllers;
  42.         $this->entityFactory $entityFactory;
  43.     }
  44.     public function create(Request $requestDashboardControllerInterface $dashboardController, ?CrudControllerInterface $crudController): AdminContext
  45.     {
  46.         $crudAction $request->query->get(EA::CRUD_ACTION);
  47.         $validPageNames = [Crud::PAGE_INDEXCrud::PAGE_DETAILCrud::PAGE_EDITCrud::PAGE_NEW];
  48.         $pageName \in_array($crudAction$validPageNamestrue) ? $crudAction null;
  49.         $dashboardDto $this->getDashboardDto($request$dashboardController);
  50.         $assetDto $this->getAssetDto($dashboardController$crudController$pageName);
  51.         $actionConfigDto $this->getActionConfig($dashboardController$crudController$pageName);
  52.         $filters $this->getFilters($dashboardController$crudController);
  53.         $crudDto $this->getCrudDto($this->crudControllers$dashboardController$crudController$actionConfigDto$filters$crudAction$pageName);
  54.         $entityDto $this->getEntityDto($request$crudDto);
  55.         $searchDto $this->getSearchDto($request$crudDto);
  56.         $i18nDto $this->getI18nDto($request$dashboardDto$crudDto$entityDto);
  57.         $templateRegistry $this->getTemplateRegistry($dashboardController$crudDto);
  58.         $user $this->getUser($this->tokenStorage);
  59.         return new AdminContext($request$user$i18nDto$this->crudControllers$dashboardDto$dashboardController$assetDto$crudDto$entityDto$searchDto$this->menuFactory$templateRegistry);
  60.     }
  61.     private function getDashboardDto(Request $requestDashboardControllerInterface $dashboardControllerInstance): DashboardDto
  62.     {
  63.         $dashboardRoutesCachePath $this->cacheDir.'/'.CacheWarmer::DASHBOARD_ROUTES_CACHE;
  64.         $dashboardControllerRoutes = !file_exists($dashboardRoutesCachePath) ? [] : require $dashboardRoutesCachePath;
  65.         $dashboardController \get_class($dashboardControllerInstance).'::index';
  66.         $dashboardRouteName null;
  67.         foreach ($dashboardControllerRoutes as $routeName => $controller) {
  68.             if ($controller === $dashboardController) {
  69.                 // needed for i18n routes, whose name follows the pattern "route_name.locale"
  70.                 $dashboardRouteName explode('.'$routeName2)[0];
  71.                 break;
  72.             }
  73.         }
  74.         if (null === $dashboardRouteName) {
  75.             throw new \RuntimeException(sprintf('The name of the route associated to "%s" cannot be determined. Clear the application cache to run the EasyAdmin cache warmer, which generates the needed data to find this route.'$dashboardController));
  76.         }
  77.         $dashboardDto $dashboardControllerInstance->configureDashboard()->getAsDto();
  78.         $dashboardDto->setRouteName($dashboardRouteName);
  79.         return $dashboardDto;
  80.     }
  81.     private function getAssetDto(DashboardControllerInterface $dashboardController, ?CrudControllerInterface $crudController, ?string $pageName): AssetsDto
  82.     {
  83.         $defaultAssets $dashboardController->configureAssets();
  84.         if (null === $crudController) {
  85.             return $defaultAssets->getAsDto();
  86.         }
  87.         return $crudController->configureAssets($defaultAssets)->getAsDto()->loadedOn($pageName);
  88.     }
  89.     private function getCrudDto(CrudControllerRegistry $crudControllersDashboardControllerInterface $dashboardController, ?CrudControllerInterface $crudControllerActionConfigDto $actionConfigDtoFilterConfigDto $filters, ?string $crudAction, ?string $pageName): ?CrudDto
  90.     {
  91.         if (null === $crudController) {
  92.             return null;
  93.         }
  94.         $defaultCrud $dashboardController->configureCrud();
  95.         $crudDto $crudController->configureCrud($defaultCrud)->getAsDto();
  96.         $entityFqcn $crudControllers->findEntityFqcnByCrudFqcn(\get_class($crudController));
  97.         $crudDto->setControllerFqcn(\get_class($crudController));
  98.         $crudDto->setActionsConfig($actionConfigDto);
  99.         $crudDto->setFiltersConfig($filters);
  100.         $crudDto->setCurrentAction($crudAction);
  101.         $crudDto->setEntityFqcn($entityFqcn);
  102.         $crudDto->setPageName($pageName);
  103.         return $crudDto;
  104.     }
  105.     private function getActionConfig(DashboardControllerInterface $dashboardController, ?CrudControllerInterface $crudController, ?string $pageName): ActionConfigDto
  106.     {
  107.         if (null === $crudController) {
  108.             return new ActionConfigDto();
  109.         }
  110.         $defaultActionConfig $dashboardController->configureActions();
  111.         return $crudController->configureActions($defaultActionConfig)->getAsDto($pageName);
  112.     }
  113.     private function getFilters(DashboardControllerInterface $dashboardController, ?CrudControllerInterface $crudController): FilterConfigDto
  114.     {
  115.         if (null === $crudController) {
  116.             return new FilterConfigDto();
  117.         }
  118.         $defaultFilterConfig $dashboardController->configureFilters();
  119.         return $crudController->configureFilters($defaultFilterConfig)->getAsDto();
  120.     }
  121.     private function getTemplateRegistry(DashboardControllerInterface $dashboardController, ?CrudDto $crudDto): TemplateRegistry
  122.     {
  123.         $templateRegistry TemplateRegistry::new();
  124.         $defaultCrudDto $dashboardController->configureCrud()->getAsDto();
  125.         $templateRegistry->setTemplates($defaultCrudDto->getOverriddenTemplates());
  126.         if (null !== $crudDto) {
  127.             $templateRegistry->setTemplates($crudDto->getOverriddenTemplates());
  128.         }
  129.         return $templateRegistry;
  130.     }
  131.     private function getI18nDto(Request $requestDashboardDto $dashboardDto, ?CrudDto $crudDto, ?EntityDto $entityDto): I18nDto
  132.     {
  133.         $locale $request->getLocale();
  134.         $configuredTextDirection $dashboardDto->getTextDirection();
  135.         $localePrefix strtolower(substr($locale02));
  136.         $defaultTextDirection \in_array($localePrefix, ['ar''fa''he']) ? TextDirection::RTL TextDirection::LTR;
  137.         $textDirection $configuredTextDirection ?? $defaultTextDirection;
  138.         $translationDomain $dashboardDto->getTranslationDomain();
  139.         $translationParameters = [];
  140.         if (null !== $crudDto) {
  141.             $translationParameters['%entity_name%'] = $entityName basename(str_replace('\\''/'$crudDto->getEntityFqcn()));
  142.             $translationParameters['%entity_as_string%'] = null === $entityDto '' $entityDto->toString();
  143.             $translationParameters['%entity_id%'] = $entityId $request->query->get(EA::ENTITY_ID);
  144.             $translationParameters['%entity_short_id%'] = null === $entityId null u((string) $entityId)->truncate(7)->toString();
  145.             $entityInstance null === $entityDto null $entityDto->getInstance();
  146.             $pageName $crudDto->getCurrentPage();
  147.             $singularLabel $crudDto->getEntityLabelInSingular($entityInstance$pageName);
  148.             if (!$singularLabel instanceof TranslatableInterface) {
  149.                 $singularLabel t($singularLabel ?? $entityName$translationParameters$translationDomain);
  150.             }
  151.             $pluralLabel $crudDto->getEntityLabelInPlural($entityInstance$pageName);
  152.             if (!$pluralLabel instanceof TranslatableInterface) {
  153.                 $pluralLabel t($pluralLabel ?? $entityName$translationParameters$translationDomain);
  154.             }
  155.             $crudDto->setEntityLabelInSingular($singularLabel);
  156.             $crudDto->setEntityLabelInPlural($pluralLabel);
  157.             $translationParameters['%entity_label_singular%'] = $singularLabel;
  158.             $translationParameters['%entity_label_plural%'] = $pluralLabel;
  159.         }
  160.         return new I18nDto($locale$textDirection$translationDomain$translationParameters);
  161.     }
  162.     public function getSearchDto(Request $request, ?CrudDto $crudDto): ?SearchDto
  163.     {
  164.         if (null === $crudDto) {
  165.             return null;
  166.         }
  167.         $queryParams $request->query->all();
  168.         $searchableProperties $crudDto->getSearchFields();
  169.         $query $queryParams[EA::QUERY] ?? null;
  170.         $defaultSort $crudDto->getDefaultSort();
  171.         $customSort $queryParams[EA::SORT] ?? [];
  172.         $appliedFilters $queryParams[EA::FILTERS] ?? [];
  173.         return new SearchDto($request$searchableProperties$query$defaultSort$customSort$appliedFilters);
  174.     }
  175.     // Copied from https://github.com/symfony/twig-bridge/blob/master/AppVariable.php
  176.     // (c) Fabien Potencier <fabien@symfony.com> - MIT License
  177.     private function getUser(?TokenStorageInterface $tokenStorage): ?UserInterface
  178.     {
  179.         if (null === $tokenStorage || !$token $tokenStorage->getToken()) {
  180.             return null;
  181.         }
  182.         $user $token->getUser();
  183.         return \is_object($user) ? $user null;
  184.     }
  185.     private function getEntityDto(Request $request, ?CrudDto $crudDto): ?EntityDto
  186.     {
  187.         if (null === $crudDto) {
  188.             return null;
  189.         }
  190.         return $this->entityFactory->create($crudDto->getEntityFqcn(), $request->query->get(EA::ENTITY_ID), $crudDto->getEntityPermission());
  191.     }
  192. }