Skip to content

fix: get controllers fqcn for route load #6883

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: 4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@
->tag('cache.pool')

->set(AdminRouteGenerator::class)
->arg(0, tagged_iterator(EasyAdminExtension::TAG_DASHBOARD_CONTROLLER))
->arg(1, tagged_iterator(EasyAdminExtension::TAG_CRUD_CONTROLLER))
->arg(0, abstract_arg(sprintf('class list of %s tag', EasyAdminExtension::TAG_DASHBOARD_CONTROLLER)))
->arg(1, abstract_arg(sprintf('class list of %s tag', EasyAdminExtension::TAG_CRUD_CONTROLLER)))
->arg(2, service('cache.easyadmin'))
->arg(3, service('filesystem'))
->arg(4, '%kernel.build_dir%')
Expand Down
35 changes: 35 additions & 0 deletions src/DependencyInjection/AdminRoutePass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace EasyCorp\Bundle\EasyAdminBundle\DependencyInjection;

use EasyCorp\Bundle\EasyAdminBundle\Router\AdminRouteGenerator;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* @author Indra Gunawan <[email protected]>
*/
class AdminRoutePass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
$dashboardControllersFqcn = array_map(
static function ($serviceId) use ($container) {
return $container->getDefinition($serviceId)->getClass();
},
array_keys($container->findTaggedServiceIds(EasyAdminExtension::TAG_DASHBOARD_CONTROLLER, true))
);

$crudControllersFqcn = array_map(
static function ($serviceId) use ($container) {
return $container->getDefinition($serviceId)->getClass();
},
array_keys($container->findTaggedServiceIds(EasyAdminExtension::TAG_CRUD_CONTROLLER, true))
);

$container->getDefinition(AdminRouteGenerator::class)
->setArgument(0, $dashboardControllersFqcn)
->setArgument(1, $crudControllersFqcn)
;
}
}
2 changes: 2 additions & 0 deletions src/EasyAdminBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace EasyCorp\Bundle\EasyAdminBundle;

use EasyCorp\Bundle\EasyAdminBundle\DependencyInjection\AdminRoutePass;
use EasyCorp\Bundle\EasyAdminBundle\DependencyInjection\CreateControllerRegistriesPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
Expand All @@ -16,6 +17,7 @@ class EasyAdminBundle extends Bundle
public function build(ContainerBuilder $container): void
{
$container->addCompilerPass(new CreateControllerRegistriesPass());
$container->addCompilerPass(new AdminRoutePass());
}

public function getPath(): string
Expand Down
6 changes: 1 addition & 5 deletions src/Factory/ControllerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ private function getController(string $controllerInterface, ?string $controllerF
try {
$controllerCallable = $this->controllerResolver->getController($newRequest);
} catch (\InvalidArgumentException $e) {
$controllerCallable = false;
}

if (false === $controllerCallable) {
throw new NotFoundHttpException(sprintf('Unable to find the controller "%s::%s".', $controllerFqcn, $controllerAction));
throw new NotFoundHttpException(sprintf('Unable to find the controller "%s::%s".', $controllerFqcn, $controllerAction), $e);
}

if (!\is_array($controllerCallable)) {
Expand Down
18 changes: 7 additions & 11 deletions src/Router/AdminRouteGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ final class AdminRouteGenerator implements AdminRouteGeneratorInterface
private ?bool $applicationUsesPrettyUrls = null;

public function __construct(
private iterable $dashboardControllers,
private iterable $crudControllers,
private array $dashboardControllersFqcn,
private array $crudControllersFqcn,
private CacheItemPoolInterface $cache,
private Filesystem $filesystem,
private string $buildDir,
Expand Down Expand Up @@ -100,8 +100,7 @@ public function findRouteName(?string $dashboardFqcn = null, ?string $crudContro
$adminRoutes = $this->cache->getItem(self::CACHE_KEY_FQCN_TO_ROUTE)->get();

if (null === $dashboardFqcn) {
$dashboardControllers = iterator_to_array($this->dashboardControllers);
$dashboardFqcn = $dashboardControllers[array_key_first($dashboardControllers)]::class;
$dashboardFqcn = reset($this->dashboardControllersFqcn);
}

return $adminRoutes[$dashboardFqcn][$crudControllerFqcn ?? ''][$actionName ?? ''] ?? null;
Expand All @@ -117,8 +116,7 @@ private function generateAdminRoutes(): array
/** @var array<string> $addedRouteNames Temporary cache that stores the route names to ensure that we don't add duplicated admin routes */
$addedRouteNames = [];

foreach ($this->dashboardControllers as $dashboardController) {
$dashboardFqcn = $dashboardController::class;
foreach ($this->dashboardControllersFqcn as $dashboardFqcn) {
[$allowedCrudControllers, $deniedCrudControllers] = $this->getAllowedAndDeniedControllers($dashboardFqcn);
$defaultRoutesConfig = $this->getDefaultRoutesConfig($dashboardFqcn);
$dashboardRouteConfig = $this->getDashboardsRouteConfig()[$dashboardFqcn];
Expand All @@ -130,9 +128,7 @@ private function generateAdminRoutes(): array
}

// then, create the routes of the CRUD controllers associated with the dashboard
foreach ($this->crudControllers as $crudController) {
$crudControllerFqcn = $crudController::class;

foreach ($this->crudControllersFqcn as $crudControllerFqcn) {
if (null !== $allowedCrudControllers && !\in_array($crudControllerFqcn, $allowedCrudControllers, true)) {
continue;
}
Expand Down Expand Up @@ -231,8 +227,8 @@ private function getDashboardsRouteConfig(): array
{
$config = [];

foreach ($this->dashboardControllers as $dashboardController) {
$reflectionClass = new \ReflectionClass($dashboardController);
foreach ($this->dashboardControllersFqcn as $dashboardControllerFqcn) {
$reflectionClass = new \ReflectionClass($dashboardControllerFqcn);

// first, check if the dashboard uses the #[AdminDashboard] attribute to define its route configuration;
// this is the recommended way for modern EasyAdmin applications
Expand Down
11 changes: 5 additions & 6 deletions tests/Router/AdminRouteGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\DependencyInjection\Argument\RewindableGenerator;
use Symfony\Component\Filesystem\Filesystem;

class AdminRouteGeneratorTest extends WebTestCase
Expand Down Expand Up @@ -52,13 +51,13 @@ public function testFindRoute(?string $dashboardControllerFqcn, ?string $crudCon
return $item;
});

$dashboardControllers = new RewindableGenerator(function () {
yield DashboardController::class => new DashboardController();
yield SecondDashboardController::class => new SecondDashboardController();
}, 2);
$dashboardControllersFqcn = [
DashboardController::class,
SecondDashboardController::class,
];

$adminRouteGenerator = new AdminRouteGenerator(
$dashboardControllers,
$dashboardControllersFqcn,
[],
$cacheMock,
new Filesystem(),
Expand Down