Skip to content

Commit

Permalink
Use __call
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Apr 19, 2024
1 parent 1bb12d3 commit e551e0d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 130 deletions.
2 changes: 0 additions & 2 deletions src/Context/AdminContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
/**
* 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 <javier.eguiluz@gmail.com>
*/
final class AdminContext
Expand Down
137 changes: 9 additions & 128 deletions src/Provider/AdminContextProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,7 @@

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 @@ -31,131 +20,23 @@ public function __construct(RequestStack $requestStack)
$this->requestStack = $requestStack;
}

public function getContext(bool $throw = false): ?AdminContext
public function getContext(): ?AdminContext
{
$currentRequest = $this->requestStack->getCurrentRequest();

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();
return null !== $currentRequest ? $currentRequest->get(EA::CONTEXT_REQUEST_ATTRIBUTE) : null;
}

public function getDashboardFaviconPath(): string
public function __call(string $name, array $arguments): mixed
{
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();
}
$currentRequest = $this->requestStack->getCurrentRequest();

public function getCrud(): ?CrudDto
{
return $this->getContext(true)->getCrud();
}
$context = $currentRequest?->get(EA::CONTEXT_REQUEST_ATTRIBUTE) ?? throw new \LogicException('Cannot use the EasyAdmin context: no request is not available.');

public function getSearch(): ?SearchDto
{
return $this->getContext(true)->getSearch();
}
if (!method_exists($context, $name)) {
throw new \BadMethodCallException(sprintf('Method "%s" does not exist in the EasyAdmin context.', $name));
}

public function getTemplatePath(string $templateName): string
{
return $this->getContext(true)->getTemplatePath($templateName);
return $context->$name(...$arguments);

Check failure on line 40 in src/Provider/AdminContextProvider.php

View workflow job for this annotation

GitHub Actions / phpstan

Variable method call on mixed.
}
}

0 comments on commit e551e0d

Please sign in to comment.