vendor/easycorp/easyadmin-bundle/src/Provider/AdminContextProvider.php line 149

Open in your IDE?
  1. <?php
  2. namespace EasyCorp\Bundle\EasyAdminBundle\Provider;
  3. use EasyCorp\Bundle\EasyAdminBundle\Config\Option\EA;
  4. use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext;
  5. use EasyCorp\Bundle\EasyAdminBundle\Dto\AssetsDto;
  6. use EasyCorp\Bundle\EasyAdminBundle\Dto\CrudDto;
  7. use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
  8. use EasyCorp\Bundle\EasyAdminBundle\Dto\I18nDto;
  9. use EasyCorp\Bundle\EasyAdminBundle\Dto\LocaleDto;
  10. use EasyCorp\Bundle\EasyAdminBundle\Dto\MainMenuDto;
  11. use EasyCorp\Bundle\EasyAdminBundle\Dto\SearchDto;
  12. use EasyCorp\Bundle\EasyAdminBundle\Dto\UserMenuDto;
  13. use EasyCorp\Bundle\EasyAdminBundle\Registry\CrudControllerRegistry;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\RequestStack;
  16. use Symfony\Component\Security\Core\User\UserInterface;
  17. /**
  18.  * Inject this in services that need to get the admin context object.
  19.  *
  20.  * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  21.  */
  22. final class AdminContextProvider
  23. {
  24.     private RequestStack $requestStack;
  25.     public function __construct(RequestStack $requestStack)
  26.     {
  27.         $this->requestStack $requestStack;
  28.     }
  29.     public function hasContext(): bool
  30.     {
  31.         $currentRequest $this->requestStack->getCurrentRequest();
  32.         return null !== $currentRequest && $currentRequest->attributes->has(EA::CONTEXT_REQUEST_ATTRIBUTE);
  33.     }
  34.     public function getContext(bool $throw false): ?AdminContext
  35.     {
  36.         $currentRequest $this->requestStack->getCurrentRequest();
  37.         if (null === $currentRequest) {
  38.             if ($throw) {
  39.                 throw new \LogicException('Cannot use the EasyAdmin context: no request is available.');
  40.             }
  41.             return null;
  42.         }
  43.         return $currentRequest->get(EA::CONTEXT_REQUEST_ATTRIBUTE);
  44.     }
  45.     public function getRequest(): Request
  46.     {
  47.         return $this->getContext(true)->getRequest();
  48.     }
  49.     public function getReferrer(): ?string
  50.     {
  51.         return $this->getContext(true)->getReferrer();
  52.     }
  53.     public function getI18n(): I18nDto
  54.     {
  55.         return $this->getContext(true)->getI18n();
  56.     }
  57.     public function getCrudControllers(): CrudControllerRegistry
  58.     {
  59.         return $this->getContext(true)->getCrudControllers();
  60.     }
  61.     public function getEntity(): EntityDto
  62.     {
  63.         return $this->getContext(true)->getEntity();
  64.     }
  65.     public function getUser(): ?UserInterface
  66.     {
  67.         return $this->getContext(true)->getUser();
  68.     }
  69.     public function getAssets(): AssetsDto
  70.     {
  71.         return $this->getContext(true)->getAssets();
  72.     }
  73.     public function getSignedUrls(): bool
  74.     {
  75.         return $this->getContext(true)->getSignedUrls();
  76.     }
  77.     public function getAbsoluteUrls(): bool
  78.     {
  79.         return $this->getContext(true)->getAbsoluteUrls();
  80.     }
  81.     public function getDashboardTitle(): string
  82.     {
  83.         return $this->getContext(true)->getDashboardTitle();
  84.     }
  85.     public function getDashboardFaviconPath(): string
  86.     {
  87.         return $this->getContext(true)->getDashboardFaviconPath();
  88.     }
  89.     public function getDashboardControllerFqcn(): string
  90.     {
  91.         return $this->getContext(true)->getDashboardControllerFqcn();
  92.     }
  93.     public function getDashboardRouteName(): string
  94.     {
  95.         return $this->getContext(true)->getDashboardRouteName();
  96.     }
  97.     public function getDashboardContentWidth(): string
  98.     {
  99.         return $this->getContext(true)->getDashboardContentWidth();
  100.     }
  101.     public function getDashboardSidebarWidth(): string
  102.     {
  103.         return $this->getContext(true)->getDashboardSidebarWidth();
  104.     }
  105.     public function getDashboardHasDarkModeEnabled(): bool
  106.     {
  107.         return $this->getContext(true)->getDashboardHasDarkModeEnabled();
  108.     }
  109.     public function getDashboardDefaultColorScheme(): string
  110.     {
  111.         return $this->getContext(true)->getDashboardDefaultColorScheme();
  112.     }
  113.     /**
  114.      * @return LocaleDto[]
  115.      */
  116.     public function getDashboardLocales(): array
  117.     {
  118.         return $this->getContext(true)->getDashboardLocales();
  119.     }
  120.     public function getMainMenu(): MainMenuDto
  121.     {
  122.         return $this->getContext(true)->getMainMenu();
  123.     }
  124.     public function getUserMenu(): UserMenuDto
  125.     {
  126.         return $this->getContext(true)->getUserMenu();
  127.     }
  128.     public function getCrud(): ?CrudDto
  129.     {
  130.         return $this->getContext(true)->getCrud();
  131.     }
  132.     public function getSearch(): ?SearchDto
  133.     {
  134.         return $this->getContext(true)->getSearch();
  135.     }
  136.     public function getTemplatePath(string $templateName): string
  137.     {
  138.         return $this->getContext(true)->getTemplatePath($templateName);
  139.     }
  140.     public function usePrettyUrls(): bool
  141.     {
  142.         return $this->getContext(true)->usePrettyUrls();
  143.     }
  144. }