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

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\Context\AdminContextInterface;
  6. use EasyCorp\Bundle\EasyAdminBundle\Contracts\Controller\DashboardControllerInterface;
  7. use EasyCorp\Bundle\EasyAdminBundle\Contracts\Factory\MenuFactoryInterface;
  8. use EasyCorp\Bundle\EasyAdminBundle\Dto\AssetsDto;
  9. use EasyCorp\Bundle\EasyAdminBundle\Dto\CrudDto;
  10. use EasyCorp\Bundle\EasyAdminBundle\Dto\DashboardDto;
  11. use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
  12. use EasyCorp\Bundle\EasyAdminBundle\Dto\I18nDto;
  13. use EasyCorp\Bundle\EasyAdminBundle\Dto\LocaleDto;
  14. use EasyCorp\Bundle\EasyAdminBundle\Dto\MainMenuDto;
  15. use EasyCorp\Bundle\EasyAdminBundle\Dto\SearchDto;
  16. use EasyCorp\Bundle\EasyAdminBundle\Dto\UserMenuDto;
  17. use EasyCorp\Bundle\EasyAdminBundle\Registry\CrudControllerRegistry;
  18. use EasyCorp\Bundle\EasyAdminBundle\Registry\TemplateRegistry;
  19. use Symfony\Component\HttpFoundation\Request;
  20. use Symfony\Component\Security\Core\User\UserInterface;
  21. /**
  22.  * A context object that stores all the state and config of the current admin request.
  23.  *
  24.  * IMPORTANT: any new methods added here MUST be duplicated in the AdminContextProvider class.
  25.  *
  26.  * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  27.  */
  28. final class AdminContext implements AdminContextInterface
  29. {
  30.     private Request $request;
  31.     private ?UserInterface $user;
  32.     private I18nDto $i18nDto;
  33.     private CrudControllerRegistry $crudControllers;
  34.     private ?EntityDto $entityDto;
  35.     private DashboardDto $dashboardDto;
  36.     private DashboardControllerInterface $dashboardControllerInstance;
  37.     private AssetsDto $assetDto;
  38.     private ?CrudDto $crudDto;
  39.     private ?SearchDto $searchDto;
  40.     private MenuFactoryInterface $menuFactory;
  41.     private TemplateRegistry $templateRegistry;
  42.     private ?MainMenuDto $mainMenuDto null;
  43.     private ?UserMenuDto $userMenuDto null;
  44.     private bool $usePrettyUrls;
  45.     public function __construct(Request $request, ?UserInterface $userI18nDto $i18nDtoCrudControllerRegistry $crudControllersDashboardDto $dashboardDtoDashboardControllerInterface $dashboardControllerAssetsDto $assetDto, ?CrudDto $crudDto, ?EntityDto $entityDto, ?SearchDto $searchDtoMenuFactoryInterface $menuFactoryTemplateRegistry $templateRegistrybool $usePrettyUrls false)
  46.     {
  47.         $this->request $request;
  48.         $this->user $user;
  49.         $this->i18nDto $i18nDto;
  50.         $this->crudControllers $crudControllers;
  51.         $this->dashboardDto $dashboardDto;
  52.         $this->dashboardControllerInstance $dashboardController;
  53.         $this->crudDto $crudDto;
  54.         $this->assetDto $assetDto;
  55.         $this->entityDto $entityDto;
  56.         $this->searchDto $searchDto;
  57.         $this->menuFactory $menuFactory;
  58.         $this->templateRegistry $templateRegistry;
  59.         $this->usePrettyUrls $usePrettyUrls;
  60.     }
  61.     public function getRequest(): Request
  62.     {
  63.         return $this->request;
  64.     }
  65.     public function getReferrer(): ?string
  66.     {
  67.         trigger_deprecation(
  68.             'easycorp/easyadmin-bundle',
  69.             '4.8.11',
  70.             'EasyAdmin URLs no longer include the referrer URL. If you still need it, you can get the referrer provided by browsers via $context->getRequest()->headers->get(\'referer\').',
  71.             __METHOD__,
  72.         );
  73.         $referrer $this->request->query->get(EA::REFERRER);
  74.         return '' !== $referrer $referrer null;
  75.     }
  76.     public function getI18n(): I18nDto
  77.     {
  78.         return $this->i18nDto;
  79.     }
  80.     public function getCrudControllers(): CrudControllerRegistry
  81.     {
  82.         return $this->crudControllers;
  83.     }
  84.     public function getEntity(): EntityDto
  85.     {
  86.         return $this->entityDto;
  87.     }
  88.     public function getUser(): ?UserInterface
  89.     {
  90.         return $this->user;
  91.     }
  92.     public function getAssets(): AssetsDto
  93.     {
  94.         return $this->assetDto;
  95.     }
  96.     public function getSignedUrls(): bool
  97.     {
  98.         trigger_deprecation(
  99.             'easycorp/easyadmin-bundle',
  100.             '4.1.0',
  101.             'EasyAdmin URLs no longer include signatures because they don\'t provide any additional security. The "%s" method will be removed in EasyAdmin 5.0.0, so you should stop using it.',
  102.             __METHOD__
  103.         );
  104.         return $this->dashboardDto->getSignedUrls();
  105.     }
  106.     public function getAbsoluteUrls(): bool
  107.     {
  108.         return $this->dashboardDto->getAbsoluteUrls();
  109.     }
  110.     public function usePrettyUrls(): bool
  111.     {
  112.         return $this->usePrettyUrls;
  113.     }
  114.     public function getDashboardTitle(): string
  115.     {
  116.         return $this->dashboardDto->getTitle();
  117.     }
  118.     public function getDashboardFaviconPath(): string
  119.     {
  120.         return $this->dashboardDto->getFaviconPath();
  121.     }
  122.     public function getDashboardControllerFqcn(): string
  123.     {
  124.         return \get_class($this->dashboardControllerInstance);
  125.     }
  126.     public function getDashboardRouteName(): string
  127.     {
  128.         return $this->dashboardDto->getRouteName();
  129.     }
  130.     public function getDashboardContentWidth(): string
  131.     {
  132.         return $this->dashboardDto->getContentWidth();
  133.     }
  134.     public function getDashboardSidebarWidth(): string
  135.     {
  136.         return $this->dashboardDto->getSidebarWidth();
  137.     }
  138.     public function getDashboardHasDarkModeEnabled(): bool
  139.     {
  140.         return $this->dashboardDto->isDarkModeEnabled();
  141.     }
  142.     public function getDashboardDefaultColorScheme(): string
  143.     {
  144.         return $this->dashboardDto->getDefaultColorScheme();
  145.     }
  146.     /**
  147.      * @return LocaleDto[]
  148.      */
  149.     public function getDashboardLocales(): array
  150.     {
  151.         return $this->dashboardDto->getLocales();
  152.     }
  153.     public function getMainMenu(): MainMenuDto
  154.     {
  155.         if (null !== $this->mainMenuDto) {
  156.             return $this->mainMenuDto;
  157.         }
  158.         $configuredMenuItems $this->dashboardControllerInstance->configureMenuItems();
  159.         $mainMenuItems \is_array($configuredMenuItems) ? $configuredMenuItems iterator_to_array($configuredMenuItemsfalse);
  160.         return $this->mainMenuDto $this->menuFactory->createMainMenu($mainMenuItems);
  161.     }
  162.     public function getUserMenu(): UserMenuDto
  163.     {
  164.         if (null !== $this->userMenuDto) {
  165.             return $this->userMenuDto;
  166.         }
  167.         if (null === $this->user) {
  168.             return UserMenu::new()->getAsDto();
  169.         }
  170.         $userMenu $this->dashboardControllerInstance->configureUserMenu($this->user);
  171.         return $this->userMenuDto $this->menuFactory->createUserMenu($userMenu);
  172.     }
  173.     public function getCrud(): ?CrudDto
  174.     {
  175.         return $this->crudDto;
  176.     }
  177.     public function getSearch(): ?SearchDto
  178.     {
  179.         return $this->searchDto;
  180.     }
  181.     public function getTemplatePath(string $templateName): string
  182.     {
  183.         return $this->templateRegistry->get($templateName);
  184.     }
  185. }