From c04ccef4cdf42059cb05deb25fb63f36bcf7e0b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 22 Jun 2021 14:40:56 +0200 Subject: [PATCH] improve loading for existing crud cotnroller --- .../CompilerPass/CRUDControllerCompilerPass.php | 13 ++++++++++--- .../CRUD/Routing/CRUDRoutesLoader.php | 2 +- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Bundle/ChillMainBundle/CRUD/CompilerPass/CRUDControllerCompilerPass.php b/src/Bundle/ChillMainBundle/CRUD/CompilerPass/CRUDControllerCompilerPass.php index f84628ccd..11b3c34fd 100644 --- a/src/Bundle/ChillMainBundle/CRUD/CompilerPass/CRUDControllerCompilerPass.php +++ b/src/Bundle/ChillMainBundle/CRUD/CompilerPass/CRUDControllerCompilerPass.php @@ -50,12 +50,21 @@ class CRUDControllerCompilerPass implements CompilerPassInterface private function configureCrudController(ContainerBuilder $container, array $crudEntry, string $apiOrCrud): void { $controllerClass = $crudEntry['controller']; - $controllerServiceName = 'cs'.$apiOrCrud.'_'.$crudEntry['name'].'_controller'; + // create config parameter in container + $param = 'chill_main_'.$apiOrCrud.'_config_'.$crudEntry['name']; + $container->setParameter($param, $crudEntry); + if ($container->hasDefinition($controllerClass)) { + // create an alias to not to re-create the service $alias = new Alias($controllerClass, true); $container->setAlias($controllerServiceName, $alias); + + // add the "addMethodCall" + $container->getDefinition($controllerClass) + ->addMethodCall('setCrudConfig', ['%'.$param.'%']); + } else { $controller = new Definition($controllerClass); @@ -63,8 +72,6 @@ class CRUDControllerCompilerPass implements CompilerPassInterface $controller->setAutoconfigured(true); $controller->setPublic(true); - $param = 'chill_main_'.$apiOrCrud.'_config_'.$crudEntry['name']; - $container->setParameter($param, $crudEntry); $controller->addMethodCall('setCrudConfig', ['%'.$param.'%']); $container->setDefinition($controllerServiceName, $controller); diff --git a/src/Bundle/ChillMainBundle/CRUD/Routing/CRUDRoutesLoader.php b/src/Bundle/ChillMainBundle/CRUD/Routing/CRUDRoutesLoader.php index d7e19ea9c..36aadb8b7 100644 --- a/src/Bundle/ChillMainBundle/CRUD/Routing/CRUDRoutesLoader.php +++ b/src/Bundle/ChillMainBundle/CRUD/Routing/CRUDRoutesLoader.php @@ -142,7 +142,7 @@ class CRUDRoutesLoader extends Loader protected function loadApi(array $crudConfig): RouteCollection { $collection = new RouteCollection(); - $controller ='csapi_'.$crudConfig['name'].'_controller'; + $controller = 'csapi_'.$crudConfig['name'].'_controller'; foreach ($crudConfig['actions'] as $name => $action) { // filter only on single actions