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

Open in your IDE?
  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 Sonata\Twig\Status\StatusClassRendererInterface;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\HttpFoundation\Session\Session;
  15. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  16. /**
  17.  * @author Vincent Composieux <composieux@ekino.com>
  18.  *
  19.  * NEXT_MAJOR: remove StatusClassRendererInterface implementation
  20.  */
  21. final class FlashManager implements FlashManagerInterfaceStatusClassRendererInterface
  22. {
  23.     /**
  24.      * NEXT_MAJOR: Restrict to RequestStack.
  25.      *
  26.      * @var RequestStack|Session
  27.      */
  28.     private $requestStackOrDeprecatedSession;
  29.     /**
  30.      * @var array<string, array<string, array<string, mixed>>>
  31.      */
  32.     private array $types;
  33.     /**
  34.      * @var array<string, string>
  35.      */
  36.     private array $cssClasses;
  37.     /**
  38.      * @param Session|RequestStack                               $requestStackOrDeprecatedSession
  39.      * @param array<string, array<string, array<string, mixed>>> $types                           Sonata flash message types array (defined in configuration)
  40.      * @param array<string, string>                              $cssClasses                      Css classes associated with $types
  41.      */
  42.     public function __construct($requestStackOrDeprecatedSession, array $types, array $cssClasses)
  43.     {
  44.         if ($requestStackOrDeprecatedSession instanceof Session) {
  45.             @trigger_error(sprintf(
  46.                 'Passing "%s" as $session to "%s" method is deprecated since sonata-project/twig-extensions 1.7'
  47.                 .' and will be removed in 2.0. Pass "%s" instead.',
  48.                 Session::class,
  49.                 __METHOD__,
  50.                 RequestStack::class
  51.             ), \E_USER_DEPRECATED);
  52.             $this->requestStackOrDeprecatedSession $requestStackOrDeprecatedSession;
  53.         } elseif ($requestStackOrDeprecatedSession instanceof RequestStack) {
  54.             // NEXT_MAJOR: keep this block only
  55.             // NEXT_MAJOR: add \Symfony\Component\HttpFoundation\RequestStack typehint to $requestStackOrDeprecatedSession
  56.             // NEXT_MAJOR: rename $requestStackOrDeprecatedSession to $requestStack
  57.             $this->requestStackOrDeprecatedSession $requestStackOrDeprecatedSession;
  58.         } else {
  59.             throw new \InvalidArgumentException(
  60.                 sprintf(
  61.                     'Argument $code of "%s" method should be "%s" or "%s", "%s" provided.',
  62.                     __METHOD__,
  63.                     RequestStack::class,
  64.                     Session::class,
  65.                     \is_object($requestStackOrDeprecatedSession) ? \get_class($requestStackOrDeprecatedSession) : \gettype($requestStackOrDeprecatedSession)
  66.                 )
  67.             );
  68.         }
  69.         $this->types $types;
  70.         $this->cssClasses $cssClasses;
  71.     }
  72.     /**
  73.      * Tells if class may handle $object for status class rendering.
  74.      *
  75.      * @deprecated since sonata-project/twig-extensions 1.4, will be removed in 2.0. Use handlesType() instead.
  76.      *
  77.      * NEXT_MAJOR: remove this method
  78.      *
  79.      * @param object|string $object     FlashManager or Sonata flash message type
  80.      * @param string|null   $statusName Sonata flash message type
  81.      *
  82.      * @return bool
  83.      */
  84.     public function handlesObject($object, ?string $statusName null)
  85.     {
  86.         @trigger_error(sprintf(
  87.             'The "%s()" method is deprecated since sonata-project/twig-extensions 1.4'
  88.             .' and will be removed in version 2.0. Use "%s" instead.',
  89.             __METHOD__,
  90.             'handlesType()'
  91.         ), \E_USER_DEPRECATED);
  92.         if (\is_string($object)) {
  93.             if (null === $statusName) {
  94.                 $statusName $object;
  95.             }
  96.             $object $this;
  97.         }
  98.         if (!$object instanceof self) {
  99.             return false;
  100.         }
  101.         if (null === $statusName) {
  102.             return false;
  103.         }
  104.         return $this->handlesType($statusName);
  105.     }
  106.     /**
  107.      * Returns the status CSS class for $object.
  108.      *
  109.      * @deprecated since sonata-project/twig-extensions 1.4, will be removed in 2.0. Use getRenderedHtmlClassAttribute() instead.
  110.      *
  111.      * NEXT_MAJOR: remove this method
  112.      *
  113.      * @param object|string $object     FlashManager or Sonata flash message type
  114.      * @param string|null   $statusName Sonata flash message type
  115.      * @param string        $default    Default status class if Sonata flash message type do not exist
  116.      *
  117.      * @return string
  118.      */
  119.     public function getStatusClass($object, ?string $statusName nullstring $default '')
  120.     {
  121.         @trigger_error(sprintf(
  122.             'The "%s()" method is deprecated since sonata-project/twig-extensions 1.4'
  123.             .' and will be removed in 2.0. Use "%s" instead.',
  124.             __METHOD__,
  125.             'getRenderedHtmlClassAttribute()'
  126.         ), \E_USER_DEPRECATED);
  127.         if (\is_string($object)) {
  128.             if (null === $statusName) {
  129.                 $statusName $object;
  130.             }
  131.         }
  132.         if (null === $statusName) {
  133.             return $default;
  134.         }
  135.         return $this->getRenderedHtmlClassAttribute($statusName$default);
  136.     }
  137.     /**
  138.      * Returns Sonata flash message types.
  139.      */
  140.     public function getTypes(): array
  141.     {
  142.         return $this->types;
  143.     }
  144.     /**
  145.      * Returns Symfony session service.
  146.      *
  147.      * NEXT_MAJOR: Change visibility to private.
  148.      *
  149.      * @return Session
  150.      */
  151.     public function getSession(): SessionInterface
  152.     {
  153.         if ($this->requestStackOrDeprecatedSession instanceof Session) {
  154.             return $this->requestStackOrDeprecatedSession;
  155.         }
  156.         // @phpstan-ignore-next-line
  157.         if (method_exists($this->requestStackOrDeprecatedSession'getMainRequest')) {
  158.             $request $this->requestStackOrDeprecatedSession->getMainRequest();
  159.         } else {
  160.             // @phpstan-ignore-next-line
  161.             $request $this->requestStackOrDeprecatedSession->getMasterRequest();
  162.         }
  163.         if (null === $request) {
  164.             throw new \RuntimeException('No request was found.');
  165.         }
  166.         $session $request->getSession();
  167.         if (!$session instanceof Session) {
  168.             throw new \UnexpectedValueException(sprintf(
  169.                 'The flash manager only works with a "%s" session.',
  170.                 Session::class
  171.             ));
  172.         }
  173.         return $session;
  174.     }
  175.     /**
  176.      * Returns flash bag messages for correct type after renaming with Sonata flash message type.
  177.      */
  178.     public function get(string $type): array
  179.     {
  180.         $this->handle();
  181.         return $this->getSession()->getFlashBag()->get($type);
  182.     }
  183.     /**
  184.      * Gets handled Sonata flash message types.
  185.      */
  186.     public function getHandledTypes(): array
  187.     {
  188.         return array_keys($this->getTypes());
  189.     }
  190.     public function getRenderedHtmlClassAttribute(string $typestring $default ''): string
  191.     {
  192.         return \array_key_exists($type$this->cssClasses)
  193.             ? $this->cssClasses[$type]
  194.             : $default;
  195.     }
  196.     public function handlesType(string $type): bool
  197.     {
  198.         return \array_key_exists($type$this->cssClasses);
  199.     }
  200.     /**
  201.      * Add flash message to session.
  202.      */
  203.     public function addFlash(string $typestring $message): void
  204.     {
  205.         $this->getSession()->getFlashBag()->add($type$message);
  206.     }
  207.     /**
  208.      * Handles flash bag types renaming.
  209.      */
  210.     private function handle(): void
  211.     {
  212.         foreach ($this->getTypes() as $type => $values) {
  213.             foreach ($values as $value => $options) {
  214.                 $this->rename($type$value);
  215.             }
  216.         }
  217.     }
  218.     /**
  219.      * Process Sonata flash message type rename.
  220.      *
  221.      * @param string $type  Sonata flash message type
  222.      * @param string $value Original flash message type
  223.      */
  224.     private function rename(string $typestring $value): void
  225.     {
  226.         $flashBag $this->getSession()->getFlashBag();
  227.         foreach ($flashBag->get($value) as $message) {
  228.             $flashBag->add($type$message);
  229.         }
  230.     }
  231. }