Implement own flash messages interface and class.
This commit is contained in:
parent
d119e479f0
commit
c19cde193f
@ -26,6 +26,8 @@ use Psr\Log\LoggerInterface;
|
|||||||
use Slim\App;
|
use Slim\App;
|
||||||
use Slim\Factory\AppFactory;
|
use Slim\Factory\AppFactory;
|
||||||
use Slim\Psr7\Factory\ResponseFactory;
|
use Slim\Psr7\Factory\ResponseFactory;
|
||||||
|
use Slovocast\Infrastructure\Session\FlashMessages;
|
||||||
|
use Slovocast\Infrastructure\Session\FlashMessagesInterface;
|
||||||
use Slovocast\Middleware\SessionMiddleware;
|
use Slovocast\Middleware\SessionMiddleware;
|
||||||
use Twig\Error\LoaderError;
|
use Twig\Error\LoaderError;
|
||||||
|
|
||||||
@ -128,8 +130,16 @@ class Bootstrap
|
|||||||
$options = $container->get('config')->get('session');
|
$options = $container->get('config')->get('session');
|
||||||
return new PhpSession($options);
|
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) {
|
'flash' => function (ContainerInterface $container) {
|
||||||
return $container->get(SessionInterface::class)->getFlash();
|
return $container->get(FlashMessagesInterface::class);
|
||||||
},
|
},
|
||||||
SessionMiddleware::class => function (ContainerInterface $container) {
|
SessionMiddleware::class => function (ContainerInterface $container) {
|
||||||
return new SessionMiddleware(
|
return new SessionMiddleware(
|
||||||
|
@ -0,0 +1,34 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Slovocast\Infrastructure\Session;
|
||||||
|
|
||||||
|
interface FlashMessagesInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Add a message to the flash system
|
||||||
|
*
|
||||||
|
* @param string $level
|
||||||
|
* @param string $message
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function add(string $level, string $message): void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an array of all the flash messages for the level provided, and
|
||||||
|
* clears the cache.
|
||||||
|
*
|
||||||
|
* @param string $level
|
||||||
|
* @return array<string>
|
||||||
|
*/
|
||||||
|
public function get(string $level): array;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, array<string>>
|
||||||
|
*/
|
||||||
|
public function getAll(): array;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string|null
|
||||||
|
*/
|
||||||
|
public function clear(?string $level = null): void;
|
||||||
|
}
|
82
app/src/Infrastructure/Session/FlashMessages.php
Normal file
82
app/src/Infrastructure/Session/FlashMessages.php
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Slovocast\Infrastructure\Session;
|
||||||
|
|
||||||
|
use Odan\Session\SessionInterface;
|
||||||
|
|
||||||
|
class FlashMessages implements FlashMessagesInterface
|
||||||
|
{
|
||||||
|
const LEVEL_ERROR = "error";
|
||||||
|
const LEVEL_NOTICE = "notice";
|
||||||
|
const LEVEL_SUCCESS = "success";
|
||||||
|
|
||||||
|
private string $key = "_flash";
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
protected SessionInterface $session
|
||||||
|
) {
|
||||||
|
if (!$this->session->has($this->key)) {
|
||||||
|
$this->session->set($this->key, []);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<string, array<string>>
|
||||||
|
*/
|
||||||
|
protected function getMessages(): array
|
||||||
|
{
|
||||||
|
return $this->session->get($this->key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, array<string>> $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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -10,6 +10,7 @@ use Psr\Container\NotFoundExceptionInterface;
|
|||||||
use Slim\App;
|
use Slim\App;
|
||||||
use Slim\Views\Twig;
|
use Slim\Views\Twig;
|
||||||
use Slim\Views\TwigMiddleware;
|
use Slim\Views\TwigMiddleware;
|
||||||
|
use Slovocast\Infrastructure\Session\FlashMessagesInterface;
|
||||||
use Slovocast\Middleware\SessionMiddleware;
|
use Slovocast\Middleware\SessionMiddleware;
|
||||||
use Twig\Error\LoaderError;
|
use Twig\Error\LoaderError;
|
||||||
|
|
||||||
@ -47,7 +48,7 @@ class Middlewares
|
|||||||
$twig->getEnvironment()->addGlobal('site_description', $config->get('site.description'));
|
$twig->getEnvironment()->addGlobal('site_description', $config->get('site.description'));
|
||||||
|
|
||||||
$session = $container->get(SessionInterface::class);
|
$session = $container->get(SessionInterface::class);
|
||||||
$flash = $session->getFlash();
|
$flash = $container->get(FlashMessagesInterface::class);
|
||||||
$twig->getEnvironment()->addGlobal('session', $session);
|
$twig->getEnvironment()->addGlobal('session', $session);
|
||||||
$twig->getEnvironment()->addGlobal('flash', $flash);
|
$twig->getEnvironment()->addGlobal('flash', $flash);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user