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 array<string, MarkupParserInterface>
  22. */
  23. private array $markupParser;
  24. /**
  25. * @param iterable<string, MarkupParserInterface> $parsers
  26. */
  27. public function __construct(iterable $parsers)
  28. {
  29. $this->markupParser = [...$parsers];
  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. /** @var string $format */
  43. $format = $request->getRequestFormat();
  44. $content = $response->getContent();
  45. if (!$content || !\array_key_exists($format, $this->markupParser)) {
  46. return;
  47. }
  48. /** @var MarkupParserInterface $markupParser */
  49. $markupParser = $this->markupParser[$format];
  50. $response->setContent($markupParser->parse($content, $request->getLocale()));
  51. }
  52. }