Inject managerRegistry and serializerInterface directly into controllers

This commit is contained in:
2025-05-28 12:04:27 +02:00
parent cc20e8896f
commit abfdf2ec6d
7 changed files with 70 additions and 25 deletions

View File

@@ -14,6 +14,7 @@ namespace Chill\DocStoreBundle\Controller;
use Chill\DocStoreBundle\Entity\StoredObject; use Chill\DocStoreBundle\Entity\StoredObject;
use Chill\MainBundle\CRUD\Controller\ApiController; use Chill\MainBundle\CRUD\Controller\ApiController;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
@@ -25,9 +26,12 @@ class StoredObjectApiController extends ApiController
{ {
public function __construct( public function __construct(
private readonly Security $security, private readonly Security $security,
private readonly SerializerInterface $serializer,
private readonly EntityManagerInterface $entityManager, private readonly EntityManagerInterface $entityManager,
) {} SerializerInterface $serializer,
ManagerRegistry $managerRegistry,
) {
parent::__construct($serializer, $managerRegistry);
}
/** /**
* Creates a new stored object. * Creates a new stored object.

View File

@@ -39,7 +39,15 @@ abstract class AbstractCRUDController extends AbstractController
*/ */
protected array $crudConfig = []; 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. * get the role given from the config.
@@ -99,7 +107,7 @@ abstract class AbstractCRUDController extends AbstractController
*/ */
protected function buildQueryEntities(string $action, Request $request) protected function buildQueryEntities(string $action, Request $request)
{ {
$qb = $this->doctrine->getManager() $qb = $this->managerRegistry->getManager()
->createQueryBuilder() ->createQueryBuilder()
->select('e') ->select('e')
->from($this->getEntityClass(), 'e'); ->from($this->getEntityClass(), 'e');
@@ -171,7 +179,7 @@ abstract class AbstractCRUDController extends AbstractController
*/ */
protected function getEntity(mixed $action, string $id, Request $request): object protected function getEntity(mixed $action, string $id, Request $request): object
{ {
$e = $this->doctrine->getRepository($this->getEntityClass()) $e = $this->managerRegistry->getRepository($this->getEntityClass())
->find($id); ->find($id);
if (null === $e) { if (null === $e) {
@@ -181,7 +189,7 @@ abstract class AbstractCRUDController extends AbstractController
$expectedVersion = $request->query->getInt('entity_version'); $expectedVersion = $request->query->getInt('entity_version');
try { try {
$manager = $this->doctrine->getManagerForClass($this->getEntityClass()); $manager = $this->managerRegistry->getManagerForClass($this->getEntityClass());
if ($manager instanceof EntityManagerInterface) { if ($manager instanceof EntityManagerInterface) {
$manager->lock($e, LockMode::OPTIMISTIC, $expectedVersion); $manager->lock($e, LockMode::OPTIMISTIC, $expectedVersion);

View File

@@ -13,6 +13,7 @@ namespace Chill\MainBundle\CRUD\Controller;
use Chill\MainBundle\Pagination\PaginatorInterface; use Chill\MainBundle\Pagination\PaginatorInterface;
use Chill\MainBundle\Serializer\Model\Collection; use Chill\MainBundle\Serializer\Model\Collection;
use Doctrine\Persistence\ManagerRegistry;
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;
@@ -23,6 +24,11 @@ use Symfony\Component\Validator\ConstraintViolationListInterface;
class ApiController extends AbstractCRUDController class ApiController extends AbstractCRUDController
{ {
public function __construct(SerializerInterface $serializer, ManagerRegistry $managerRegistry)
{
parent::__construct($serializer, $managerRegistry);
}
/** /**
* Base method for handling api action. * Base method for handling api action.
* *
@@ -80,8 +86,8 @@ class ApiController extends AbstractCRUDController
return $response; return $response;
} }
$this->doctrine->getManager()->remove($entity); $this->managerRegistry->getManager()->remove($entity);
$this->doctrine->getManager()->flush(); $this->managerRegistry->getManager()->flush();
$response = $this->onAfterFlush($action, $request, $_format, $entity, $errors); $response = $this->onAfterFlush($action, $request, $_format, $entity, $errors);
@@ -242,7 +248,7 @@ class ApiController extends AbstractCRUDController
} }
try { 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) { } catch (\Symfony\Component\Serializer\Exception\UnexpectedValueException $e) {
throw new BadRequestHttpException(sprintf('Unable to deserialize posted data: %s', $e->getMessage()), $e, 0); 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) $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']]; return ['groups' => ['read']];
} }
protected function getSerializer(): SerializerInterface
{
return $this->get('serializer');
}
protected function getValidationGroups(string $action, Request $request, string $_format, $entity): ?array protected function getValidationGroups(string $action, Request $request, string $_format, $entity): ?array
{ {
return null; return null;

View File

@@ -13,14 +13,21 @@ namespace Chill\MainBundle\Controller;
use Chill\MainBundle\CRUD\Controller\ApiController; use Chill\MainBundle\CRUD\Controller\ApiController;
use Chill\MainBundle\Entity\Address; use Chill\MainBundle\Entity\Address;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\SerializerInterface;
class AddressApiController extends ApiController 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. * Duplicate an existing address.

View File

@@ -30,6 +30,7 @@ use Chill\PersonBundle\Repository\AccompanyingPeriodRepository;
use Chill\PersonBundle\Security\Authorization\AccompanyingPeriodVoter; use Chill\PersonBundle\Security\Authorization\AccompanyingPeriodVoter;
use Chill\ThirdPartyBundle\Entity\ThirdParty; use Chill\ThirdPartyBundle\Entity\ThirdParty;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request; 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\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Serializer\Exception\RuntimeException; use Symfony\Component\Serializer\Exception\RuntimeException;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Validator\ConstraintViolationList; use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\ConstraintViolationListInterface; use Symfony\Component\Validator\ConstraintViolationListInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface; use Symfony\Component\Validator\Validator\ValidatorInterface;
@@ -46,7 +48,18 @@ use Symfony\Component\Workflow\Registry;
final class AccompanyingCourseApiController extends ApiController 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 public function commentApi($id, Request $request, string $_format): Response
{ {

View File

@@ -22,16 +22,26 @@ use Chill\PersonBundle\Event\Person\PersonAddressMoveEvent;
use Chill\PersonBundle\Repository\Household\HouseholdACLAwareRepositoryInterface; use Chill\PersonBundle\Repository\Household\HouseholdACLAwareRepositoryInterface;
use Chill\PersonBundle\Repository\Household\HouseholdRepository; use Chill\PersonBundle\Repository\Household\HouseholdRepository;
use Chill\PersonBundle\Security\Authorization\HouseholdVoter; use Chill\PersonBundle\Security\Authorization\HouseholdVoter;
use Doctrine\Persistence\ManagerRegistry;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
class HouseholdApiController extends ApiController 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 * @return \Symfony\Component\HttpFoundation\JsonResponse

View File

@@ -12,9 +12,7 @@ declare(strict_types=1);
namespace Chill\PersonBundle\Controller; namespace Chill\PersonBundle\Controller;
use Chill\MainBundle\CRUD\Controller\ApiController; use Chill\MainBundle\CRUD\Controller\ApiController;
use Chill\PersonBundle\Entity\Household\Household;
use Chill\PersonBundle\Entity\Household\HouseholdMember; use Chill\PersonBundle\Entity\Household\HouseholdMember;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Form\HouseholdMemberType; use Chill\PersonBundle\Form\HouseholdMemberType;
use Chill\PersonBundle\Household\MembersEditor; use Chill\PersonBundle\Household\MembersEditor;
use Chill\PersonBundle\Repository\AccompanyingPeriodRepository; use Chill\PersonBundle\Repository\AccompanyingPeriodRepository;
@@ -23,6 +21,7 @@ use Chill\PersonBundle\Repository\Household\PositionRepository;
use Chill\PersonBundle\Repository\PersonRepository; use Chill\PersonBundle\Repository\PersonRepository;
use Chill\PersonBundle\Security\Authorization\HouseholdVoter; use Chill\PersonBundle\Security\Authorization\HouseholdVoter;
use Chill\PersonBundle\Security\Authorization\PersonVoter; use Chill\PersonBundle\Security\Authorization\PersonVoter;
use Doctrine\Persistence\ManagerRegistry;
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\AccessDeniedHttpException; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
@@ -32,6 +31,7 @@ use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Bundle\SecurityBundle\Security; use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Serializer\Exception; use Symfony\Component\Serializer\Exception;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Contracts\Translation\TranslatorInterface; use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
@@ -47,9 +47,11 @@ class HouseholdMemberController extends ApiController
private readonly HouseholdRepository $householdRepository, private readonly HouseholdRepository $householdRepository,
private readonly Security $security, private readonly Security $security,
private readonly PositionRepository $positionRepository, private readonly PositionRepository $positionRepository,
private readonly \Doctrine\Persistence\ManagerRegistry $managerRegistry, ManagerRegistry $managerRegistry,
SerializerInterface $serializer,
protected ParameterBagInterface $parameterBag, protected ParameterBagInterface $parameterBag,
) { ) {
parent::__construct($serializer, $managerRegistry);
$this->household_fields_visibility = $parameterBag->get('chill_person.household_fields'); $this->household_fields_visibility = $parameterBag->get('chill_person.household_fields');
} }
@@ -143,10 +145,10 @@ class HouseholdMemberController extends ApiController
$data = [ $data = [
'persons' => $persons ?? false ? 'persons' => $persons ?? false ?
$this->getSerializer()->normalize($persons, 'json', ['groups' => ['read']]) : [], $this->serializer->normalize($persons, 'json', ['groups' => ['read']]) : [],
'household' => $household ?? false ? 'household' => $household ?? false ?
$this->getSerializer()->normalize($household, 'json', ['groups' => ['read']]) : null, $this->serializer->normalize($household, 'json', ['groups' => ['read']]) : null,
'positions' => $this->getSerializer()->normalize($positions, 'json', ['groups' => ['read']]), 'positions' => $this->serializer->normalize($positions, 'json', ['groups' => ['read']]),
'allowHouseholdCreate' => $allowHouseholdCreate ?? true, 'allowHouseholdCreate' => $allowHouseholdCreate ?? true,
'allowHouseholdSearch' => $allowHouseholdSearch ?? true, 'allowHouseholdSearch' => $allowHouseholdSearch ?? true,
'allowLeaveWithoutHousehold' => $allowLeaveWithoutHousehold ?? $request->query->has('allow_leave_without_household'), 'allowLeaveWithoutHousehold' => $allowLeaveWithoutHousehold ?? $request->query->has('allow_leave_without_household'),
@@ -176,7 +178,7 @@ class HouseholdMemberController extends ApiController
{ {
try { try {
/** @var MembersEditor $editor */ /** @var MembersEditor $editor */
$editor = $this->getSerializer() $editor = $this->serializer
->deserialize( ->deserialize(
$request->getContent(), $request->getContent(),
MembersEditor::class, MembersEditor::class,