Merge branch 'master' into upgrade-sf5

This commit is contained in:
2024-02-12 21:50:34 +01:00
920 changed files with 6430 additions and 1914 deletions

View File

@@ -15,10 +15,14 @@ use Chill\MainBundle\CRUD\Resolver\Resolver;
use Chill\MainBundle\Pagination\PaginatorFactory;
use Chill\MainBundle\Pagination\PaginatorInterface;
use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
use Doctrine\DBAL\LockMode;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\OptimisticLockException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
@@ -143,7 +147,9 @@ abstract class AbstractCRUDController extends AbstractController
return new $class();
}
protected function customizeQuery(string $action, Request $request, $query): void {}
protected function customizeQuery(string $action, Request $request, $query): void
{
}
protected function getActionConfig(string $action)
{
@@ -172,6 +178,21 @@ abstract class AbstractCRUDController extends AbstractController
if (null === $e) {
throw $this->createNotFoundException(sprintf('The object %s for id %s is not found', $this->getEntityClass(), $id));
}
if ($request->query->has('entity_version')) {
$expectedVersion = $request->query->getInt('entity_version');
try {
$manager = $this->getDoctrine()->getManagerForClass($this->getEntityClass());
if ($manager instanceof EntityManagerInterface) {
$manager->lock($e, LockMode::OPTIMISTIC, $expectedVersion);
} else {
throw new \LogicException('This manager does not allow locking.');
}
} catch (OptimisticLockException $e) {
throw new ConflictHttpException('Sorry, but someone else has already changed this entity. Please refresh the page and apply the changes again', $e);
}
}
return $e;
}

View File

@@ -137,7 +137,7 @@ class ApiController extends AbstractCRUDController
try {
$entity = $this->deserialize($action, $request, $_format, $entity);
} catch (NotEncodableValueException $e) {
throw new BadRequestHttpException('invalid json', 400, $e);
throw new BadRequestHttpException('invalid json', $e, 400);
}
$errors = $this->validate($action, $request, $_format, $entity);
@@ -155,7 +155,7 @@ class ApiController extends AbstractCRUDController
return $response;
}
$this->managerRegistry->getManager()->flush();
$this->managerRegistry->getManagerForClass($this->getEntityClass())->flush();
$response = $this->onAfterFlush($action, $request, $_format, $entity, $errors);
@@ -177,7 +177,7 @@ class ApiController extends AbstractCRUDController
public function indexApi(Request $request, string $_format)
{
return match ($request->getMethod()) {
Request::METHOD_GET, REQUEST::METHOD_HEAD => $this->indexApiAction('_index', $request, $_format),
Request::METHOD_GET, Request::METHOD_HEAD => $this->indexApiAction('_index', $request, $_format),
default => throw $this->createNotFoundException('This method is not supported'),
};
}

View File

@@ -205,7 +205,7 @@ class CRUDController extends AbstractController
/**
* Count the number of entities.
*/
protected function countEntities(string $action, Request $request, FilterOrderHelper $filterOrder = null): int
protected function countEntities(string $action, Request $request, ?FilterOrderHelper $filterOrder = null): int
{
return $this->buildQueryEntities($action, $request)
->select('COUNT(e)')
@@ -234,7 +234,7 @@ class CRUDController extends AbstractController
* It is preferable to override customizeForm instead of overriding
* this method.
*/
protected function createFormFor(string $action, mixed $entity, string $formClass = null, array $formOptions = []): FormInterface
protected function createFormFor(string $action, mixed $entity, ?string $formClass = null, array $formOptions = []): FormInterface
{
$formClass ??= $this->getFormClassFor($action);
@@ -248,9 +248,13 @@ class CRUDController extends AbstractController
/**
* Customize the form created by createFormFor.
*/
protected function customizeForm(string $action, FormInterface $form) {}
protected function customizeForm(string $action, FormInterface $form)
{
}
protected function customizeQuery(string $action, Request $request, $query): void {}
protected function customizeQuery(string $action, Request $request, $query): void
{
}
/**
* @param null $formClass
@@ -483,7 +487,7 @@ class CRUDController extends AbstractController
*
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
protected function formEditAction(string $action, Request $request, mixed $id, string $formClass = null, array $formOptions = []): Response
protected function formEditAction(string $action, Request $request, mixed $id, ?string $formClass = null, array $formOptions = []): Response
{
$entity = $this->getEntity($action, $id, $request);
@@ -685,7 +689,7 @@ class CRUDController extends AbstractController
Request $request,
int $totalItems,
PaginatorInterface $paginator,
FilterOrderHelper $filterOrder = null
?FilterOrderHelper $filterOrder = null
) {
$query = $this->queryEntities($action, $request, $paginator, $filterOrder);
@@ -695,7 +699,7 @@ class CRUDController extends AbstractController
/**
* @return \Chill\MainBundle\Entity\Center[]
*/
protected function getReachableCenters(string $role, Scope $scope = null)
protected function getReachableCenters(string $role, ?Scope $scope = null)
{
return $this->getAuthorizationHelper()
->getReachableCenters($this->getUser(), $role, $scope);
@@ -869,7 +873,9 @@ class CRUDController extends AbstractController
};
}
protected function onFormValid(string $action, object $entity, FormInterface $form, Request $request) {}
protected function onFormValid(string $action, object $entity, FormInterface $form, Request $request)
{
}
protected function onPostCheckACL($action, Request $request, $entity): ?Response
{
@@ -881,36 +887,58 @@ class CRUDController extends AbstractController
return null;
}
protected function onPostFlush(string $action, $entity, FormInterface $form, Request $request) {}
protected function onPostFlush(string $action, $entity, FormInterface $form, Request $request)
{
}
/**
* method used by indexAction.
*/
protected function onPostIndexBuildQuery(string $action, Request $request, int $totalItems, PaginatorInterface $paginator, mixed $query) {}
protected function onPostIndexBuildQuery(string $action, Request $request, int $totalItems, PaginatorInterface $paginator, mixed $query)
{
}
/**
* method used by indexAction.
*/
protected function onPostIndexFetchQuery(string $action, Request $request, int $totalItems, PaginatorInterface $paginator, mixed $entities) {}
protected function onPostIndexFetchQuery(string $action, Request $request, int $totalItems, PaginatorInterface $paginator, mixed $entities)
{
}
protected function onPostPersist(string $action, $entity, FormInterface $form, Request $request) {}
protected function onPostPersist(string $action, $entity, FormInterface $form, Request $request)
{
}
protected function onPostRemove(string $action, $entity, FormInterface $form, Request $request) {}
protected function onPostRemove(string $action, $entity, FormInterface $form, Request $request)
{
}
protected function onPreDelete(string $action, Request $request) {}
protected function onPreDelete(string $action, Request $request)
{
}
protected function onPreFlush(string $action, $entity, FormInterface $form, Request $request) {}
protected function onPreFlush(string $action, $entity, FormInterface $form, Request $request)
{
}
protected function onPreIndex(string $action, Request $request) {}
protected function onPreIndex(string $action, Request $request)
{
}
/**
* method used by indexAction.
*/
protected function onPreIndexBuildQuery(string $action, Request $request, int $totalItems, PaginatorInterface $paginator) {}
protected function onPreIndexBuildQuery(string $action, Request $request, int $totalItems, PaginatorInterface $paginator)
{
}
protected function onPrePersist(string $action, $entity, FormInterface $form, Request $request) {}
protected function onPrePersist(string $action, $entity, FormInterface $form, Request $request)
{
}
protected function onPreRemove(string $action, $entity, FormInterface $form, Request $request) {}
protected function onPreRemove(string $action, $entity, FormInterface $form, Request $request)
{
}
/**
* Add ordering fields in the query build by self::queryEntities.
@@ -935,7 +963,7 @@ class CRUDController extends AbstractController
*
* @return type
*/
protected function queryEntities(string $action, Request $request, PaginatorInterface $paginator, FilterOrderHelper $filterOrder = null)
protected function queryEntities(string $action, Request $request, PaginatorInterface $paginator, ?FilterOrderHelper $filterOrder = null)
{
$query = $this->buildQueryEntities($action, $request)
->setFirstResult($paginator->getCurrentPage()->getFirstItemNumber())