src/EventSubscriber/PragmaActionsDisabler/PragmaBoActionsDisablerSubscriber.php line 60

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\EventSubscriber\PragmaActionsDisabler;
  4. use App\Helper\Response\ResponseInterface as CustomResponseInterface;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  9. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  10. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. class PragmaBoActionsDisablerSubscriber implements EventSubscriberInterface
  13. {
  14.     private const ROUTES_TO_DISABLE = [
  15.         'foxorders_shop_menu_index',
  16.         'foxorders_shop_menu_show',
  17.         'foxorders_shop_category_index',
  18.         'foxorders_shop_product_index',
  19.         'foxorders_shop_option_index',
  20.     ];
  21.     public function onKernelResponse(ResponseEvent $event): void
  22.     {
  23.         if (false === $event->isMainRequest()) {
  24.             return;
  25.         }
  26.         $request $event->getRequest();
  27.         if (false === $this->isPragmaSource($request)) {
  28.             return;
  29.         }
  30.         $route $request->attributes->get('_route');
  31.         if (false === \in_array($routeself::ROUTES_TO_DISABLEtrue)) {
  32.             return;
  33.         }
  34.         $response $event->getResponse();
  35.         if (false === $response instanceof Response) {
  36.             return;
  37.         }
  38.         $content $response->getContent();
  39.         if (false === str_contains($content'<body')) {
  40.             return;
  41.         }
  42.         $content preg_replace(
  43.             '/<body([^>]*)>/',
  44.             '<body$1 data-buttons-disabled>',
  45.             $content
  46.         );
  47.         $response->setContent($content);
  48.     }
  49.     public function onKernelController(ControllerEvent $event): void
  50.     {
  51.         if (false === $event->isMainRequest()) {
  52.             return;
  53.         }
  54.         if (false === $this->isPragmaSource($event->getRequest())) {
  55.             return;
  56.         }
  57.         [$controllerObject$methodName] = $this->getControllerInfos($event->getController());
  58.         if (null === $controllerObject) {
  59.             return;
  60.         }
  61.         if (false === $this->hasCheckActionsAllowedAttribute($controllerObject$methodName)) {
  62.             return;
  63.         }
  64.         throw new AccessDeniedHttpException(CustomResponseInterface::NOT_ALLOWED);
  65.     }
  66.     public static function getSubscribedEvents()
  67.     {
  68.         return [
  69.             KernelEvents::CONTROLLER => 'onKernelController',
  70.             KernelEvents::RESPONSE => 'onKernelResponse',
  71.         ];
  72.     }
  73.     private function getControllerInfos(mixed $controller): ?array
  74.     {
  75.         if (\is_array($controller) && \count($controller) && \is_object($controller[0]) && \is_string($controller[1])) {
  76.             return [$controller[0], $controller[1]];
  77.         }
  78.         return [nullnull];
  79.     }
  80.     private function hasCheckActionsAllowedAttribute(object $controllerstring $method): bool
  81.     {
  82.         try {
  83.             return [] !== (new \ReflectionMethod($controller$method))->getAttributes(CheckActionsAllowed::class);
  84.         } catch (\ReflectionException) {
  85.             return false;
  86.         }
  87.     }
  88.     private function isPragmaSource(Request $request): bool
  89.     {
  90.         return false;
  91.         // $session = $request->getSession();
  92.         // if (null === $session) {
  93.         //     return false;
  94.         // }
  95.         // $source = $session->get('shop')?->getSource();
  96.         // return null !== $source && \in_array($source->getCode(), SourceInterface::SOURCE_PRAGMA, true);
  97.     }
  98. }