diff --git a/app/src/Bootstrap.php b/app/src/Bootstrap.php index 8215e6e..65ba985 100644 --- a/app/src/Bootstrap.php +++ b/app/src/Bootstrap.php @@ -26,6 +26,8 @@ use Psr\Log\LoggerInterface; use Slim\App; use Slim\Factory\AppFactory; use Slim\Psr7\Factory\ResponseFactory; +use Slovocast\Infrastructure\Session\FlashMessages; +use Slovocast\Infrastructure\Session\FlashMessagesInterface; use Slovocast\Middleware\SessionMiddleware; use Twig\Error\LoaderError; @@ -128,8 +130,16 @@ class Bootstrap $options = $container->get('config')->get('session'); return new PhpSession($options); }, + 'session' => function (ContainerInterface $container) { + return $container->get(SessionInterface::class); + }, + FlashMessagesInterface::class => function (ContainerInterface $container) { + return new FlashMessages( + $container->get(SessionInterface::class) + ); + }, 'flash' => function (ContainerInterface $container) { - return $container->get(SessionInterface::class)->getFlash(); + return $container->get(FlashMessagesInterface::class); }, SessionMiddleware::class => function (ContainerInterface $container) { return new SessionMiddleware( diff --git a/app/src/Infrastructure/Api/Session/FlashMessagesInterface.php b/app/src/Infrastructure/Api/Session/FlashMessagesInterface.php new file mode 100644 index 0000000..e1b9f2e --- /dev/null +++ b/app/src/Infrastructure/Api/Session/FlashMessagesInterface.php @@ -0,0 +1,34 @@ + + */ + public function get(string $level): array; + + /** + * @return array> + */ + public function getAll(): array; + + /** + * @param string|null + */ + public function clear(?string $level = null): void; +} diff --git a/app/src/Infrastructure/Session/FlashMessages.php b/app/src/Infrastructure/Session/FlashMessages.php new file mode 100644 index 0000000..d180207 --- /dev/null +++ b/app/src/Infrastructure/Session/FlashMessages.php @@ -0,0 +1,82 @@ +session->has($this->key)) { + $this->session->set($this->key, []); + } + } + + /** + * @return array> + */ + protected function getMessages(): array + { + return $this->session->get($this->key); + } + + /** + * @param array> $messages + * @return void + */ + protected function saveMessages(array $messages): void + { + $this->session->set($this->key, $messages); + } + + public function add(string $level, string $message): void + { + $messages = $this->getMessages(); + $messages[$level][] = $message; + $this->saveMessages($messages); + } + + public function get(string $level): array + { + $messages = $this->getMessages(); + + if (!array_key_exists($level, $messages)) { + return []; + } + + $buffer = $messages[$level]; + unset($messages[$level]); + $this->saveMessages($messages); + return $buffer; + } + + public function getAll(): array + { + $messages = $this->getMessages(); + $this->clear(); + return $messages; + } + + public function clear(?string $level = null): void + { + if (!$level) { + $this->session->set($this->key, []); + } else { + $messages = $this->getMessages(); + + if (array_key_exists($level)) { + unset($messages[$level]); + } + + $this->saveMessages($messages); + } + } +} diff --git a/app/src/Middlewares.php b/app/src/Middlewares.php index 067079f..8796530 100644 --- a/app/src/Middlewares.php +++ b/app/src/Middlewares.php @@ -10,6 +10,7 @@ use Psr\Container\NotFoundExceptionInterface; use Slim\App; use Slim\Views\Twig; use Slim\Views\TwigMiddleware; +use Slovocast\Infrastructure\Session\FlashMessagesInterface; use Slovocast\Middleware\SessionMiddleware; use Twig\Error\LoaderError; @@ -47,7 +48,7 @@ class Middlewares $twig->getEnvironment()->addGlobal('site_description', $config->get('site.description')); $session = $container->get(SessionInterface::class); - $flash = $session->getFlash(); + $flash = $container->get(FlashMessagesInterface::class); $twig->getEnvironment()->addGlobal('session', $session); $twig->getEnvironment()->addGlobal('flash', $flash);