vendor/easycorp/easyadmin-bundle/src/ArgumentResolver/BatchActionDtoResolver.php line 40

  1. <?php
  2. namespace EasyCorp\Bundle\EasyAdminBundle\ArgumentResolver;
  3. use EasyCorp\Bundle\EasyAdminBundle\Config\Option\EA;
  4. use EasyCorp\Bundle\EasyAdminBundle\Dto\BatchActionDto;
  5. use EasyCorp\Bundle\EasyAdminBundle\Provider\AdminContextProvider;
  6. use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
  9. use Symfony\Component\HttpKernel\Controller\ValueResolverInterface;
  10. use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
  11. /*
  12.  * @author Javier Eguiluz <javier.eguiluz@gmail.com>
  13.  */
  14. if (interface_exists(ValueResolverInterface::class)) {
  15.     final class BatchActionDtoResolver implements ValueResolverInterface
  16.     {
  17.         private AdminContextProvider $adminContextProvider;
  18.         private AdminUrlGenerator $adminUrlGenerator;
  19.         public function __construct(AdminContextProvider $adminContextProviderAdminUrlGenerator $adminUrlGenerator)
  20.         {
  21.             $this->adminContextProvider $adminContextProvider;
  22.             $this->adminUrlGenerator $adminUrlGenerator;
  23.         }
  24.         public function resolve(Request $requestArgumentMetadata $argument): iterable
  25.         {
  26.             if (BatchActionDto::class !== $argument->getType()) {
  27.                 return [];
  28.             }
  29.             if (null === $context $this->adminContextProvider->getContext()) {
  30.                 throw new \RuntimeException(sprintf('Some of your controller actions have type-hinted an argument with the "%s" class but that\'s only available for actions run to serve EasyAdmin requests. Remove the type-hint or make sure the action is part of an EasyAdmin request.'BatchActionDto::class));
  31.             }
  32.             $batchActionUrl $context->getRequest()->request->get(EA::BATCH_ACTION_URL);
  33.             $batchActionUrlQueryString parse_url($batchActionUrl\PHP_URL_QUERY);
  34.             parse_str($batchActionUrlQueryString$batchActionUrlParts);
  35.             $referrerUrl $batchActionUrlParts[EA::REFERRER] ?? $this->adminUrlGenerator->unsetAll()->generateUrl();
  36.             yield new BatchActionDto(
  37.                 $context->getRequest()->request->get(EA::BATCH_ACTION_NAME),
  38.                 $context->getRequest()->request->all()[EA::BATCH_ACTION_ENTITY_IDS] ?? [],
  39.                 $context->getRequest()->request->get(EA::ENTITY_FQCN),
  40.                 $referrerUrl,
  41.                 $context->getRequest()->request->get(EA::BATCH_ACTION_CSRF_TOKEN)
  42.             );
  43.         }
  44.     }
  45. } else {
  46.     final class BatchActionDtoResolver implements ArgumentValueResolverInterface
  47.     {
  48.         private AdminContextProvider $adminContextProvider;
  49.         private AdminUrlGenerator $adminUrlGenerator;
  50.         public function __construct(AdminContextProvider $adminContextProviderAdminUrlGenerator $adminUrlGenerator)
  51.         {
  52.             $this->adminContextProvider $adminContextProvider;
  53.             $this->adminUrlGenerator $adminUrlGenerator;
  54.         }
  55.         public function supports(Request $requestArgumentMetadata $argument): bool
  56.         {
  57.             return BatchActionDto::class === $argument->getType();
  58.         }
  59.         public function resolve(Request $requestArgumentMetadata $argument): iterable
  60.         {
  61.             if (null === $context $this->adminContextProvider->getContext()) {
  62.                 throw new \RuntimeException(sprintf('Some of your controller actions have type-hinted an argument with the "%s" class but that\'s only available for actions run to serve EasyAdmin requests. Remove the type-hint or make sure the action is part of an EasyAdmin request.'BatchActionDto::class));
  63.             }
  64.             $batchActionUrl $context->getRequest()->request->get(EA::BATCH_ACTION_URL);
  65.             $batchActionUrlQueryString parse_url($batchActionUrl\PHP_URL_QUERY);
  66.             parse_str($batchActionUrlQueryString$batchActionUrlParts);
  67.             $referrerUrl $batchActionUrlParts[EA::REFERRER] ?? $this->adminUrlGenerator->unsetAll()->generateUrl();
  68.             yield new BatchActionDto(
  69.                 $context->getRequest()->request->get(EA::BATCH_ACTION_NAME),
  70.                 $context->getRequest()->request->all()[EA::BATCH_ACTION_ENTITY_IDS] ?? [],
  71.                 $context->getRequest()->request->get(EA::ENTITY_FQCN),
  72.                 $referrerUrl,
  73.                 $context->getRequest()->request->get(EA::BATCH_ACTION_CSRF_TOKEN)
  74.             );
  75.         }
  76.     }
  77. }