Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen 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\Storage;
13
 
14
use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
15
 
16
/**
17
 * Metadata container.
18
 *
19
 * Adds metadata to the session.
20
 *
21
 * @author Drak <drak@zikula.org>
22
 */
23
class MetadataBag implements SessionBagInterface
24
{
25
    public const CREATED = 'c';
26
    public const UPDATED = 'u';
27
    public const LIFETIME = 'l';
28
 
29
    private string $name = '__metadata';
30
    private string $storageKey;
31
 
32
    /**
33
     * @var array
34
     */
35
    protected $meta = [self::CREATED => 0, self::UPDATED => 0, self::LIFETIME => 0];
36
 
37
    /**
38
     * Unix timestamp.
39
     */
40
    private int $lastUsed;
41
 
42
    private int $updateThreshold;
43
 
44
    /**
45
     * @param string $storageKey      The key used to store bag in the session
46
     * @param int    $updateThreshold The time to wait between two UPDATED updates
47
     */
48
    public function __construct(string $storageKey = '_sf2_meta', int $updateThreshold = 0)
49
    {
50
        $this->storageKey = $storageKey;
51
        $this->updateThreshold = $updateThreshold;
52
    }
53
 
54
    public function initialize(array &$array)
55
    {
56
        $this->meta = &$array;
57
 
58
        if (isset($array[self::CREATED])) {
59
            $this->lastUsed = $this->meta[self::UPDATED];
60
 
61
            $timeStamp = time();
62
            if ($timeStamp - $array[self::UPDATED] >= $this->updateThreshold) {
63
                $this->meta[self::UPDATED] = $timeStamp;
64
            }
65
        } else {
66
            $this->stampCreated();
67
        }
68
    }
69
 
70
    /**
71
     * Gets the lifetime that the session cookie was set with.
72
     */
73
    public function getLifetime(): int
74
    {
75
        return $this->meta[self::LIFETIME];
76
    }
77
 
78
    /**
79
     * Stamps a new session's metadata.
80
     *
81
     * @param int $lifetime Sets the cookie lifetime for the session cookie. A null value
82
     *                      will leave the system settings unchanged, 0 sets the cookie
83
     *                      to expire with browser session. Time is in seconds, and is
84
     *                      not a Unix timestamp.
85
     */
86
    public function stampNew(int $lifetime = null)
87
    {
88
        $this->stampCreated($lifetime);
89
    }
90
 
91
    public function getStorageKey(): string
92
    {
93
        return $this->storageKey;
94
    }
95
 
96
    /**
97
     * Gets the created timestamp metadata.
98
     *
99
     * @return int Unix timestamp
100
     */
101
    public function getCreated(): int
102
    {
103
        return $this->meta[self::CREATED];
104
    }
105
 
106
    /**
107
     * Gets the last used metadata.
108
     *
109
     * @return int Unix timestamp
110
     */
111
    public function getLastUsed(): int
112
    {
113
        return $this->lastUsed;
114
    }
115
 
116
    public function clear(): mixed
117
    {
118
        // nothing to do
119
        return null;
120
    }
121
 
122
    public function getName(): string
123
    {
124
        return $this->name;
125
    }
126
 
127
    /**
128
     * Sets name.
129
     */
130
    public function setName(string $name)
131
    {
132
        $this->name = $name;
133
    }
134
 
135
    private function stampCreated(int $lifetime = null): void
136
    {
137
        $timeStamp = time();
138
        $this->meta[self::CREATED] = $this->meta[self::UPDATED] = $this->lastUsed = $timeStamp;
139
        $this->meta[self::LIFETIME] = $lifetime ?? (int) \ini_get('session.cookie_lifetime');
140
    }
141
}