Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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
 
12
namespace Symfony\Component\HttpFoundation\Session\Flash;
13
 
14
/**
15
 * AutoExpireFlashBag flash message container.
16
 *
17
 * @author Drak <drak@zikula.org>
18
 */
19
class AutoExpireFlashBag implements FlashBagInterface
20
{
21
    private string $name = 'flashes';
22
    private array $flashes = ['display' => [], 'new' => []];
23
    private string $storageKey;
24
 
25
    /**
26
     * @param string $storageKey The key used to store flashes in the session
27
     */
28
    public function __construct(string $storageKey = '_symfony_flashes')
29
    {
30
        $this->storageKey = $storageKey;
31
    }
32
 
33
    public function getName(): string
34
    {
35
        return $this->name;
36
    }
37
 
38
    public function setName(string $name)
39
    {
40
        $this->name = $name;
41
    }
42
 
43
    public function initialize(array &$flashes)
44
    {
45
        $this->flashes = &$flashes;
46
 
47
        // The logic: messages from the last request will be stored in new, so we move them to previous
48
        // This request we will show what is in 'display'.  What is placed into 'new' this time round will
49
        // be moved to display next time round.
50
        $this->flashes['display'] = \array_key_exists('new', $this->flashes) ? $this->flashes['new'] : [];
51
        $this->flashes['new'] = [];
52
    }
53
 
54
    public function add(string $type, mixed $message)
55
    {
56
        $this->flashes['new'][$type][] = $message;
57
    }
58
 
59
    public function peek(string $type, array $default = []): array
60
    {
61
        return $this->has($type) ? $this->flashes['display'][$type] : $default;
62
    }
63
 
64
    public function peekAll(): array
65
    {
66
        return \array_key_exists('display', $this->flashes) ? $this->flashes['display'] : [];
67
    }
68
 
69
    public function get(string $type, array $default = []): array
70
    {
71
        $return = $default;
72
 
73
        if (!$this->has($type)) {
74
            return $return;
75
        }
76
 
77
        if (isset($this->flashes['display'][$type])) {
78
            $return = $this->flashes['display'][$type];
79
            unset($this->flashes['display'][$type]);
80
        }
81
 
82
        return $return;
83
    }
84
 
85
    public function all(): array
86
    {
87
        $return = $this->flashes['display'];
88
        $this->flashes['display'] = [];
89
 
90
        return $return;
91
    }
92
 
93
    public function setAll(array $messages)
94
    {
95
        $this->flashes['new'] = $messages;
96
    }
97
 
98
    public function set(string $type, string|array $messages)
99
    {
100
        $this->flashes['new'][$type] = (array) $messages;
101
    }
102
 
103
    public function has(string $type): bool
104
    {
105
        return \array_key_exists($type, $this->flashes['display']) && $this->flashes['display'][$type];
106
    }
107
 
108
    public function keys(): array
109
    {
110
        return array_keys($this->flashes['display']);
111
    }
112
 
113
    public function getStorageKey(): string
114
    {
115
        return $this->storageKey;
116
    }
117
 
118
    public function clear(): mixed
119
    {
120
        return $this->all();
121
    }
122
}