vendor/sonata-project/twig-extensions/src/FlashMessage/FlashManager.php line 102

  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of the Sonata Project package.
  5.  *
  6.  * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  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 Sonata\Twig\FlashMessage;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use Symfony\Component\HttpFoundation\Session\Session;
  14. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  15. /**
  16.  * @author Vincent Composieux <composieux@ekino.com>
  17.  */
  18. final class FlashManager implements FlashManagerInterface
  19. {
  20.     /**
  21.      * @param array<string, array<string>> $types      Sonata flash message types array (defined in configuration)
  22.      * @param array<string, string>        $cssClasses Css classes associated with $types
  23.      */
  24.     public function __construct(
  25.         private RequestStack $requestStack,
  26.         private array $types,
  27.         private array $cssClasses
  28.     ) {
  29.     }
  30.     /**
  31.      * Returns Sonata flash message types.
  32.      */
  33.     public function getTypes(): array
  34.     {
  35.         return $this->types;
  36.     }
  37.     /**
  38.      * Returns flash bag messages for correct type after renaming with Sonata flash message type.
  39.      */
  40.     public function get(string $type): array
  41.     {
  42.         $this->handle();
  43.         return $this->getSession()->getFlashBag()->get($type);
  44.     }
  45.     /**
  46.      * Gets handled Sonata flash message types.
  47.      */
  48.     public function getHandledTypes(): array
  49.     {
  50.         return array_keys($this->getTypes());
  51.     }
  52.     public function getRenderedHtmlClassAttribute(string $typestring $default ''): string
  53.     {
  54.         return \array_key_exists($type$this->cssClasses)
  55.             ? $this->cssClasses[$type]
  56.             : $default;
  57.     }
  58.     public function handlesType(string $type): bool
  59.     {
  60.         return \array_key_exists($type$this->cssClasses);
  61.     }
  62.     /**
  63.      * Add flash message to session.
  64.      */
  65.     public function addFlash(string $typestring $message): void
  66.     {
  67.         $this->getSession()->getFlashBag()->add($type$message);
  68.     }
  69.     /**
  70.      * Handles flash bag types renaming.
  71.      */
  72.     private function handle(): void
  73.     {
  74.         foreach ($this->getTypes() as $type => $values) {
  75.             foreach ($values as $value) {
  76.                 $this->rename($type$value);
  77.             }
  78.         }
  79.     }
  80.     /**
  81.      * Process Sonata flash message type rename.
  82.      *
  83.      * @param string $type  Sonata flash message type
  84.      * @param string $value Original flash message type
  85.      */
  86.     private function rename(string $typestring $value): void
  87.     {
  88.         $flashBag $this->getSession()->getFlashBag();
  89.         foreach ($flashBag->get($value) as $message) {
  90.             $flashBag->add($type$message);
  91.         }
  92.     }
  93.     /**
  94.      * Returns Symfony session service.
  95.      *
  96.      * @return Session
  97.      */
  98.     private function getSession(): SessionInterface
  99.     {
  100.         // @phpstan-ignore-next-line
  101.         if (method_exists($this->requestStack'getMainRequest')) {
  102.             $request $this->requestStack->getMainRequest();
  103.         } else {
  104.             // @phpstan-ignore-next-line
  105.             $request $this->requestStack->getMasterRequest();
  106.         }
  107.         if (null === $request) {
  108.             throw new \RuntimeException('No request was found.');
  109.         }
  110.         $session $request->getSession();
  111.         if (!$session instanceof Session) {
  112.             throw new \UnexpectedValueException(sprintf(
  113.                 'The flash manager only works with a "%s" session.',
  114.                 Session::class
  115.             ));
  116.         }
  117.         return $session;
  118.     }
  119. }