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

Open in your IDE?
  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.      * @var MarkupParserInterface
  22.      */
  23.     private $markupParser;
  24.     /**
  25.      * @param MarkupParserInterface[] $markupParser
  26.      */
  27.     public function __construct(array $markupParser)
  28.     {
  29.         $this->markupParser $markupParser;
  30.     }
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [KernelEvents::RESPONSE => ['replaceMarkup', -10]];
  34.     }
  35.     /**
  36.      * Parses content of response and set the replaced html as new content.
  37.      */
  38.     public function replaceMarkup(ResponseEvent $event)
  39.     {
  40.         $request $event->getRequest();
  41.         $response $event->getResponse();
  42.         $format $request->getRequestFormat();
  43.         $content $response->getContent();
  44.         if (!$content || !\array_key_exists($format$this->markupParser)) {
  45.             return;
  46.         }
  47.         $response->setContent(
  48.             $this->markupParser[$format]->parse($content$request->getLocale())
  49.         );
  50.     }
  51. }