local/modules/ShortCode/EventListener/ResponseListener.php line 48

  1. <?php
  2. /*
  3.  * This file is part of the Thelia package.
  4.  * http://www.thelia.net
  5.  *
  6.  * (c) OpenStudio <info@thelia.net>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace ShortCode\EventListener;
  12. use Maiorano\Shortcodes\Library\SimpleShortcode;
  13. use Maiorano\Shortcodes\Manager\ShortcodeManager;
  14. use ShortCode\Event\ShortCodeEvent;
  15. use ShortCode\Model\ShortCode;
  16. use ShortCode\Model\ShortCodeQuery;
  17. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  20. use Symfony\Component\HttpFoundation\JsonResponse;
  21. use Symfony\Component\HttpFoundation\RedirectResponse;
  22. use Symfony\Component\HttpFoundation\StreamedResponse;
  23. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  24. use Symfony\Component\HttpKernel\KernelEvents;
  25. use Thelia\Log\Tlog;
  26. class ResponseListener implements EventSubscriberInterface
  27. {
  28.     /** @var EventDispatcherInterface */
  29.     protected $eventDispatcher;
  30.     public function __construct(
  31.         EventDispatcherInterface $eventDispatcher
  32.     ) {
  33.         $this->eventDispatcher $eventDispatcher;
  34.     }
  35.     public static function getSubscribedEvents()
  36.     {
  37.         return [
  38.             KernelEvents::RESPONSE => [['dispatchShortCodeEvents'64]],
  39.         ];
  40.     }
  41.     public function dispatchShortCodeEvents(ResponseEvent $event): void
  42.     {
  43.         if ($event->getRequest()->get('disable_shortcode'0) == 1) {
  44.             return;
  45.         }
  46.         $response $event->getResponse();
  47.         if (
  48.             $response instanceof BinaryFileResponse
  49.             || $response instanceof StreamedResponse
  50.             || $response instanceof RedirectResponse
  51.             || $response instanceof JsonResponse
  52.         ) {
  53.             return;
  54.         }
  55.         $dispatcher $this->eventDispatcher;
  56.         $simpleShortCodes = [];
  57.         $shortCodes ShortCodeQuery::create()
  58.             ->filterByActive(1)
  59.             ->find();
  60.         /** @var ShortCode $shortCode */
  61.         foreach ($shortCodes as $shortCode) {
  62.             $simpleShortCodes[$shortCode->getTag()] = new SimpleShortcode($shortCode->getTag(), null, function ($content$attributes) use ($shortCode$dispatcher) {
  63.                 $shortCodeEvent = new ShortCodeEvent($content$attributes);
  64.                 $dispatcher->dispatch($shortCodeEvent$shortCode->getEvent());
  65.                 return $shortCodeEvent->getResult();
  66.             });
  67.         }
  68.         $manager = new ShortcodeManager($simpleShortCodes);
  69.         $content $response->getContent();
  70.         try {
  71.             $content $manager->doShortCode($contentnulltrue);
  72.         } catch (\Exception $exception) {
  73.             Tlog::getInstance()->error($exception->getMessage());
  74.         }
  75.         $response->setContent($content);
  76.     }
  77. }