mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-30 03:23:48 +00:00
apply more cs rules for php-cs
This commit is contained in:
@@ -38,10 +38,10 @@ class CRUDControllerCompilerPass implements CompilerPassInterface
|
||||
private function configureCrudController(ContainerBuilder $container, array $crudEntry, string $apiOrCrud): void
|
||||
{
|
||||
$controllerClass = $crudEntry['controller'];
|
||||
$controllerServiceName = 'cs' . $apiOrCrud . '_' . $crudEntry['name'] . '_controller';
|
||||
$controllerServiceName = 'cs'.$apiOrCrud.'_'.$crudEntry['name'].'_controller';
|
||||
|
||||
// create config parameter in container
|
||||
$param = 'chill_main_' . $apiOrCrud . '_config_' . $crudEntry['name'];
|
||||
$param = 'chill_main_'.$apiOrCrud.'_config_'.$crudEntry['name'];
|
||||
$container->setParameter($param, $crudEntry);
|
||||
|
||||
if ($container->hasDefinition($controllerClass)) {
|
||||
@@ -51,7 +51,7 @@ class CRUDControllerCompilerPass implements CompilerPassInterface
|
||||
|
||||
// add the "addMethodCall"
|
||||
$container->getDefinition($controllerClass)
|
||||
->addMethodCall('setCrudConfig', ['%' . $param . '%']);
|
||||
->addMethodCall('setCrudConfig', ['%'.$param.'%']);
|
||||
} else {
|
||||
$controller = new Definition($controllerClass);
|
||||
|
||||
@@ -59,7 +59,7 @@ class CRUDControllerCompilerPass implements CompilerPassInterface
|
||||
$controller->setAutoconfigured(true);
|
||||
$controller->setPublic(true);
|
||||
|
||||
$controller->addMethodCall('setCrudConfig', ['%' . $param . '%']);
|
||||
$controller->addMethodCall('setCrudConfig', ['%'.$param.'%']);
|
||||
|
||||
$container->setDefinition($controllerServiceName, $controller);
|
||||
}
|
||||
|
@@ -23,8 +23,6 @@ use Symfony\Component\Serializer\SerializerInterface;
|
||||
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
use function array_merge;
|
||||
|
||||
abstract class AbstractCRUDController extends AbstractController
|
||||
{
|
||||
/**
|
||||
@@ -49,14 +47,12 @@ abstract class AbstractCRUDController extends AbstractController
|
||||
return $this->crudConfig['base_role'];
|
||||
}
|
||||
|
||||
throw new \RuntimeException(sprintf('the config does not have any role for the ' .
|
||||
'method %s nor a global role for the whole action. Add those to your ' .
|
||||
'configuration or override the required method', $request->getMethod()));
|
||||
throw new \RuntimeException(sprintf('the config does not have any role for the method %s nor a global role for the whole action. Add those to your configuration or override the required method', $request->getMethod()));
|
||||
}
|
||||
|
||||
public static function getSubscribedServices(): array
|
||||
{
|
||||
return array_merge(
|
||||
return \array_merge(
|
||||
parent::getSubscribedServices(),
|
||||
[
|
||||
'chill_main.paginator_factory' => PaginatorFactory::class,
|
||||
@@ -153,7 +149,7 @@ abstract class AbstractCRUDController extends AbstractController
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string The crud name.
|
||||
* @return string the crud name
|
||||
*/
|
||||
protected function getCrudName(): string
|
||||
{
|
||||
|
@@ -13,9 +13,6 @@ namespace Chill\MainBundle\CRUD\Controller;
|
||||
|
||||
use Chill\MainBundle\Pagination\PaginatorInterface;
|
||||
use Chill\MainBundle\Serializer\Model\Collection;
|
||||
use Exception;
|
||||
use LogicException;
|
||||
use RuntimeException;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
@@ -24,15 +21,11 @@ use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
use Symfony\Component\Validator\ConstraintViolationListInterface;
|
||||
|
||||
use function array_merge;
|
||||
use function ucfirst;
|
||||
|
||||
class ApiController extends AbstractCRUDController
|
||||
{
|
||||
/**
|
||||
* Base method for handling api action.
|
||||
*
|
||||
* @param string $_format
|
||||
* @return void
|
||||
*/
|
||||
public function entityApi(Request $request, mixed $id, ?string $_format = 'json'): Response
|
||||
@@ -51,8 +44,7 @@ class ApiController extends AbstractCRUDController
|
||||
$entity = $this->getEntity($action, $id, $request);
|
||||
|
||||
if (null === $entity) {
|
||||
throw $this->createNotFoundException(sprintf('The %s with id %s '
|
||||
. 'is not found', $this->getCrudName(), $id));
|
||||
throw $this->createNotFoundException(sprintf('The %s with id %s is not found', $this->getCrudName(), $id));
|
||||
}
|
||||
|
||||
$response = $this->checkACL($action, $request, $_format, $entity);
|
||||
@@ -119,8 +111,7 @@ class ApiController extends AbstractCRUDController
|
||||
}
|
||||
|
||||
if (null === $entity) {
|
||||
throw $this->createNotFoundException(sprintf('The %s with id %s '
|
||||
. 'is not found', $this->getCrudName(), $id));
|
||||
throw $this->createNotFoundException(sprintf('The %s with id %s is not found', $this->getCrudName(), $id));
|
||||
}
|
||||
|
||||
$response = $this->checkACL($action, $request, $_format, $entity);
|
||||
@@ -214,10 +205,11 @@ class ApiController extends AbstractCRUDController
|
||||
*
|
||||
* @param string action
|
||||
* @param mixed id
|
||||
* @param string $property the name of the property. This will be used to make a `add+$property` and `remove+$property` method
|
||||
* @param string $postedDataType the type of the posted data (the content)
|
||||
* @param string $property the name of the property. This will be used to make a `add+$property` and `remove+$property` method
|
||||
* @param string $postedDataType the type of the posted data (the content)
|
||||
* @param string $postedDataContext a context to deserialize posted data (the content)
|
||||
* @param bool $forcePersist force to persist the created element (only for POST request)
|
||||
* @param bool $forcePersist force to persist the created element (only for POST request)
|
||||
*
|
||||
* @throw BadRequestException if unable to deserialize the posted data
|
||||
* @throw BadRequestException if the method is not POST or DELETE
|
||||
*/
|
||||
@@ -252,15 +244,14 @@ class ApiController extends AbstractCRUDController
|
||||
try {
|
||||
$postedData = $this->getSerializer()->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);
|
||||
throw new BadRequestHttpException(sprintf('Unable to deserialize posted data: %s', $e->getMessage()), $e, 0);
|
||||
}
|
||||
|
||||
match ($request->getMethod()) {
|
||||
/* @phpstan-ignore-next-line */
|
||||
Request::METHOD_DELETE => $entity->{'remove' . ucfirst($property)}($postedData),
|
||||
/* @phpstan-ignore-next-line */
|
||||
Request::METHOD_POST => $entity->{'add' . ucfirst($property)}($postedData),
|
||||
/* @phpstan-ignore-next-line */
|
||||
Request::METHOD_DELETE => $entity->{'remove'.\ucfirst($property)}($postedData),
|
||||
/* @phpstan-ignore-next-line */
|
||||
Request::METHOD_POST => $entity->{'add'.\ucfirst($property)}($postedData),
|
||||
default => throw new BadRequestHttpException('this method is not supported'),
|
||||
};
|
||||
|
||||
@@ -277,7 +268,7 @@ class ApiController extends AbstractCRUDController
|
||||
return $this->json($errors, 422);
|
||||
}
|
||||
|
||||
if ($forcePersist && $request->getMethod() === Request::METHOD_POST) {
|
||||
if ($forcePersist && Request::METHOD_POST === $request->getMethod()) {
|
||||
$this->getDoctrine()->getManager()->persist($postedData);
|
||||
}
|
||||
|
||||
@@ -288,6 +279,7 @@ class ApiController extends AbstractCRUDController
|
||||
if ($response instanceof Response) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
return match ($request->getMethod()) {
|
||||
Request::METHOD_DELETE => $this->json('', Response::HTTP_OK),
|
||||
Request::METHOD_POST => $this->json(
|
||||
@@ -296,7 +288,7 @@ class ApiController extends AbstractCRUDController
|
||||
[],
|
||||
$this->getContextForSerializationPostAlter($action, $request, $_format, $entity, [$postedData])
|
||||
),
|
||||
default => throw new Exception('Unable to handle such request method.'),
|
||||
default => throw new \Exception('Unable to handle such request method.'),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -313,7 +305,7 @@ class ApiController extends AbstractCRUDController
|
||||
$default[AbstractNormalizer::OBJECT_TO_POPULATE] = $entity;
|
||||
}
|
||||
|
||||
$context = array_merge(
|
||||
$context = \array_merge(
|
||||
$default,
|
||||
$this->getContextForSerialization($action, $request, $_format, $entity)
|
||||
);
|
||||
@@ -438,7 +430,7 @@ class ApiController extends AbstractCRUDController
|
||||
return match ($request->getMethod()) {
|
||||
Request::METHOD_GET => ['groups' => ['read']],
|
||||
Request::METHOD_PUT, Request::METHOD_PATCH, Request::METHOD_POST => ['groups' => ['write']],
|
||||
default => throw new LogicException('get context for serialization is not implemented for this method'),
|
||||
default => throw new \LogicException('get context for serialization is not implemented for this method'),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -453,7 +445,6 @@ class ApiController extends AbstractCRUDController
|
||||
return ['groups' => ['read']];
|
||||
}
|
||||
|
||||
|
||||
protected function getSerializer(): SerializerInterface
|
||||
{
|
||||
return $this->get('serializer');
|
||||
|
@@ -19,7 +19,6 @@ use Chill\MainBundle\Security\Authorization\AuthorizationHelper;
|
||||
use Chill\MainBundle\Templating\Listing\FilterOrderHelper;
|
||||
use Chill\MainBundle\Templating\Listing\FilterOrderHelperFactoryInterface;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use LogicException;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
@@ -30,9 +29,6 @@ use Symfony\Component\Security\Core\Role\Role;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
use function array_key_exists;
|
||||
use function array_merge;
|
||||
|
||||
class CRUDController extends AbstractController
|
||||
{
|
||||
/**
|
||||
@@ -47,9 +43,6 @@ class CRUDController extends AbstractController
|
||||
return new Response($parameter);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
*/
|
||||
public function delete(Request $request, $id): Response
|
||||
{
|
||||
return $this->deleteAction('delete', $request, $id);
|
||||
@@ -75,7 +68,7 @@ class CRUDController extends AbstractController
|
||||
|
||||
public static function getSubscribedServices(): array
|
||||
{
|
||||
return array_merge(
|
||||
return \array_merge(
|
||||
parent::getSubscribedServices(),
|
||||
[
|
||||
'chill_main.paginator_factory' => PaginatorFactory::class,
|
||||
@@ -178,7 +171,6 @@ class CRUDController extends AbstractController
|
||||
* Throw an \Symfony\Component\Security\Core\Exception\AccessDeniedHttpException
|
||||
* if not accessible.
|
||||
*
|
||||
*
|
||||
* @throws \Symfony\Component\Security\Core\Exception\AccessDeniedHttpException
|
||||
*/
|
||||
protected function checkACL(string $action, mixed $entity)
|
||||
@@ -189,7 +181,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)')
|
||||
@@ -217,10 +209,8 @@ class CRUDController extends AbstractController
|
||||
*
|
||||
* It is preferable to override customizeForm instead of overriding
|
||||
* this method.
|
||||
*
|
||||
* @param string $formClass
|
||||
*/
|
||||
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);
|
||||
|
||||
@@ -239,7 +229,6 @@ class CRUDController extends AbstractController
|
||||
protected function customizeQuery(string $action, Request $request, $query): void {}
|
||||
|
||||
/**
|
||||
* @param $id
|
||||
* @param null $formClass
|
||||
*/
|
||||
protected function deleteAction(string $action, Request $request, $id, $formClass = null): Response
|
||||
@@ -255,13 +244,7 @@ class CRUDController extends AbstractController
|
||||
}
|
||||
|
||||
if (null === $entity) {
|
||||
throw $this->createNotFoundException(
|
||||
sprintf(
|
||||
'The %s with id %s is not found',
|
||||
$this->getCrudName(),
|
||||
$id
|
||||
)
|
||||
);
|
||||
throw $this->createNotFoundException(sprintf('The %s with id %s is not found', $this->getCrudName(), $id));
|
||||
}
|
||||
|
||||
$response = $this->checkACL($action, $entity);
|
||||
@@ -300,7 +283,7 @@ class CRUDController extends AbstractController
|
||||
return $result;
|
||||
}
|
||||
|
||||
return $this->redirectToRoute('chill_crud_' . $this->getCrudName() . '_view', ['id' => $entity->getId()]);
|
||||
return $this->redirectToRoute('chill_crud_'.$this->getCrudName().'_view', ['id' => $entity->getId()]);
|
||||
}
|
||||
|
||||
if ($form->isSubmitted()) {
|
||||
@@ -321,8 +304,6 @@ class CRUDController extends AbstractController
|
||||
|
||||
/**
|
||||
* Duplicate an entity.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function duplicateEntity(string $action, Request $request)
|
||||
{
|
||||
@@ -419,7 +400,7 @@ class CRUDController extends AbstractController
|
||||
return $result;
|
||||
}
|
||||
|
||||
return $this->redirectToRoute('chill_crud_' . $this->getCrudName() . '_view', ['id' => $entity->getId()]);
|
||||
return $this->redirectToRoute('chill_crud_'.$this->getCrudName().'_view', ['id' => $entity->getId()]);
|
||||
}
|
||||
|
||||
if ($form->isSubmitted()) {
|
||||
@@ -475,20 +456,15 @@ class CRUDController extends AbstractController
|
||||
* The parameters may be personnalized using `generateTemplateParameter`.
|
||||
*
|
||||
* @param class-string $formClass
|
||||
*
|
||||
* @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);
|
||||
|
||||
if (null === $entity) {
|
||||
throw $this->createNotFoundException(
|
||||
sprintf(
|
||||
'The %s with id %s is not found',
|
||||
$this->getCrudName(),
|
||||
$id
|
||||
)
|
||||
);
|
||||
throw $this->createNotFoundException(sprintf('The %s with id %s is not found', $this->getCrudName(), $id));
|
||||
}
|
||||
|
||||
$response = $this->checkACL($action, $entity);
|
||||
@@ -523,7 +499,7 @@ class CRUDController extends AbstractController
|
||||
return $result;
|
||||
}
|
||||
|
||||
return $this->redirectToRoute('chill_crud_' . $this->getCrudName() . '_index');
|
||||
return $this->redirectToRoute('chill_crud_'.$this->getCrudName().'_index');
|
||||
}
|
||||
|
||||
if ($form->isSubmitted()) {
|
||||
@@ -574,7 +550,6 @@ class CRUDController extends AbstractController
|
||||
/**
|
||||
* Customize template parameters.
|
||||
*
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function generateTemplateParameter(
|
||||
@@ -588,8 +563,6 @@ class CRUDController extends AbstractController
|
||||
|
||||
/**
|
||||
* Include services.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getActionConfig(string $action)
|
||||
{
|
||||
@@ -623,8 +596,6 @@ class CRUDController extends AbstractController
|
||||
* get the instance of the entity with the given id.
|
||||
*
|
||||
* @param string $id
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
protected function getEntity(mixed $action, $id, Request $request): ?object
|
||||
{
|
||||
@@ -679,15 +650,13 @@ class CRUDController extends AbstractController
|
||||
|
||||
/**
|
||||
* Get the result of the query.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getQueryResult(
|
||||
string $action,
|
||||
Request $request,
|
||||
int $totalItems,
|
||||
PaginatorInterface $paginator,
|
||||
?FilterOrderHelper $filterOrder = null
|
||||
FilterOrderHelper $filterOrder = null
|
||||
) {
|
||||
$query = $this->queryEntities($action, $request, $paginator, $filterOrder);
|
||||
|
||||
@@ -697,7 +666,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);
|
||||
@@ -712,7 +681,7 @@ class CRUDController extends AbstractController
|
||||
*/
|
||||
protected function getRoleFor($action)
|
||||
{
|
||||
if (array_key_exists('role', $this->getActionConfig($action))) {
|
||||
if (\array_key_exists('role', $this->getActionConfig($action))) {
|
||||
return $this->getActionConfig($action)['role'];
|
||||
}
|
||||
|
||||
@@ -727,11 +696,11 @@ class CRUDController extends AbstractController
|
||||
* and view.
|
||||
*
|
||||
* @param string $action
|
||||
* @param mixed $entity the entity for the current request, or an array of entities
|
||||
*
|
||||
* @throws LogicException if no template are available
|
||||
* @param mixed $entity the entity for the current request, or an array of entities
|
||||
*
|
||||
* @return string the path to the template
|
||||
*
|
||||
* @throws \LogicException if no template are available
|
||||
*/
|
||||
protected function getTemplateFor($action, mixed $entity, Request $request)
|
||||
{
|
||||
@@ -745,9 +714,7 @@ class CRUDController extends AbstractController
|
||||
'index' => '@ChillMain/CRUD/index.html.twig',
|
||||
'view' => '@ChillMain/CRUD/view.html.twig',
|
||||
'delete' => '@ChillMain/CRUD/delete.html.twig',
|
||||
default => throw new LogicException("the view for action {$action} is not "
|
||||
. 'defined. You should override ' . __METHOD__ . ' to add this '
|
||||
. 'action'),
|
||||
default => throw new \LogicException("the view for action {$action} is not ".'defined. You should override '.__METHOD__.' to add this action'),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -756,10 +723,6 @@ class CRUDController extends AbstractController
|
||||
return $this->container->get('translator');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $action
|
||||
* @param $entity
|
||||
*/
|
||||
protected function hasCustomTemplate($action, $entity, Request $request): bool
|
||||
{
|
||||
return !empty($this->getActionConfig($action)['template']);
|
||||
@@ -869,9 +832,9 @@ class CRUDController extends AbstractController
|
||||
$next = $request->request->get('submit', 'save-and-close');
|
||||
|
||||
return match ($next) {
|
||||
'save-and-close' => $this->redirectToRoute('chill_crud_' . $this->getCrudName() . '_index'),
|
||||
'save-and-new' => $this->redirectToRoute('chill_crud_' . $this->getCrudName() . '_new', $request->query->all()),
|
||||
default => $this->redirectToRoute('chill_crud_' . $this->getCrudName() . '_view', [
|
||||
'save-and-close' => $this->redirectToRoute('chill_crud_'.$this->getCrudName().'_index'),
|
||||
'save-and-new' => $this->redirectToRoute('chill_crud_'.$this->getCrudName().'_new', $request->query->all()),
|
||||
default => $this->redirectToRoute('chill_crud_'.$this->getCrudName().'_view', [
|
||||
'id' => $entity->getId(),
|
||||
]),
|
||||
};
|
||||
@@ -879,27 +842,16 @@ class CRUDController extends AbstractController
|
||||
|
||||
protected function onFormValid(string $action, object $entity, FormInterface $form, Request $request) {}
|
||||
|
||||
/**
|
||||
* @param $action
|
||||
* @param $entity
|
||||
*/
|
||||
protected function onPostCheckACL($action, Request $request, $entity): ?Response
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $action
|
||||
* @param $entity
|
||||
*/
|
||||
protected function onPostFetchEntity($action, Request $request, $entity): ?Response
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $entity
|
||||
*/
|
||||
protected function onPostFlush(string $action, $entity, FormInterface $form, Request $request) {}
|
||||
|
||||
/**
|
||||
@@ -912,21 +864,12 @@ class CRUDController extends AbstractController
|
||||
*/
|
||||
protected function onPostIndexFetchQuery(string $action, Request $request, int $totalItems, PaginatorInterface $paginator, mixed $entities) {}
|
||||
|
||||
/**
|
||||
* @param $entity
|
||||
*/
|
||||
protected function onPostPersist(string $action, $entity, FormInterface $form, Request $request) {}
|
||||
|
||||
/**
|
||||
* @param $entity
|
||||
*/
|
||||
protected function onPostRemove(string $action, $entity, FormInterface $form, Request $request) {}
|
||||
|
||||
protected function onPreDelete(string $action, Request $request) {}
|
||||
|
||||
/**
|
||||
* @param $entity
|
||||
*/
|
||||
protected function onPreFlush(string $action, $entity, FormInterface $form, Request $request) {}
|
||||
|
||||
protected function onPreIndex(string $action, Request $request) {}
|
||||
@@ -936,14 +879,8 @@ class CRUDController extends AbstractController
|
||||
*/
|
||||
protected function onPreIndexBuildQuery(string $action, Request $request, int $totalItems, PaginatorInterface $paginator) {}
|
||||
|
||||
/**
|
||||
* @param $entity
|
||||
*/
|
||||
protected function onPrePersist(string $action, $entity, FormInterface $form, Request $request) {}
|
||||
|
||||
/**
|
||||
* @param $entity
|
||||
*/
|
||||
protected function onPreRemove(string $action, $entity, FormInterface $form, Request $request) {}
|
||||
|
||||
/**
|
||||
@@ -969,7 +906,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())
|
||||
@@ -979,9 +916,6 @@ class CRUDController extends AbstractController
|
||||
return $this->orderQuery($action, $query, $request, $paginator);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $entity
|
||||
*/
|
||||
protected function removeEntity(string $action, $entity, FormInterface $form, Request $request)
|
||||
{
|
||||
$this->getDoctrine()
|
||||
@@ -1021,13 +955,7 @@ class CRUDController extends AbstractController
|
||||
}
|
||||
|
||||
if (null === $entity) {
|
||||
throw $this->createNotFoundException(
|
||||
sprintf(
|
||||
'The %s with id %s is not found',
|
||||
$this->getCrudName(),
|
||||
$id
|
||||
)
|
||||
);
|
||||
throw $this->createNotFoundException(sprintf('The %s with id %s is not found', $this->getCrudName(), $id));
|
||||
}
|
||||
|
||||
$response = $this->checkACL($action, $entity);
|
||||
|
@@ -12,10 +12,6 @@ declare(strict_types=1);
|
||||
namespace Chill\MainBundle\CRUD\Resolver;
|
||||
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use LogicException;
|
||||
|
||||
use function array_key_exists;
|
||||
use function strtoupper;
|
||||
|
||||
/**
|
||||
* Class Resolver.
|
||||
@@ -65,28 +61,22 @@ class Resolver
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $crudName
|
||||
* @param $action
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function buildDefaultRole($crudName, $action)
|
||||
{
|
||||
if (empty($this->crudConfig[$crudName]['base_role'])) {
|
||||
throw new LogicException(sprintf('the base role is not defined. You must define '
|
||||
. 'on or override %s or %s methods', __METHOD__, 'getRoleFor'));
|
||||
throw new \LogicException(sprintf('the base role is not defined. You must define on or override %s or %s methods', __METHOD__, 'getRoleFor'));
|
||||
}
|
||||
|
||||
return strtoupper(
|
||||
$this->crudConfig[$crudName]['base_role'] .
|
||||
'_' .
|
||||
return \strtoupper(
|
||||
$this->crudConfig[$crudName]['base_role'].
|
||||
'_'.
|
||||
$action
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $crudName
|
||||
* @param null $action
|
||||
*
|
||||
* @return string
|
||||
@@ -102,14 +92,11 @@ class Resolver
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $crudName
|
||||
* @param $action
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasAction($crudName, $action)
|
||||
{
|
||||
return array_key_exists(
|
||||
return \array_key_exists(
|
||||
$action,
|
||||
$this->crudConfig[$crudName]['actions']
|
||||
);
|
||||
|
@@ -11,20 +11,11 @@ declare(strict_types=1);
|
||||
|
||||
namespace Chill\MainBundle\CRUD\Routing;
|
||||
|
||||
use RuntimeException;
|
||||
use Symfony\Component\Config\Loader\Loader;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\Routing\Route;
|
||||
use Symfony\Component\Routing\RouteCollection;
|
||||
|
||||
use function array_filter;
|
||||
use function array_keys;
|
||||
use function array_search;
|
||||
use function count;
|
||||
use function in_array;
|
||||
|
||||
use const ARRAY_FILTER_USE_BOTH;
|
||||
|
||||
class CRUDRoutesLoader extends Loader
|
||||
{
|
||||
private const ALL_INDEX_METHODS = [Request::METHOD_GET, Request::METHOD_HEAD];
|
||||
@@ -46,13 +37,12 @@ class CRUDRoutesLoader extends Loader
|
||||
/**
|
||||
* Load routes for CRUD and CRUD Api.
|
||||
*
|
||||
* @param mixed $resource
|
||||
* @param mixed|null $type
|
||||
*/
|
||||
public function load($resource, $type = null): RouteCollection
|
||||
{
|
||||
if (true === $this->isLoaded) {
|
||||
throw new RuntimeException('Do not add the "CRUD" loader twice');
|
||||
throw new \RuntimeException('Do not add the "CRUD" loader twice');
|
||||
}
|
||||
|
||||
$collection = new RouteCollection();
|
||||
@@ -69,7 +59,6 @@ class CRUDRoutesLoader extends Loader
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $resource
|
||||
* @param null $type
|
||||
*
|
||||
* @return bool
|
||||
@@ -81,13 +70,11 @@ class CRUDRoutesLoader extends Loader
|
||||
|
||||
/**
|
||||
* Load routes for api single.
|
||||
*
|
||||
* @param $crudConfig
|
||||
*/
|
||||
protected function loadApi(array $crudConfig): RouteCollection
|
||||
{
|
||||
$collection = new RouteCollection();
|
||||
$controller = 'csapi_' . $crudConfig['name'] . '_controller';
|
||||
$controller = 'csapi_'.$crudConfig['name'].'_controller';
|
||||
|
||||
foreach ($crudConfig['actions'] as $name => $action) {
|
||||
// filter only on single actions
|
||||
@@ -100,40 +87,38 @@ class CRUDRoutesLoader extends Loader
|
||||
$controllerAction = match ($name) {
|
||||
'_entity' => 'entityApi',
|
||||
'_index' => 'indexApi',
|
||||
default => $name . 'Api',
|
||||
default => $name.'Api',
|
||||
};
|
||||
|
||||
$defaults = [
|
||||
'_controller' => $controller . ':' . ($action['controller_action'] ?? $controllerAction),
|
||||
'_controller' => $controller.':'.($action['controller_action'] ?? $controllerAction),
|
||||
];
|
||||
|
||||
// path are rewritten
|
||||
// if name === 'default', we rewrite it to nothing :-)
|
||||
$localName = in_array($name, ['_entity', '_index'], true) ? '' : '/' . $name;
|
||||
$localName = \in_array($name, ['_entity', '_index'], true) ? '' : '/'.$name;
|
||||
|
||||
if ('collection' === $action['single_collection'] || '_index' === $name) {
|
||||
$localPath = $action['path'] ?? $localName . '.{_format}';
|
||||
$localPath = $action['path'] ?? $localName.'.{_format}';
|
||||
} else {
|
||||
$localPath = $action['path'] ?? '/{id}' . $localName . '.{_format}';
|
||||
$localPath = $action['path'] ?? '/{id}'.$localName.'.{_format}';
|
||||
}
|
||||
$path = $crudConfig['base_path'] . $localPath;
|
||||
$path = $crudConfig['base_path'].$localPath;
|
||||
|
||||
$requirements = $action['requirements'] ?? ['{id}' => '\d+'];
|
||||
|
||||
$methods = array_keys(array_filter(
|
||||
$methods = \array_keys(\array_filter(
|
||||
$action['methods'],
|
||||
static fn ($value, $key) => $value,
|
||||
ARRAY_FILTER_USE_BOTH
|
||||
\ARRAY_FILTER_USE_BOTH
|
||||
));
|
||||
|
||||
if (count($methods) === 0) {
|
||||
throw new RuntimeException("The api configuration named \"{$crudConfig['name']}\", action \"{$name}\", " .
|
||||
'does not have any allowed methods. You should remove this action from the config ' .
|
||||
'or allow, at least, one method');
|
||||
if (0 === \count($methods)) {
|
||||
throw new \RuntimeException("The api configuration named \"{$crudConfig['name']}\", action \"{$name}\", ".'does not have any allowed methods. You should remove this action from the config or allow, at least, one method');
|
||||
}
|
||||
|
||||
if ('_entity' === $name && in_array(Request::METHOD_POST, $methods, true)) {
|
||||
unset($methods[array_search(Request::METHOD_POST, $methods, true)]);
|
||||
if ('_entity' === $name && \in_array(Request::METHOD_POST, $methods, true)) {
|
||||
unset($methods[\array_search(Request::METHOD_POST, $methods, true)]);
|
||||
$entityPostRoute = $this->createEntityPostRoute(
|
||||
$name,
|
||||
$crudConfig,
|
||||
@@ -146,7 +131,7 @@ class CRUDRoutesLoader extends Loader
|
||||
);
|
||||
}
|
||||
|
||||
if (count($methods) === 0) {
|
||||
if (0 === \count($methods)) {
|
||||
// the only method was POST,
|
||||
// continue to next
|
||||
continue;
|
||||
@@ -155,7 +140,7 @@ class CRUDRoutesLoader extends Loader
|
||||
$route = new Route($path, $defaults, $requirements);
|
||||
$route->setMethods($methods);
|
||||
|
||||
$collection->add('chill_api_single_' . $crudConfig['name'] . '_' . $name, $route);
|
||||
$collection->add('chill_api_single_'.$crudConfig['name'].'_'.$name, $route);
|
||||
}
|
||||
|
||||
return $collection;
|
||||
@@ -163,35 +148,33 @@ class CRUDRoutesLoader extends Loader
|
||||
|
||||
/**
|
||||
* Load routes for CRUD (without api).
|
||||
*
|
||||
* @param $crudConfig
|
||||
*/
|
||||
protected function loadCrudConfig($crudConfig): RouteCollection
|
||||
{
|
||||
$collection = new RouteCollection();
|
||||
$controller = 'cscrud_' . $crudConfig['name'] . '_controller';
|
||||
$controller = 'cscrud_'.$crudConfig['name'].'_controller';
|
||||
|
||||
foreach ($crudConfig['actions'] as $name => $action) {
|
||||
// defaults (controller name)
|
||||
$defaults = [
|
||||
'_controller' => $controller . ':' . ($action['controller_action'] ?? $name),
|
||||
'_controller' => $controller.':'.($action['controller_action'] ?? $name),
|
||||
];
|
||||
|
||||
if ('index' === $name) {
|
||||
$path = '{_locale}' . $crudConfig['base_path'];
|
||||
$path = '{_locale}'.$crudConfig['base_path'];
|
||||
$route = new Route($path, $defaults);
|
||||
} elseif ('new' === $name) {
|
||||
$path = '{_locale}' . $crudConfig['base_path'] . '/' . $name;
|
||||
$path = '{_locale}'.$crudConfig['base_path'].'/'.$name;
|
||||
$route = new Route($path, $defaults);
|
||||
} else {
|
||||
$path = '{_locale}' . $crudConfig['base_path'] . ($action['path'] ?? '/{id}/' . $name);
|
||||
$path = '{_locale}'.$crudConfig['base_path'].($action['path'] ?? '/{id}/'.$name);
|
||||
$requirements = $action['requirements'] ?? [
|
||||
'{id}' => '\d+',
|
||||
];
|
||||
$route = new Route($path, $defaults, $requirements);
|
||||
}
|
||||
|
||||
$collection->add('chill_crud_' . $crudConfig['name'] . '_' . $name, $route);
|
||||
$collection->add('chill_crud_'.$crudConfig['name'].'_'.$name, $route);
|
||||
}
|
||||
|
||||
return $collection;
|
||||
@@ -199,11 +182,11 @@ class CRUDRoutesLoader extends Loader
|
||||
|
||||
private function createEntityPostRoute(string $name, $crudConfig, array $action, $controller): Route
|
||||
{
|
||||
$localPath = $action['path'] . '.{_format}';
|
||||
$localPath = $action['path'].'.{_format}';
|
||||
$defaults = [
|
||||
'_controller' => $controller . ':' . ($action['controller_action'] ?? 'entityPost'),
|
||||
'_controller' => $controller.':'.($action['controller_action'] ?? 'entityPost'),
|
||||
];
|
||||
$path = $crudConfig['base_path'] . $localPath;
|
||||
$path = $crudConfig['base_path'].$localPath;
|
||||
$requirements = $action['requirements'] ?? [];
|
||||
$route = new Route($path, $defaults, $requirements);
|
||||
$route->setMethods([Request::METHOD_POST]);
|
||||
|
@@ -35,8 +35,6 @@ class TwigCRUDResolver extends AbstractExtension
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $configKey
|
||||
* @param $crudName
|
||||
* @param null $action
|
||||
*
|
||||
* @return string
|
||||
@@ -66,9 +64,6 @@ class TwigCRUDResolver extends AbstractExtension
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $crudName
|
||||
* @param $action
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasAction($crudName, $action)
|
||||
|
Reference in New Issue
Block a user