mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
use service subscriber injection to inject manager registry
This commit is contained in:
parent
5703fd0046
commit
a63b40fb6c
@ -19,16 +19,40 @@ use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
|
|||||||
use Chill\MainBundle\Templating\Listing\FilterOrderHelper;
|
use Chill\MainBundle\Templating\Listing\FilterOrderHelper;
|
||||||
use Chill\MainBundle\Templating\Listing\FilterOrderHelperFactoryInterface;
|
use Chill\MainBundle\Templating\Listing\FilterOrderHelperFactoryInterface;
|
||||||
use Doctrine\ORM\QueryBuilder;
|
use Doctrine\ORM\QueryBuilder;
|
||||||
|
use Doctrine\Persistence\ManagerRegistry;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||||
use Symfony\Component\Form\FormInterface;
|
use Symfony\Component\Form\FormInterface;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||||
use Symfony\Component\Security\Core\Role\Role;
|
|
||||||
use Symfony\Component\Serializer\SerializerInterface;
|
use Symfony\Component\Serializer\SerializerInterface;
|
||||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The CRUDController class is an abstract class that provides basic CRUD (Create, Read, Update, Delete) functionality for entities in a Symfony application. It extends the Abstract
|
||||||
|
*Controller class.
|
||||||
|
*
|
||||||
|
* For the class \Chill\MainBundle\CRUD\Controller\CRUDController, the dependency injection system does not work as in
|
||||||
|
* the usual way, where dependencies are provided via the constructor method. Instead, it utilizes the capabilities
|
||||||
|
* provided by the \Symfony\Contracts\Service\ServiceSubscriberInterface.
|
||||||
|
*
|
||||||
|
* The \Symfony\Contracts\Service\ServiceSubscriberInterface is an interface that is used by objects needing explicit
|
||||||
|
* service references. It means this class has a direct dependency on the Symfony service container, allowing it to
|
||||||
|
* request specific services it needs for execution.
|
||||||
|
*
|
||||||
|
* By implementing the ServiceSubscriberInterface, this class effectively defines a method called getSubscribedServices,
|
||||||
|
* which returns an array mapping service IDs to their classes or interfaces. Symfony's service container uses this
|
||||||
|
* method to preconfigure the services required, optimizing the dependency injection process.
|
||||||
|
*
|
||||||
|
* This model can be used to keep the class itself concise by preventing the need to define a constructor and inject
|
||||||
|
* services through method calls. This approach also allows for more flexible service usage, as services can be fetched
|
||||||
|
* as and when required, rather than all services being loaded whether they are used or not. This makes for a more
|
||||||
|
* efficient and maintainable solution especially when dealing with larger services.
|
||||||
|
*
|
||||||
|
* For more details about how this works, you can refer to Symfony's Documentation
|
||||||
|
* on Service Subscribers & Service Locators: https://symfony.com/doc/current/service_container/service_subscribers_locators.html
|
||||||
|
*/
|
||||||
class CRUDController extends AbstractController
|
class CRUDController extends AbstractController
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
@ -37,9 +61,6 @@ class CRUDController extends AbstractController
|
|||||||
* This configuration si defined by `chill_main['crud']`.
|
* This configuration si defined by `chill_main['crud']`.
|
||||||
*/
|
*/
|
||||||
protected array $crudConfig;
|
protected array $crudConfig;
|
||||||
public function __construct(private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public function CRUD(?string $parameter): Response
|
public function CRUD(?string $parameter): Response
|
||||||
{
|
{
|
||||||
@ -81,6 +102,7 @@ class CRUDController extends AbstractController
|
|||||||
Resolver::class => Resolver::class,
|
Resolver::class => Resolver::class,
|
||||||
SerializerInterface::class => SerializerInterface::class,
|
SerializerInterface::class => SerializerInterface::class,
|
||||||
FilterOrderHelperFactoryInterface::class => FilterOrderHelperFactoryInterface::class,
|
FilterOrderHelperFactoryInterface::class => FilterOrderHelperFactoryInterface::class,
|
||||||
|
ManagerRegistry::class => ManagerRegistry::class,
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@ -153,7 +175,7 @@ class CRUDController extends AbstractController
|
|||||||
*/
|
*/
|
||||||
protected function buildQueryEntities(string $action, Request $request)
|
protected function buildQueryEntities(string $action, Request $request)
|
||||||
{
|
{
|
||||||
$query = $this->managerRegistry
|
$query = $this->getManagerRegistry()
|
||||||
->getManager()
|
->getManager()
|
||||||
->createQueryBuilder()
|
->createQueryBuilder()
|
||||||
->select('e')
|
->select('e')
|
||||||
@ -267,7 +289,7 @@ class CRUDController extends AbstractController
|
|||||||
|
|
||||||
if ($form->isSubmitted() && $form->isValid()) {
|
if ($form->isSubmitted() && $form->isValid()) {
|
||||||
$this->onFormValid($action, $entity, $form, $request);
|
$this->onFormValid($action, $entity, $form, $request);
|
||||||
$em = $this->managerRegistry->getManager();
|
$em = $this->getManagerRegistry()->getManager();
|
||||||
|
|
||||||
$this->onPreRemove($action, $entity, $form, $request);
|
$this->onPreRemove($action, $entity, $form, $request);
|
||||||
$this->removeEntity($action, $entity, $form, $request);
|
$this->removeEntity($action, $entity, $form, $request);
|
||||||
@ -312,7 +334,7 @@ class CRUDController extends AbstractController
|
|||||||
$id = $request->query->get('duplicate_id', 0);
|
$id = $request->query->get('duplicate_id', 0);
|
||||||
$originalEntity = $this->getEntity($action, $id, $request);
|
$originalEntity = $this->getEntity($action, $id, $request);
|
||||||
|
|
||||||
$this->managerRegistry->getManager()
|
$this->getManagerRegistry()->getManager()
|
||||||
->detach($originalEntity);
|
->detach($originalEntity);
|
||||||
|
|
||||||
return $originalEntity;
|
return $originalEntity;
|
||||||
@ -384,7 +406,7 @@ class CRUDController extends AbstractController
|
|||||||
|
|
||||||
if ($form->isSubmitted() && $form->isValid()) {
|
if ($form->isSubmitted() && $form->isValid()) {
|
||||||
$this->onFormValid($action, $entity, $form, $request);
|
$this->onFormValid($action, $entity, $form, $request);
|
||||||
$em = $this->managerRegistry->getManager();
|
$em = $this->getManagerRegistry()->getManager();
|
||||||
|
|
||||||
$this->onPrePersist($action, $entity, $form, $request);
|
$this->onPrePersist($action, $entity, $form, $request);
|
||||||
$em->persist($entity);
|
$em->persist($entity);
|
||||||
@ -487,7 +509,7 @@ class CRUDController extends AbstractController
|
|||||||
|
|
||||||
if ($form->isSubmitted() && $form->isValid()) {
|
if ($form->isSubmitted() && $form->isValid()) {
|
||||||
$this->onFormValid($action, $entity, $form, $request);
|
$this->onFormValid($action, $entity, $form, $request);
|
||||||
$em = $this->managerRegistry->getManager();
|
$em = $this->getManagerRegistry()->getManager();
|
||||||
|
|
||||||
$this->onPreFlush($action, $entity, $form, $request);
|
$this->onPreFlush($action, $entity, $form, $request);
|
||||||
$em->flush();
|
$em->flush();
|
||||||
@ -601,7 +623,7 @@ class CRUDController extends AbstractController
|
|||||||
*/
|
*/
|
||||||
protected function getEntity(mixed $action, $id, Request $request): ?object
|
protected function getEntity(mixed $action, $id, Request $request): ?object
|
||||||
{
|
{
|
||||||
return $this->managerRegistry
|
return $this->getManagerRegistry()
|
||||||
->getRepository($this->getEntityClass())
|
->getRepository($this->getEntityClass())
|
||||||
->find($id);
|
->find($id);
|
||||||
}
|
}
|
||||||
@ -650,6 +672,11 @@ class CRUDController extends AbstractController
|
|||||||
return $this->container->get('chill_main.paginator_factory');
|
return $this->container->get('chill_main.paginator_factory');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function getManagerRegistry(): ManagerRegistry
|
||||||
|
{
|
||||||
|
return $this->container->get(ManagerRegistry::class);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the result of the query.
|
* Get the result of the query.
|
||||||
*/
|
*/
|
||||||
@ -920,7 +947,7 @@ class CRUDController extends AbstractController
|
|||||||
|
|
||||||
protected function removeEntity(string $action, $entity, FormInterface $form, Request $request)
|
protected function removeEntity(string $action, $entity, FormInterface $form, Request $request)
|
||||||
{
|
{
|
||||||
$this->managerRegistry
|
$this->getManagerRegistry()
|
||||||
->getManager()
|
->getManager()
|
||||||
->remove($entity);
|
->remove($entity);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user