Skip to content

Commit

Permalink
Make the "ea" global variable dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Apr 19, 2024
1 parent 2a26e7e commit 1bb12d3
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/Context/AdminContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>
*/
final class AdminContext
Expand Down
136 changes: 134 additions & 2 deletions src/Provider/AdminContextProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}
}
5 changes: 1 addition & 4 deletions src/Twig/EasyAdminTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

/**
Expand Down

0 comments on commit 1bb12d3

Please sign in to comment.