From ab15ec8a4c20cbec25ead4e10c579e07185524c3 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Wed, 28 May 2025 12:04:27 +0200 Subject: [PATCH] Inject managerRegistry and serializerInterface directly into controllers --- .../Controller/StoredObjectApiController.php | 8 ++++++-- .../Controller/AbstractCRUDController.php | 16 ++++++++++++---- .../CRUD/Controller/ApiController.php | 19 ++++++++++--------- .../Controller/AddressApiController.php | 9 ++++++++- .../AccompanyingCourseApiController.php | 15 ++++++++++++++- .../Controller/HouseholdApiController.php | 12 +++++++++++- .../Controller/HouseholdMemberController.php | 16 +++++++++------- 7 files changed, 70 insertions(+), 25 deletions(-) diff --git a/src/Bundle/ChillDocStoreBundle/Controller/StoredObjectApiController.php b/src/Bundle/ChillDocStoreBundle/Controller/StoredObjectApiController.php index 8b78245c2..4bde437cc 100644 --- a/src/Bundle/ChillDocStoreBundle/Controller/StoredObjectApiController.php +++ b/src/Bundle/ChillDocStoreBundle/Controller/StoredObjectApiController.php @@ -14,6 +14,7 @@ namespace Chill\DocStoreBundle\Controller; use Chill\DocStoreBundle\Entity\StoredObject; use Chill\MainBundle\CRUD\Controller\ApiController; use Doctrine\ORM\EntityManagerInterface; +use Doctrine\Persistence\ManagerRegistry; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Symfony\Component\Routing\Annotation\Route; @@ -25,9 +26,12 @@ class StoredObjectApiController extends ApiController { public function __construct( private readonly Security $security, - private readonly SerializerInterface $serializer, private readonly EntityManagerInterface $entityManager, - ) {} + SerializerInterface $serializer, + ManagerRegistry $managerRegistry, + ) { + parent::__construct($serializer, $managerRegistry); + } /** * Creates a new stored object. diff --git a/src/Bundle/ChillMainBundle/CRUD/Controller/AbstractCRUDController.php b/src/Bundle/ChillMainBundle/CRUD/Controller/AbstractCRUDController.php index ee6770f28..7e24a113e 100644 --- a/src/Bundle/ChillMainBundle/CRUD/Controller/AbstractCRUDController.php +++ b/src/Bundle/ChillMainBundle/CRUD/Controller/AbstractCRUDController.php @@ -39,7 +39,15 @@ abstract class AbstractCRUDController extends AbstractController */ protected array $crudConfig = []; - public function __construct(protected ManagerRegistry $doctrine) {} + protected readonly SerializerInterface $serializer; + + protected readonly ManagerRegistry $managerRegistry; + + public function __construct(SerializerInterface $serializer, ManagerRegistry $managerRegistry) + { + $this->serializer = $serializer; + $this->managerRegistry = $managerRegistry; + } /** * get the role given from the config. @@ -99,7 +107,7 @@ abstract class AbstractCRUDController extends AbstractController */ protected function buildQueryEntities(string $action, Request $request) { - $qb = $this->doctrine->getManager() + $qb = $this->managerRegistry->getManager() ->createQueryBuilder() ->select('e') ->from($this->getEntityClass(), 'e'); @@ -171,7 +179,7 @@ abstract class AbstractCRUDController extends AbstractController */ protected function getEntity(mixed $action, string $id, Request $request): object { - $e = $this->doctrine->getRepository($this->getEntityClass()) + $e = $this->managerRegistry->getRepository($this->getEntityClass()) ->find($id); if (null === $e) { @@ -181,7 +189,7 @@ abstract class AbstractCRUDController extends AbstractController $expectedVersion = $request->query->getInt('entity_version'); try { - $manager = $this->doctrine->getManagerForClass($this->getEntityClass()); + $manager = $this->managerRegistry->getManagerForClass($this->getEntityClass()); if ($manager instanceof EntityManagerInterface) { $manager->lock($e, LockMode::OPTIMISTIC, $expectedVersion); diff --git a/src/Bundle/ChillMainBundle/CRUD/Controller/ApiController.php b/src/Bundle/ChillMainBundle/CRUD/Controller/ApiController.php index 986b0d449..1137754a3 100644 --- a/src/Bundle/ChillMainBundle/CRUD/Controller/ApiController.php +++ b/src/Bundle/ChillMainBundle/CRUD/Controller/ApiController.php @@ -13,6 +13,7 @@ namespace Chill\MainBundle\CRUD\Controller; use Chill\MainBundle\Pagination\PaginatorInterface; use Chill\MainBundle\Serializer\Model\Collection; +use Doctrine\Persistence\ManagerRegistry; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; @@ -23,6 +24,11 @@ use Symfony\Component\Validator\ConstraintViolationListInterface; class ApiController extends AbstractCRUDController { + public function __construct(SerializerInterface $serializer, ManagerRegistry $managerRegistry) + { + parent::__construct($serializer, $managerRegistry); + } + /** * Base method for handling api action. * @@ -80,8 +86,8 @@ class ApiController extends AbstractCRUDController return $response; } - $this->doctrine->getManager()->remove($entity); - $this->doctrine->getManager()->flush(); + $this->managerRegistry->getManager()->remove($entity); + $this->managerRegistry->getManager()->flush(); $response = $this->onAfterFlush($action, $request, $_format, $entity, $errors); @@ -242,7 +248,7 @@ class ApiController extends AbstractCRUDController } try { - $postedData = $this->getSerializer()->deserialize($request->getContent(), $postedDataType, $_format, $postedDataContext); + $postedData = $this->serializer->deserialize($request->getContent(), $postedDataType, $_format, $postedDataContext); } catch (\Symfony\Component\Serializer\Exception\UnexpectedValueException $e) { throw new BadRequestHttpException(sprintf('Unable to deserialize posted data: %s', $e->getMessage()), $e, 0); } @@ -310,7 +316,7 @@ class ApiController extends AbstractCRUDController $this->getContextForSerialization($action, $request, $_format, $entity) ); - return $this->getSerializer()->deserialize($request->getContent(), $this->getEntityClass(), $_format, $context); + return $this->serializer->deserialize($request->getContent(), $this->getEntityClass(), $_format, $context); } /** @@ -445,11 +451,6 @@ class ApiController extends AbstractCRUDController return ['groups' => ['read']]; } - protected function getSerializer(): SerializerInterface - { - return $this->get('serializer'); - } - protected function getValidationGroups(string $action, Request $request, string $_format, $entity): ?array { return null; diff --git a/src/Bundle/ChillMainBundle/Controller/AddressApiController.php b/src/Bundle/ChillMainBundle/Controller/AddressApiController.php index 5ea68cdbc..455862f3f 100644 --- a/src/Bundle/ChillMainBundle/Controller/AddressApiController.php +++ b/src/Bundle/ChillMainBundle/Controller/AddressApiController.php @@ -13,14 +13,21 @@ namespace Chill\MainBundle\Controller; use Chill\MainBundle\CRUD\Controller\ApiController; use Chill\MainBundle\Entity\Address; +use Doctrine\Persistence\ManagerRegistry; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; +use Symfony\Component\Serializer\SerializerInterface; class AddressApiController extends ApiController { - public function __construct(private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry) {} + public function __construct( + SerializerInterface $serializer, + ManagerRegistry $managerRegistry, + ) { + parent::__construct($serializer, $managerRegistry); + } /** * Duplicate an existing address. diff --git a/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseApiController.php b/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseApiController.php index 531a5cb24..ca9520217 100644 --- a/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseApiController.php +++ b/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseApiController.php @@ -30,6 +30,7 @@ use Chill\PersonBundle\Repository\AccompanyingPeriodRepository; use Chill\PersonBundle\Security\Authorization\AccompanyingPeriodVoter; use Chill\ThirdPartyBundle\Entity\ThirdParty; use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; +use Doctrine\Persistence\ManagerRegistry; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; @@ -39,6 +40,7 @@ use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Security\Core\Exception\AccessDeniedException; use Symfony\Component\Serializer\Exception\RuntimeException; use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; +use Symfony\Component\Serializer\SerializerInterface; use Symfony\Component\Validator\ConstraintViolationList; use Symfony\Component\Validator\ConstraintViolationListInterface; use Symfony\Component\Validator\Validator\ValidatorInterface; @@ -46,7 +48,18 @@ use Symfony\Component\Workflow\Registry; final class AccompanyingCourseApiController extends ApiController { - public function __construct(private readonly AccompanyingPeriodRepository $accompanyingPeriodRepository, private readonly EventDispatcherInterface $eventDispatcher, private readonly ReferralsSuggestionInterface $referralAvailable, private readonly Registry $registry, private readonly ValidatorInterface $validator, private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry, private readonly AccompanyingPeriodWorkRepository $accompanyingPeriodWorkRepository) {} + public function __construct( + private readonly AccompanyingPeriodRepository $accompanyingPeriodRepository, + private readonly EventDispatcherInterface $eventDispatcher, + private readonly ReferralsSuggestionInterface $referralAvailable, + private readonly Registry $registry, + private readonly ValidatorInterface $validator, + private readonly AccompanyingPeriodWorkRepository $accompanyingPeriodWorkRepository, + ManagerRegistry $managerRegistry, + SerializerInterface $serializer, + ) { + parent::__construct($serializer, $managerRegistry); + } public function commentApi($id, Request $request, string $_format): Response { diff --git a/src/Bundle/ChillPersonBundle/Controller/HouseholdApiController.php b/src/Bundle/ChillPersonBundle/Controller/HouseholdApiController.php index 8a09bed38..f32e44b89 100644 --- a/src/Bundle/ChillPersonBundle/Controller/HouseholdApiController.php +++ b/src/Bundle/ChillPersonBundle/Controller/HouseholdApiController.php @@ -22,16 +22,26 @@ use Chill\PersonBundle\Event\Person\PersonAddressMoveEvent; use Chill\PersonBundle\Repository\Household\HouseholdACLAwareRepositoryInterface; use Chill\PersonBundle\Repository\Household\HouseholdRepository; use Chill\PersonBundle\Security\Authorization\HouseholdVoter; +use Doctrine\Persistence\ManagerRegistry; use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; +use Symfony\Component\Serializer\SerializerInterface; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; class HouseholdApiController extends ApiController { - public function __construct(private readonly EventDispatcherInterface $eventDispatcher, private readonly HouseholdRepository $householdRepository, private readonly HouseholdACLAwareRepositoryInterface $householdACLAwareRepository, private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry) {} + public function __construct( + private readonly EventDispatcherInterface $eventDispatcher, + private readonly HouseholdRepository $householdRepository, + private readonly HouseholdACLAwareRepositoryInterface $householdACLAwareRepository, + ManagerRegistry $managerRegistry, + SerializerInterface $serializer, + ) { + parent::__construct($serializer, $managerRegistry); + } /** * @return \Symfony\Component\HttpFoundation\JsonResponse diff --git a/src/Bundle/ChillPersonBundle/Controller/HouseholdMemberController.php b/src/Bundle/ChillPersonBundle/Controller/HouseholdMemberController.php index 414c97cf7..1583cad49 100644 --- a/src/Bundle/ChillPersonBundle/Controller/HouseholdMemberController.php +++ b/src/Bundle/ChillPersonBundle/Controller/HouseholdMemberController.php @@ -12,9 +12,7 @@ declare(strict_types=1); namespace Chill\PersonBundle\Controller; use Chill\MainBundle\CRUD\Controller\ApiController; -use Chill\PersonBundle\Entity\Household\Household; use Chill\PersonBundle\Entity\Household\HouseholdMember; -use Chill\PersonBundle\Entity\Person; use Chill\PersonBundle\Form\HouseholdMemberType; use Chill\PersonBundle\Household\MembersEditor; use Chill\PersonBundle\Repository\AccompanyingPeriodRepository; @@ -23,6 +21,7 @@ use Chill\PersonBundle\Repository\Household\PositionRepository; use Chill\PersonBundle\Repository\PersonRepository; use Chill\PersonBundle\Security\Authorization\HouseholdVoter; use Chill\PersonBundle\Security\Authorization\PersonVoter; +use Doctrine\Persistence\ManagerRegistry; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; @@ -32,6 +31,7 @@ use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Bundle\SecurityBundle\Security; use Symfony\Component\Serializer\Exception; +use Symfony\Component\Serializer\SerializerInterface; use Symfony\Contracts\Translation\TranslatorInterface; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; @@ -47,9 +47,11 @@ class HouseholdMemberController extends ApiController private readonly HouseholdRepository $householdRepository, private readonly Security $security, private readonly PositionRepository $positionRepository, - private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry, + ManagerRegistry $managerRegistry, + SerializerInterface $serializer, protected ParameterBagInterface $parameterBag, ) { + parent::__construct($serializer, $managerRegistry); $this->household_fields_visibility = $parameterBag->get('chill_person.household_fields'); } @@ -143,10 +145,10 @@ class HouseholdMemberController extends ApiController $data = [ 'persons' => $persons ?? false ? - $this->getSerializer()->normalize($persons, 'json', ['groups' => ['read']]) : [], + $this->serializer->normalize($persons, 'json', ['groups' => ['read']]) : [], 'household' => $household ?? false ? - $this->getSerializer()->normalize($household, 'json', ['groups' => ['read']]) : null, - 'positions' => $this->getSerializer()->normalize($positions, 'json', ['groups' => ['read']]), + $this->serializer->normalize($household, 'json', ['groups' => ['read']]) : null, + 'positions' => $this->serializer->normalize($positions, 'json', ['groups' => ['read']]), 'allowHouseholdCreate' => $allowHouseholdCreate ?? true, 'allowHouseholdSearch' => $allowHouseholdSearch ?? true, 'allowLeaveWithoutHousehold' => $allowLeaveWithoutHousehold ?? $request->query->has('allow_leave_without_household'), @@ -176,7 +178,7 @@ class HouseholdMemberController extends ApiController { try { /** @var MembersEditor $editor */ - $editor = $this->getSerializer() + $editor = $this->serializer ->deserialize( $request->getContent(), MembersEditor::class,