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

Open in your IDE?
  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\Dto\AssetsDto;
  7. use EasyCorp\Bundle\EasyAdminBundle\Dto\CrudDto;
  8. use EasyCorp\Bundle\EasyAdminBundle\Dto\DashboardDto;
  9. use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
  10. use EasyCorp\Bundle\EasyAdminBundle\Dto\I18nDto;
  11. use EasyCorp\Bundle\EasyAdminBundle\Dto\LocaleDto;
  12. use EasyCorp\Bundle\EasyAdminBundle\Dto\MainMenuDto;
  13. use EasyCorp\Bundle\EasyAdminBundle\Dto\SearchDto;
  14. use EasyCorp\Bundle\EasyAdminBundle\Dto\UserMenuDto;
  15. use EasyCorp\Bundle\EasyAdminBundle\Factory\MenuFactory;
  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 MenuFactory $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 $searchDtoMenuFactory $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.         return $this->request->query->get(EA::REFERRER);
  63.     }
  64.     public function getI18n(): I18nDto
  65.     {
  66.         return $this->i18nDto;
  67.     }
  68.     public function getCrudControllers(): CrudControllerRegistry
  69.     {
  70.         return $this->crudControllers;
  71.     }
  72.     public function getEntity(): EntityDto
  73.     {
  74.         return $this->entityDto;
  75.     }
  76.     public function getUser(): ?UserInterface
  77.     {
  78.         return $this->user;
  79.     }
  80.     public function getAssets(): AssetsDto
  81.     {
  82.         return $this->assetDto;
  83.     }
  84.     public function getSignedUrls(): bool
  85.     {
  86.         return $this->dashboardDto->getSignedUrls();
  87.     }
  88.     public function getAbsoluteUrls(): bool
  89.     {
  90.         return $this->dashboardDto->getAbsoluteUrls();
  91.     }
  92.     public function getDashboardTitle(): string
  93.     {
  94.         return $this->dashboardDto->getTitle();
  95.     }
  96.     public function getDashboardFaviconPath(): string
  97.     {
  98.         return $this->dashboardDto->getFaviconPath();
  99.     }
  100.     public function getDashboardControllerFqcn(): string
  101.     {
  102.         return \get_class($this->dashboardControllerInstance);
  103.     }
  104.     public function getDashboardRouteName(): string
  105.     {
  106.         return $this->dashboardDto->getRouteName();
  107.     }
  108.     public function getDashboardContentWidth(): string
  109.     {
  110.         return $this->dashboardDto->getContentWidth();
  111.     }
  112.     public function getDashboardSidebarWidth(): string
  113.     {
  114.         return $this->dashboardDto->getSidebarWidth();
  115.     }
  116.     public function getDashboardHasDarkModeEnabled(): bool
  117.     {
  118.         return $this->dashboardDto->isDarkModeEnabled();
  119.     }
  120.     /**
  121.      * @return LocaleDto[]
  122.      */
  123.     public function getDashboardLocales(): array
  124.     {
  125.         return $this->dashboardDto->getLocales();
  126.     }
  127.     public function getMainMenu(): MainMenuDto
  128.     {
  129.         if (null !== $this->mainMenuDto) {
  130.             return $this->mainMenuDto;
  131.         }
  132.         $configuredMenuItems $this->dashboardControllerInstance->configureMenuItems();
  133.         $mainMenuItems \is_array($configuredMenuItems) ? $configuredMenuItems iterator_to_array($configuredMenuItemsfalse);
  134.         return $this->mainMenuDto $this->menuFactory->createMainMenu($mainMenuItems);
  135.     }
  136.     public function getUserMenu(): UserMenuDto
  137.     {
  138.         if (null !== $this->userMenuDto) {
  139.             return $this->userMenuDto;
  140.         }
  141.         if (null === $this->user) {
  142.             return UserMenu::new()->getAsDto();
  143.         }
  144.         $userMenu $this->dashboardControllerInstance->configureUserMenu($this->user);
  145.         return $this->userMenuDto $this->menuFactory->createUserMenu($userMenu);
  146.     }
  147.     public function getCrud(): ?CrudDto
  148.     {
  149.         return $this->crudDto;
  150.     }
  151.     public function getSearch(): ?SearchDto
  152.     {
  153.         return $this->searchDto;
  154.     }
  155.     public function getTemplatePath(string $templateName): string
  156.     {
  157.         return $this->templateRegistry->get($templateName);
  158.     }
  159. }