-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathFlashMessenger.php
More file actions
66 lines (48 loc) · 1.61 KB
/
FlashMessenger.php
File metadata and controls
66 lines (48 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
declare(strict_types=1);
namespace PhpMyAdmin;
use RuntimeException;
use Twig\Attribute\AsTwigFunction;
use function __;
/** @psalm-type FlashMessageList = list<array{context: string, message: string, statement: string}> */
final class FlashMessenger
{
private const STORAGE_KEY = 'FlashMessenger';
/** @var mixed[] */
private array|null $storage = null;
/** @psalm-var FlashMessageList */
private array $previousMessages = [];
/** @psalm-assert !null $this->storage */
private function initSessionStorage(): void
{
if ($this->storage !== null) {
return;
}
if (! isset($_SESSION)) {
throw new RuntimeException(__('Session not found.'));
}
$this->storage = &$_SESSION;
if (isset($this->storage[self::STORAGE_KEY])) {
$this->previousMessages = $this->storage[self::STORAGE_KEY];
}
$this->storage[self::STORAGE_KEY] = [];
}
public function addMessage(string $context, string $message, string $statement = ''): void
{
$this->initSessionStorage();
$this->storage[self::STORAGE_KEY][] = ['context' => $context, 'message' => $message, 'statement' => $statement];
}
/** @psalm-return FlashMessageList */
#[AsTwigFunction('flash_messages')]
public function getMessages(): array
{
$this->initSessionStorage();
return $this->previousMessages;
}
/** @psalm-return FlashMessageList */
public function getCurrentMessages(): array
{
$this->initSessionStorage();
return $this->storage[self::STORAGE_KEY];
}
}