mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-10-04 20:39:40 +00:00
Inject managerRegistry and serializerInterface directly into controllers
This commit is contained in:
@@ -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);
|
||||
|
@@ -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;
|
||||
|
@@ -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.
|
||||
|
Reference in New Issue
Block a user