give
/
vendor
/
vendor-prefixed
/
symfony
/
http-foundation
/
Session
/
Flash
/
AutoExpireFlashBag.php
AutoExpireFlashBag.php
165 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | * |
| 11 | * Modified by impress-org on 23-January-2024 using Strauss. |
| 12 | * @see https://github.com/BrianHenryIE/strauss |
| 13 | */ |
| 14 | |
| 15 | namespace Give\Vendors\Symfony\Component\HttpFoundation\Session\Flash; |
| 16 | |
| 17 | /** |
| 18 | * AutoExpireFlashBag flash message container. |
| 19 | * |
| 20 | * @author Drak <drak@zikula.org> |
| 21 | */ |
| 22 | class AutoExpireFlashBag implements FlashBagInterface |
| 23 | { |
| 24 | private $name = 'flashes'; |
| 25 | private $flashes = ['display' => [], 'new' => []]; |
| 26 | private $storageKey; |
| 27 | |
| 28 | /** |
| 29 | * @param string $storageKey The key used to store flashes in the session |
| 30 | */ |
| 31 | public function __construct($storageKey = '_symfony_flashes') |
| 32 | { |
| 33 | $this->storageKey = $storageKey; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * {@inheritdoc} |
| 38 | */ |
| 39 | public function getName() |
| 40 | { |
| 41 | return $this->name; |
| 42 | } |
| 43 | |
| 44 | public function setName($name) |
| 45 | { |
| 46 | $this->name = $name; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * {@inheritdoc} |
| 51 | */ |
| 52 | public function initialize(array &$flashes) |
| 53 | { |
| 54 | $this->flashes = &$flashes; |
| 55 | |
| 56 | // The logic: messages from the last request will be stored in new, so we move them to previous |
| 57 | // This request we will show what is in 'display'. What is placed into 'new' this time round will |
| 58 | // be moved to display next time round. |
| 59 | $this->flashes['display'] = \array_key_exists('new', $this->flashes) ? $this->flashes['new'] : []; |
| 60 | $this->flashes['new'] = []; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * {@inheritdoc} |
| 65 | */ |
| 66 | public function add($type, $message) |
| 67 | { |
| 68 | $this->flashes['new'][$type][] = $message; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * {@inheritdoc} |
| 73 | */ |
| 74 | public function peek($type, array $default = []) |
| 75 | { |
| 76 | return $this->has($type) ? $this->flashes['display'][$type] : $default; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * {@inheritdoc} |
| 81 | */ |
| 82 | public function peekAll() |
| 83 | { |
| 84 | return \array_key_exists('display', $this->flashes) ? (array) $this->flashes['display'] : []; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * {@inheritdoc} |
| 89 | */ |
| 90 | public function get($type, array $default = []) |
| 91 | { |
| 92 | $return = $default; |
| 93 | |
| 94 | if (!$this->has($type)) { |
| 95 | return $return; |
| 96 | } |
| 97 | |
| 98 | if (isset($this->flashes['display'][$type])) { |
| 99 | $return = $this->flashes['display'][$type]; |
| 100 | unset($this->flashes['display'][$type]); |
| 101 | } |
| 102 | |
| 103 | return $return; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * {@inheritdoc} |
| 108 | */ |
| 109 | public function all() |
| 110 | { |
| 111 | $return = $this->flashes['display']; |
| 112 | $this->flashes['display'] = []; |
| 113 | |
| 114 | return $return; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * {@inheritdoc} |
| 119 | */ |
| 120 | public function setAll(array $messages) |
| 121 | { |
| 122 | $this->flashes['new'] = $messages; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * {@inheritdoc} |
| 127 | */ |
| 128 | public function set($type, $messages) |
| 129 | { |
| 130 | $this->flashes['new'][$type] = (array) $messages; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * {@inheritdoc} |
| 135 | */ |
| 136 | public function has($type) |
| 137 | { |
| 138 | return \array_key_exists($type, $this->flashes['display']) && $this->flashes['display'][$type]; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * {@inheritdoc} |
| 143 | */ |
| 144 | public function keys() |
| 145 | { |
| 146 | return array_keys($this->flashes['display']); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * {@inheritdoc} |
| 151 | */ |
| 152 | public function getStorageKey() |
| 153 | { |
| 154 | return $this->storageKey; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * {@inheritdoc} |
| 159 | */ |
| 160 | public function clear() |
| 161 | { |
| 162 | return $this->all(); |
| 163 | } |
| 164 | } |
| 165 |