diff --git a/src/Bundle/ChillMainBundle/CRUD/Controller/CRUDController.php b/src/Bundle/ChillMainBundle/CRUD/Controller/CRUDController.php index 18dedebf5..092afe4ec 100644 --- a/src/Bundle/ChillMainBundle/CRUD/Controller/CRUDController.php +++ b/src/Bundle/ChillMainBundle/CRUD/Controller/CRUDController.php @@ -19,16 +19,40 @@ use Chill\MainBundle\Security\Authorization\AuthorizationHelper; use Chill\MainBundle\Templating\Listing\FilterOrderHelper; use Chill\MainBundle\Templating\Listing\FilterOrderHelperFactoryInterface; use Doctrine\ORM\QueryBuilder; +use Doctrine\Persistence\ManagerRegistry; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\Form\FormInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; -use Symfony\Component\Security\Core\Role\Role; use Symfony\Component\Serializer\SerializerInterface; 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 { /** @@ -37,9 +61,6 @@ class CRUDController extends AbstractController * This configuration si defined by `chill_main['crud']`. */ protected array $crudConfig; - public function __construct(private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry) - { - } public function CRUD(?string $parameter): Response { @@ -81,6 +102,7 @@ class CRUDController extends AbstractController Resolver::class => Resolver::class, SerializerInterface::class => SerializerInterface::class, FilterOrderHelperFactoryInterface::class => FilterOrderHelperFactoryInterface::class, + ManagerRegistry::class => ManagerRegistry::class, ] ); } @@ -153,7 +175,7 @@ class CRUDController extends AbstractController */ protected function buildQueryEntities(string $action, Request $request) { - $query = $this->managerRegistry + $query = $this->getManagerRegistry() ->getManager() ->createQueryBuilder() ->select('e') @@ -267,7 +289,7 @@ class CRUDController extends AbstractController if ($form->isSubmitted() && $form->isValid()) { $this->onFormValid($action, $entity, $form, $request); - $em = $this->managerRegistry->getManager(); + $em = $this->getManagerRegistry()->getManager(); $this->onPreRemove($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); $originalEntity = $this->getEntity($action, $id, $request); - $this->managerRegistry->getManager() + $this->getManagerRegistry()->getManager() ->detach($originalEntity); return $originalEntity; @@ -384,7 +406,7 @@ class CRUDController extends AbstractController if ($form->isSubmitted() && $form->isValid()) { $this->onFormValid($action, $entity, $form, $request); - $em = $this->managerRegistry->getManager(); + $em = $this->getManagerRegistry()->getManager(); $this->onPrePersist($action, $entity, $form, $request); $em->persist($entity); @@ -487,7 +509,7 @@ class CRUDController extends AbstractController if ($form->isSubmitted() && $form->isValid()) { $this->onFormValid($action, $entity, $form, $request); - $em = $this->managerRegistry->getManager(); + $em = $this->getManagerRegistry()->getManager(); $this->onPreFlush($action, $entity, $form, $request); $em->flush(); @@ -601,7 +623,7 @@ class CRUDController extends AbstractController */ protected function getEntity(mixed $action, $id, Request $request): ?object { - return $this->managerRegistry + return $this->getManagerRegistry() ->getRepository($this->getEntityClass()) ->find($id); } @@ -650,6 +672,11 @@ class CRUDController extends AbstractController return $this->container->get('chill_main.paginator_factory'); } + protected function getManagerRegistry(): ManagerRegistry + { + return $this->container->get(ManagerRegistry::class); + } + /** * Get the result of the query. */ @@ -920,7 +947,7 @@ class CRUDController extends AbstractController protected function removeEntity(string $action, $entity, FormInterface $form, Request $request) { - $this->managerRegistry + $this->getManagerRegistry() ->getManager() ->remove($entity); }