vendor/easycorp/easyadmin-bundle/src/Context/AdminContext.php line 28

  1. <?php
  2. namespace EasyCorp\Bundle\EasyAdminBundle\Context;
  3. use EasyCorp\Bundle\EasyAdminBundle\Config\Option\EA;
  4. use EasyCorp\Bundle\EasyAdminBundle\Config\UserMenu;
  5. use EasyCorp\Bundle\EasyAdminBundle\Contracts\Controller\DashboardControllerInterface;
  6. use EasyCorp\Bundle\EasyAdminBundle\Contracts\Factory\MenuFactoryInterface;
  7. use EasyCorp\Bundle\EasyAdminBundle\Dto\AssetsDto;
  8. use EasyCorp\Bundle\EasyAdminBundle\Dto\CrudDto;
  9. use EasyCorp\Bundle\EasyAdminBundle\Dto\DashboardDto;
  10. use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
  11. use EasyCorp\Bundle\EasyAdminBundle\Dto\I18nDto;
  12. use EasyCorp\Bundle\EasyAdminBundle\Dto\LocaleDto;
  13. use EasyCorp\Bundle\EasyAdminBundle\Dto\MainMenuDto;
  14. use EasyCorp\Bundle\EasyAdminBundle\Dto\SearchDto;
  15. use EasyCorp\Bundle\EasyAdminBundle\Dto\UserMenuDto;
  16. use EasyCorp\Bundle\EasyAdminBundle\Registry\CrudControllerRegistry;
  17. use EasyCorp\Bundle\EasyAdminBundle\Registry\TemplateRegistry;
  18. use Symfony\Component\HttpFoundation\Request;
  19. use Symfony\Component\Security\Core\User\UserInterface;
  20. /**
  21.  * A context object that stores all the state and config of the current admin request.
  22.  *
  23.  * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  24.  */
  25. final class AdminContext
  26. {
  27.     private Request $request;
  28.     private ?UserInterface $user;
  29.     private I18nDto $i18nDto;
  30.     private CrudControllerRegistry $crudControllers;
  31.     private ?EntityDto $entityDto;
  32.     private DashboardDto $dashboardDto;
  33.     private DashboardControllerInterface $dashboardControllerInstance;
  34.     private AssetsDto $assetDto;
  35.     private ?CrudDto $crudDto;
  36.     private ?SearchDto $searchDto;
  37.     private MenuFactoryInterface $menuFactory;
  38.     private TemplateRegistry $templateRegistry;
  39.     private ?MainMenuDto $mainMenuDto null;
  40.     private ?UserMenuDto $userMenuDto null;
  41.     public function __construct(Request $request, ?UserInterface $userI18nDto $i18nDtoCrudControllerRegistry $crudControllersDashboardDto $dashboardDtoDashboardControllerInterface $dashboardControllerAssetsDto $assetDto, ?CrudDto $crudDto, ?EntityDto $entityDto, ?SearchDto $searchDtoMenuFactoryInterface $menuFactoryTemplateRegistry $templateRegistry)
  42.     {
  43.         $this->request $request;
  44.         $this->user $user;
  45.         $this->i18nDto $i18nDto;
  46.         $this->crudControllers $crudControllers;
  47.         $this->dashboardDto $dashboardDto;
  48.         $this->dashboardControllerInstance $dashboardController;
  49.         $this->crudDto $crudDto;
  50.         $this->assetDto $assetDto;
  51.         $this->entityDto $entityDto;
  52.         $this->searchDto $searchDto;
  53.         $this->menuFactory $menuFactory;
  54.         $this->templateRegistry $templateRegistry;
  55.     }
  56.     public function getRequest(): Request
  57.     {
  58.         return $this->request;
  59.     }
  60.     public function getReferrer(): ?string
  61.     {
  62.         $referrer $this->request->query->get(EA::REFERRER);
  63.         return '' !== $referrer $referrer null;
  64.     }
  65.     public function getI18n(): I18nDto
  66.     {
  67.         return $this->i18nDto;
  68.     }
  69.     public function getCrudControllers(): CrudControllerRegistry
  70.     {
  71.         return $this->crudControllers;
  72.     }
  73.     public function getEntity(): EntityDto
  74.     {
  75.         return $this->entityDto;
  76.     }
  77.     public function getUser(): ?UserInterface
  78.     {
  79.         return $this->user;
  80.     }
  81.     public function getAssets(): AssetsDto
  82.     {
  83.         return $this->assetDto;
  84.     }
  85.     public function getSignedUrls(): bool
  86.     {
  87.         return $this->dashboardDto->getSignedUrls();
  88.     }
  89.     public function getAbsoluteUrls(): bool
  90.     {
  91.         return $this->dashboardDto->getAbsoluteUrls();
  92.     }
  93.     public function getDashboardTitle(): string
  94.     {
  95.         return $this->dashboardDto->getTitle();
  96.     }
  97.     public function getDashboardFaviconPath(): string
  98.     {
  99.         return $this->dashboardDto->getFaviconPath();
  100.     }
  101.     public function getDashboardControllerFqcn(): string
  102.     {
  103.         return \get_class($this->dashboardControllerInstance);
  104.     }
  105.     public function getDashboardRouteName(): string
  106.     {
  107.         return $this->dashboardDto->getRouteName();
  108.     }
  109.     public function getDashboardContentWidth(): string
  110.     {
  111.         return $this->dashboardDto->getContentWidth();
  112.     }
  113.     public function getDashboardSidebarWidth(): string
  114.     {
  115.         return $this->dashboardDto->getSidebarWidth();
  116.     }
  117.     public function getDashboardHasDarkModeEnabled(): bool
  118.     {
  119.         return $this->dashboardDto->isDarkModeEnabled();
  120.     }
  121.     /**
  122.      * @return LocaleDto[]
  123.      */
  124.     public function getDashboardLocales(): array
  125.     {
  126.         return $this->dashboardDto->getLocales();
  127.     }
  128.     public function getMainMenu(): MainMenuDto
  129.     {
  130.         if (null !== $this->mainMenuDto) {
  131.             return $this->mainMenuDto;
  132.         }
  133.         $configuredMenuItems $this->dashboardControllerInstance->configureMenuItems();
  134.         $mainMenuItems \is_array($configuredMenuItems) ? $configuredMenuItems iterator_to_array($configuredMenuItemsfalse);
  135.         return $this->mainMenuDto $this->menuFactory->createMainMenu($mainMenuItems);
  136.     }
  137.     public function getUserMenu(): UserMenuDto
  138.     {
  139.         if (null !== $this->userMenuDto) {
  140.             return $this->userMenuDto;
  141.         }
  142.         if (null === $this->user) {
  143.             return UserMenu::new()->getAsDto();
  144.         }
  145.         $userMenu $this->dashboardControllerInstance->configureUserMenu($this->user);
  146.         return $this->userMenuDto $this->menuFactory->createUserMenu($userMenu);
  147.     }
  148.     public function getCrud(): ?CrudDto
  149.     {
  150.         return $this->crudDto;
  151.     }
  152.     public function getSearch(): ?SearchDto
  153.     {
  154.         return $this->searchDto;
  155.     }
  156.     public function getTemplatePath(string $templateName): string
  157.     {
  158.         return $this->templateRegistry->get($templateName);
  159.     }
  160. }