sf4 depreciation: repairing crud custom routes loader

This commit is contained in:
Mathieu Jaumotte 2021-02-02 11:48:19 +01:00
parent 42c2ac48d2
commit 0b8a22ae46
4 changed files with 173 additions and 13 deletions

View File

@ -36,8 +36,9 @@ use Chill\MainBundle\Pagination\PaginatorInterface;
use Chill\MainBundle\CRUD\Form\CRUDDeleteEntityForm; use Chill\MainBundle\CRUD\Form\CRUDDeleteEntityForm;
/** /**
* * Class CRUDController
* *
* @package Chill\MainBundle\CRUD\Controller
*/ */
class CRUDController extends AbstractController class CRUDController extends AbstractController
{ {
@ -49,17 +50,41 @@ class CRUDController extends AbstractController
* @var array * @var array
*/ */
protected $crudConfig; protected $crudConfig;
/**
* @param array $config
*/
public function setCrudConfig(array $config) public function setCrudConfig(array $config)
{ {
$this->crudConfig = $config; $this->crudConfig = $config;
} }
/**
* @param $parameter
* @return Response
*/
public function CRUD($parameter)
{
return new Response($parameter);
}
/**
* @param Request $request
* @param $id
* @return \Symfony\Component\HttpFoundation\RedirectResponse|Response
*/
public function delete(Request $request, $id) public function delete(Request $request, $id)
{ {
return $this->deleteAction('delete', $request, $id); return $this->deleteAction('delete', $request, $id);
} }
/**
* @param string $action
* @param Request $request
* @param $id
* @param null $formClass
* @return null|\Symfony\Component\HttpFoundation\RedirectResponse|Response|void
*/
protected function deleteAction(string $action, Request $request, $id, $formClass = null) protected function deleteAction(string $action, Request $request, $id, $formClass = null)
{ {
$this->onPreDelete($action, $request, $id); $this->onPreDelete($action, $request, $id);
@ -129,12 +154,34 @@ class CRUDController extends AbstractController
); );
} }
/**
* @param string $action
* @param Request $request
*/
protected function onPreDelete(string $action, Request $request) {} protected function onPreDelete(string $action, Request $request) {}
/**
* @param string $action
* @param $entity
* @param FormInterface $form
* @param Request $request
*/
protected function onPreRemove(string $action, $entity, FormInterface $form, Request $request) {} protected function onPreRemove(string $action, $entity, FormInterface $form, Request $request) {}
/**
* @param string $action
* @param $entity
* @param FormInterface $form
* @param Request $request
*/
protected function onPostRemove(string $action, $entity, FormInterface $form, Request $request) {} protected function onPostRemove(string $action, $entity, FormInterface $form, Request $request) {}
/**
* @param string $action
* @param $entity
* @param FormInterface $form
* @param Request $request
*/
protected function removeEntity(string $action, $entity, FormInterface $form, Request $request) protected function removeEntity(string $action, $entity, FormInterface $form, Request $request)
{ {
$this->getDoctrine() $this->getDoctrine()
@ -238,7 +285,6 @@ class CRUDController extends AbstractController
} }
/** /**
*
* @param string $action * @param string $action
* @param Request $request * @param Request $request
*/ */
@ -941,37 +987,84 @@ class CRUDController extends AbstractController
} }
} }
/**
* @param $action
* @param $entity
* @param Request $request
* @return bool
*/
protected function hasCustomTemplate($action, $entity, Request $request): bool protected function hasCustomTemplate($action, $entity, Request $request): bool
{ {
return !empty($this->getActionConfig($action)['template']); return !empty($this->getActionConfig($action)['template']);
} }
/**
* @param string $action
* @param $entity
* @param FormInterface $form
* @param Request $request
*/
protected function onPreFlush(string $action, $entity, FormInterface $form, Request $request) protected function onPreFlush(string $action, $entity, FormInterface $form, Request $request)
{ {
} }
/**
* @param string $action
* @param $entity
* @param FormInterface $form
* @param Request $request
*/
protected function onPostFlush(string $action, $entity, FormInterface $form, Request $request) protected function onPostFlush(string $action, $entity, FormInterface $form, Request $request)
{ {
} }
/**
* @param string $action
* @param $entity
* @param FormInterface $form
* @param Request $request
*/
protected function onPrePersist(string $action, $entity, FormInterface $form, Request $request) protected function onPrePersist(string $action, $entity, FormInterface $form, Request $request)
{ {
} }
/**
* @param string $action
* @param $entity
* @param FormInterface $form
* @param Request $request
*/
protected function onPostPersist(string $action, $entity, FormInterface $form, Request $request) protected function onPostPersist(string $action, $entity, FormInterface $form, Request $request)
{ {
} }
/**
* @param $action
* @param Request $request
* @param $entity
* @return null|Response
*/
protected function onPostFetchEntity($action, Request $request, $entity): ?Response protected function onPostFetchEntity($action, Request $request, $entity): ?Response
{ {
return null; return null;
} }
/**
* @param $action
* @param Request $request
* @param $entity
* @return null|Response
*/
protected function onPostCheckACL($action, Request $request, $entity): ?Response protected function onPostCheckACL($action, Request $request, $entity): ?Response
{ {
return null; return null;
} }
/**
* @param object $entity
* @param FormInterface $form
* @param Request $request
*/
protected function onFormValid(object $entity, FormInterface $form, Request $request) protected function onFormValid(object $entity, FormInterface $form, Request $request)
{ {
} }
@ -1007,26 +1100,44 @@ class CRUDController extends AbstractController
} }
} }
/**
* @param string $action
* @return mixed
*/
protected function getActionConfig(string $action) protected function getActionConfig(string $action)
{ {
return $this->crudConfig['actions'][$action]; return $this->crudConfig['actions'][$action];
} }
/**
* @return PaginatorFactory
*/
protected function getPaginatorFactory(): PaginatorFactory protected function getPaginatorFactory(): PaginatorFactory
{ {
return $this->get(PaginatorFactory::class); return $this->get(PaginatorFactory::class);
} }
/**
* @return TranslatorInterface
*/
protected function getTranslator(): TranslatorInterface protected function getTranslator(): TranslatorInterface
{ {
return $this->container->get('translator'); return $this->container->get('translator');
} }
/**
* @return AuthorizationHelper
*/
protected function getAuthorizationHelper(): AuthorizationHelper protected function getAuthorizationHelper(): AuthorizationHelper
{ {
return $this->container->get(AuthorizationHelper::class); return $this->container->get(AuthorizationHelper::class);
} }
/**
* @param Role $role
* @param Scope|null $scope
* @return \Chill\MainBundle\Entity\Center[]
*/
protected function getReachableCenters(Role $role, Scope $scope = null) protected function getReachableCenters(Role $role, Scope $scope = null)
{ {
return $this->getAuthorizationHelper() return $this->getAuthorizationHelper()
@ -1034,16 +1145,25 @@ class CRUDController extends AbstractController
; ;
} }
/**
* @return EventDispatcherInterface
*/
protected function getEventDispatcher(): EventDispatcherInterface protected function getEventDispatcher(): EventDispatcherInterface
{ {
return $this->get(EventDispatcherInterface::class); return $this->get(EventDispatcherInterface::class);
} }
/**
* @return Resolver
*/
protected function getCrudResolver(): Resolver protected function getCrudResolver(): Resolver
{ {
return $this->get(Resolver::class); return $this->get(Resolver::class);
} }
/**
* @return array
*/
public static function getSubscribedServices() public static function getSubscribedServices()
{ {
return \array_merge( return \array_merge(

View File

@ -20,25 +20,58 @@
namespace Chill\MainBundle\CRUD\Routing; namespace Chill\MainBundle\CRUD\Routing;
use Symfony\Component\Config\Loader\Loader;
use Symfony\Component\Routing\Route; use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\RouteCollection;
/** /**
* Class CRUDRoutesLoader
* Load the route for CRUD * Load the route for CRUD
* *
* @package Chill\MainBundle\CRUD\Routing
*/ */
class CRUDRoutesLoader class CRUDRoutesLoader extends Loader
{ {
/**
* @var array
*/
protected $config = []; protected $config = [];
/**
* @var bool
*/
private $isLoaded = false;
/**
* CRUDRoutesLoader constructor.
*
* @param $config
*/
public function __construct($config) public function __construct($config)
{ {
$this->config = $config; $this->config = $config;
} }
public function load() /**
* @param mixed $resource
* @param null $type
* @return bool
*/
public function supports($resource, $type = null)
{ {
return 'CRUD' === $type;
}
/**
* @return RouteCollection
*/
public function load($resource, $type = null)
{
if (true === $this->isLoaded) {
throw new \RuntimeException('Do not add the "CRUD" loader twice');
}
$collection = new RouteCollection(); $collection = new RouteCollection();
foreach ($this->config as $config) { foreach ($this->config as $config) {
@ -48,6 +81,10 @@ class CRUDRoutesLoader
return $collection; return $collection;
} }
/**
* @param $config
* @return RouteCollection
*/
protected function loadConfig($config): RouteCollection protected function loadConfig($config): RouteCollection
{ {
$collection = new RouteCollection(); $collection = new RouteCollection();

View File

@ -30,6 +30,9 @@ chill_password_recover:
resource: "@ChillMainBundle/config/routes/password_recover.yaml" resource: "@ChillMainBundle/config/routes/password_recover.yaml"
prefix: "public/{_locale}/password" prefix: "public/{_locale}/password"
chill_crud:
resource: "@ChillMainBundle"
type: CRUD
root: root:
path: / path: /

View File

@ -1,8 +1,8 @@
services: services:
# Chill\MainBundle\CRUD\Routing\CRUDRoutesLoader: Chill\MainBundle\CRUD\Routing\CRUDRoutesLoader:
# arguments:
# tags: $config: '%chill_main_crud_route_loader_config%'
# - routing.loader tags: [ routing.loader ]
Chill\MainBundle\CRUD\Resolver\Resolver: Chill\MainBundle\CRUD\Resolver\Resolver:
arguments: arguments: