Skip to content

Commit

Permalink
bug #3546 Added a compiler pass to create the controller registries (…
Browse files Browse the repository at this point in the history
…javiereguiluz)

This PR was merged into the 3.0.x-dev branch.

Discussion
----------

Added a compiler pass to create the controller registries

Fixes #3541.

Commits
-------

e650e53 Added a compiler pass to create the controller registries
  • Loading branch information
javiereguiluz committed Jul 13, 2020
2 parents 59c9077 + e650e53 commit 8de9d23
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 16 deletions.
51 changes: 51 additions & 0 deletions src/DependencyInjection/CreateControllerRegistriesPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace EasyCorp\Bundle\EasyAdminBundle\DependencyInjection;

use EasyCorp\Bundle\EasyAdminBundle\Registry\CrudControllerRegistry;
use EasyCorp\Bundle\EasyAdminBundle\Registry\DashboardControllerRegistry;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Creates the services of the Dashboard and CRUD controller registries. They can't
* be defined as normal services because they cause circular dependencies.
* See https://github.com/EasyCorp/EasyAdminBundle/issues/3541
*
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
*/
class CreateControllerRegistriesPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$this->createDashboardControllerRegistryService($container);
$this->createCrudControllerRegistryService($container);
}

private function createDashboardControllerRegistryService(ContainerBuilder $container): void
{
$dashboardControllersFqcn = array_keys($container->findTaggedServiceIds(EasyAdminExtension::TAG_DASHBOARD_CONTROLLER, true));

$container
->register(DashboardControllerRegistry::class, DashboardControllerRegistry::class)
->setPublic(false)
->setArguments([
$container->getParameter('kernel.secret'),
$container->getParameter('kernel.cache_dir'),
$dashboardControllersFqcn,
]);
}

private function createCrudControllerRegistryService(ContainerBuilder $container): void
{
$crudControllersFqcn = array_keys($container->findTaggedServiceIds(EasyAdminExtension::TAG_CRUD_CONTROLLER, true));

$container
->register(CrudControllerRegistry::class, CrudControllerRegistry::class)
->setPublic(false)
->setArguments([
$container->getParameter('kernel.secret'),
$crudControllersFqcn,
]);
}
}
8 changes: 8 additions & 0 deletions src/EasyAdminBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace EasyCorp\Bundle\EasyAdminBundle;

use EasyCorp\Bundle\EasyAdminBundle\DependencyInjection\CreateControllerRegistriesPass;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

/**
Expand All @@ -11,4 +14,9 @@ class EasyAdminBundle extends Bundle
{
public const VERSION = '3.1.1-DEV';
public const CONTEXT_ATTRIBUTE_NAME = 'easyadmin_context';

public function build(ContainerBuilder $container)
{
$container->addCompilerPass(new CreateControllerRegistriesPass(), PassConfig::TYPE_BEFORE_REMOVING);
}
}
7 changes: 3 additions & 4 deletions src/Registry/CrudControllerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ final class CrudControllerRegistry
/**
* @param CrudControllerInterface[] $crudControllers
*/
public function __construct(string $kernelSecret, iterable $crudControllers)
public function __construct(string $kernelSecret, array $crudControllersFqcn)
{
foreach (iterator_to_array($crudControllers, false) as $controller) {
$controllerFqcn = \get_class($controller);
$this->crudFqcnToEntityFqcnMap[$controllerFqcn] = $controller::getEntityFqcn();
foreach ($crudControllersFqcn as $controllerFqcn) {
$this->crudFqcnToEntityFqcnMap[$controllerFqcn] = $controllerFqcn::getEntityFqcn();
$this->crudFqcnToCrudIdMap[$controllerFqcn] = substr(sha1($kernelSecret.$controllerFqcn), 0, 7);
}

Expand Down
5 changes: 2 additions & 3 deletions src/Registry/DashboardControllerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ final class DashboardControllerRegistry
private $controllerFqcnToRouteMap = [];
private $routeToControllerFqcnMap = [];

public function __construct(string $kernelSecret, string $cacheDir, iterable $dashboardControllers)
public function __construct(string $kernelSecret, string $cacheDir, array $dashboardControllersFqcn)
{
foreach (iterator_to_array($dashboardControllers, false) as $controller) {
$controllerFqcn = \get_class($controller);
foreach ($dashboardControllersFqcn as $controllerFqcn) {
$this->controllerFqcnToContextIdMap[$controllerFqcn] = substr(sha1($kernelSecret.$controllerFqcn), 0, 7);
}

Expand Down
9 changes: 0 additions & 9 deletions src/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,6 @@
->arg(4, new Reference(CrudControllerRegistry::class))
->arg(5, new Reference(EntityFactory::class))

->set(DashboardControllerRegistry::class)
->arg(0, '%kernel.secret%')
->arg(1, '%kernel.cache_dir%')
->arg(2, tagged_iterator(EasyAdminExtension::TAG_DASHBOARD_CONTROLLER))

->set(CrudControllerRegistry::class)
->arg(0, '%kernel.secret%')
->arg(1, tagged_iterator(EasyAdminExtension::TAG_CRUD_CONTROLLER))

->set(CrudUrlGenerator::class)
->arg(0, new Reference(AdminContextProvider::class))
->arg(1, new Reference('router.default'))
Expand Down

0 comments on commit 8de9d23

Please sign in to comment.