From 1bb12d372b612d06f1e5270d70c1f4a143d6f302 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Fri, 19 Apr 2024 17:06:28 +0200 Subject: [PATCH] Make the "ea" global variable dynamic --- src/Context/AdminContext.php | 2 + src/Provider/AdminContextProvider.php | 136 +++++++++++++++++++++++++- src/Twig/EasyAdminTwigExtension.php | 5 +- 3 files changed, 137 insertions(+), 6 deletions(-) diff --git a/src/Context/AdminContext.php b/src/Context/AdminContext.php index 39cae836b5..139eeab4ad 100644 --- a/src/Context/AdminContext.php +++ b/src/Context/AdminContext.php @@ -23,6 +23,8 @@ /** * A context object that stores all the state and config of the current admin request. * + * Please duplicate any new methods added here in the AdminContextProvider class. + * * @author Javier Eguiluz */ final class AdminContext diff --git a/src/Provider/AdminContextProvider.php b/src/Provider/AdminContextProvider.php index 9111811277..2676cab7ba 100644 --- a/src/Provider/AdminContextProvider.php +++ b/src/Provider/AdminContextProvider.php @@ -4,7 +4,18 @@ use EasyCorp\Bundle\EasyAdminBundle\Config\Option\EA; use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext; +use EasyCorp\Bundle\EasyAdminBundle\Dto\AssetsDto; +use EasyCorp\Bundle\EasyAdminBundle\Dto\CrudDto; +use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto; +use EasyCorp\Bundle\EasyAdminBundle\Dto\I18nDto; +use EasyCorp\Bundle\EasyAdminBundle\Dto\LocaleDto; +use EasyCorp\Bundle\EasyAdminBundle\Dto\MainMenuDto; +use EasyCorp\Bundle\EasyAdminBundle\Dto\SearchDto; +use EasyCorp\Bundle\EasyAdminBundle\Dto\UserMenuDto; +use EasyCorp\Bundle\EasyAdminBundle\Registry\CrudControllerRegistry; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; +use Symfony\Component\Security\Core\User\UserInterface; /** * Inject this in services that need to get the admin context object. @@ -20,10 +31,131 @@ public function __construct(RequestStack $requestStack) $this->requestStack = $requestStack; } - public function getContext(): ?AdminContext + public function getContext(bool $throw = false): ?AdminContext { $currentRequest = $this->requestStack->getCurrentRequest(); - return null !== $currentRequest ? $currentRequest->get(EA::CONTEXT_REQUEST_ATTRIBUTE) : null; + if (null === $currentRequest) { + if ($throw) { + throw new \LogicException('Cannot use the EasyAdmin context: no request is not available.'); + } + + return null; + } + + return $currentRequest->get(EA::CONTEXT_REQUEST_ATTRIBUTE); + } + + public function getRequest(): Request + { + return $this->getContext(true)->getRequest(); + } + + public function getReferrer(): ?string + { + return $this->getContext(true)->getReferrer(); + } + + public function getI18n(): I18nDto + { + return $this->getContext(true)->getI18n(); + } + + public function getCrudControllers(): CrudControllerRegistry + { + return $this->getContext(true)->getCrudControllers(); + } + + public function getEntity(): EntityDto + { + return $this->getContext(true)->getEntity(); + } + + public function getUser(): ?UserInterface + { + return $this->getContext(true)->getUser(); + } + + public function getAssets(): AssetsDto + { + return $this->getContext(true)->getAssets(); + } + + public function getSignedUrls(): bool + { + return $this->getContext(true)->getSignedUrls(); + } + + public function getAbsoluteUrls(): bool + { + return $this->getContext(true)->getAbsoluteUrls(); + } + + public function getDashboardTitle(): string + { + return $this->getContext(true)->getDashboardTitle(); + } + + public function getDashboardFaviconPath(): string + { + return $this->getContext(true)->getDashboardFaviconPath(); + } + + public function getDashboardControllerFqcn(): string + { + return $this->getContext(true)->getDashboardControllerFqcn(); + } + + public function getDashboardRouteName(): string + { + return $this->getContext(true)->getDashboardRouteName(); + } + + public function getDashboardContentWidth(): string + { + return $this->getContext(true)->getDashboardContentWidth(); + } + + public function getDashboardSidebarWidth(): string + { + return $this->getContext(true)->getDashboardSidebarWidth(); + } + + public function getDashboardHasDarkModeEnabled(): bool + { + return $this->getContext(true)->getDashboardHasDarkModeEnabled(); + } + + /** + * @return LocaleDto[] + */ + public function getDashboardLocales(): array + { + return $this->getContext(true)->getDashboardLocales(); + } + + public function getMainMenu(): MainMenuDto + { + return $this->getContext(true)->getMainMenu(); + } + + public function getUserMenu(): UserMenuDto + { + return $this->getContext(true)->getUserMenu(); + } + + public function getCrud(): ?CrudDto + { + return $this->getContext(true)->getCrud(); + } + + public function getSearch(): ?SearchDto + { + return $this->getContext(true)->getSearch(); + } + + public function getTemplatePath(string $templateName): string + { + return $this->getContext(true)->getTemplatePath($templateName); } } diff --git a/src/Twig/EasyAdminTwigExtension.php b/src/Twig/EasyAdminTwigExtension.php index 865218a2f4..0675184ea2 100644 --- a/src/Twig/EasyAdminTwigExtension.php +++ b/src/Twig/EasyAdminTwigExtension.php @@ -67,10 +67,7 @@ public function getFilters(): array public function getGlobals(): array { - $context = $this->adminContextProvider->getContext(); - - // when there's an admin context, make it available in all templates as a short named variable - return null === $context ? [] : ['ea' => $context]; + return ['ea' => $this->adminContextProvider]; } /**