| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\HttpFoundation\Session\Flash; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
class AutoExpireFlashBag implements FlashBagInterface |
| 20 |
{ |
| 21 |
private string $name = 'flashes'; |
| 22 |
private array $flashes = ['display' => [], 'new' => []]; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public function __construct( |
| 28 |
private string $storageKey = '_symfony_flashes', |
| 29 |
) { |
| 30 |
} |
| 31 |
|
| 32 |
public function getName(): string |
| 33 |
{ |
| 34 |
return $this->name; |
| 35 |
} |
| 36 |
|
| 37 |
public function setName(string $name): void |
| 38 |
{ |
| 39 |
$this->name = $name; |
| 40 |
} |
| 41 |
|
| 42 |
public function initialize(array &$flashes): void |
| 43 |
{ |
| 44 |
$this->flashes = &$flashes; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
$this->flashes['display'] = \array_key_exists('new', $this->flashes) ? $this->flashes['new'] : []; |
| 50 |
$this->flashes['new'] = []; |
| 51 |
} |
| 52 |
|
| 53 |
public function add(string $type, mixed $message): void |
| 54 |
{ |
| 55 |
$this->flashes['new'][$type][] = $message; |
| 56 |
} |
| 57 |
|
| 58 |
public function peek(string $type, array $default = []): array |
| 59 |
{ |
| 60 |
return $this->has($type) ? $this->flashes['display'][$type] : $default; |
| 61 |
} |
| 62 |
|
| 63 |
public function peekAll(): array |
| 64 |
{ |
| 65 |
return \array_key_exists('display', $this->flashes) ? $this->flashes['display'] : []; |
| 66 |
} |
| 67 |
|
| 68 |
public function get(string $type, array $default = []): array |
| 69 |
{ |
| 70 |
$return = $default; |
| 71 |
|
| 72 |
if (!$this->has($type)) { |
| 73 |
return $return; |
| 74 |
} |
| 75 |
|
| 76 |
if (isset($this->flashes['display'][$type])) { |
| 77 |
$return = $this->flashes['display'][$type]; |
| 78 |
unset($this->flashes['display'][$type]); |
| 79 |
} |
| 80 |
|
| 81 |
return $return; |
| 82 |
} |
| 83 |
|
| 84 |
public function all(): array |
| 85 |
{ |
| 86 |
$return = $this->flashes['display']; |
| 87 |
$this->flashes['display'] = []; |
| 88 |
|
| 89 |
return $return; |
| 90 |
} |
| 91 |
|
| 92 |
public function setAll(array $messages): void |
| 93 |
{ |
| 94 |
$this->flashes['new'] = $messages; |
| 95 |
} |
| 96 |
|
| 97 |
public function set(string $type, string|array $messages): void |
| 98 |
{ |
| 99 |
$this->flashes['new'][$type] = (array) $messages; |
| 100 |
} |
| 101 |
|
| 102 |
public function has(string $type): bool |
| 103 |
{ |
| 104 |
return \array_key_exists($type, $this->flashes['display']) && $this->flashes['display'][$type]; |
| 105 |
} |
| 106 |
|
| 107 |
public function keys(): array |
| 108 |
{ |
| 109 |
return array_keys($this->flashes['display']); |
| 110 |
} |
| 111 |
|
| 112 |
public function getStorageKey(): string |
| 113 |
{ |
| 114 |
return $this->storageKey; |
| 115 |
} |
| 116 |
|
| 117 |
public function clear(): mixed |
| 118 |
{ |
| 119 |
return $this->all(); |
| 120 |
} |
| 121 |
} |