vendor/sulu/sulu/src/Sulu/Bundle/MarkupBundle/Listener/MarkupListener.php line 45

  1. <?php
  2. /*
  3.  * This file is part of Sulu.
  4.  *
  5.  * (c) Sulu GmbH
  6.  *
  7.  * This source file is subject to the MIT license that is bundled
  8.  * with this source code in the file LICENSE.
  9.  */
  10. namespace Sulu\Bundle\MarkupBundle\Listener;
  11. use Sulu\Bundle\MarkupBundle\Markup\MarkupParserInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  14. use Symfony\Component\HttpKernel\KernelEvents;
  15. /**
  16.  * Parses content of response and set the replaced html as new content.
  17.  */
  18. class MarkupListener implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @param array<string, MarkupParserInterface> $markupParser
  22.      */
  23.     public function __construct(private array $markupParser)
  24.     {
  25.     }
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [KernelEvents::RESPONSE => ['replaceMarkup', -10]];
  29.     }
  30.     /**
  31.      * Parses content of response and set the replaced html as new content.
  32.      */
  33.     public function replaceMarkup(ResponseEvent $event)
  34.     {
  35.         $request $event->getRequest();
  36.         $response $event->getResponse();
  37.         $format $request->getRequestFormat();
  38.         $content $response->getContent();
  39.         if (!$content || !\array_key_exists($format$this->markupParser)) {
  40.             return;
  41.         }
  42.         $response->setContent(
  43.             $this->markupParser[$format]->parse($content$request->getLocale())
  44.         );
  45.     }
  46. }