Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Session;
4
 
5
use Illuminate\Support\InteractsWithTime;
6
use SessionHandlerInterface;
7
 
8
class ArraySessionHandler implements SessionHandlerInterface
9
{
10
    use InteractsWithTime;
11
 
12
    /**
13
     * The array of stored values.
14
     *
15
     * @var array
16
     */
17
    protected $storage = [];
18
 
19
    /**
20
     * The number of minutes the session should be valid.
21
     *
22
     * @var int
23
     */
24
    protected $minutes;
25
 
26
    /**
27
     * Create a new array driven handler instance.
28
     *
29
     * @param  int  $minutes
30
     * @return void
31
     */
32
    public function __construct($minutes)
33
    {
34
        $this->minutes = $minutes;
35
    }
36
 
37
    /**
38
     * {@inheritdoc}
39
     *
40
     * @return bool
41
     */
42
    public function open($savePath, $sessionName): bool
43
    {
44
        return true;
45
    }
46
 
47
    /**
48
     * {@inheritdoc}
49
     *
50
     * @return bool
51
     */
52
    public function close(): bool
53
    {
54
        return true;
55
    }
56
 
57
    /**
58
     * {@inheritdoc}
59
     *
60
     * @return string|false
61
     */
62
    public function read($sessionId): string|false
63
    {
64
        if (! isset($this->storage[$sessionId])) {
65
            return '';
66
        }
67
 
68
        $session = $this->storage[$sessionId];
69
 
70
        $expiration = $this->calculateExpiration($this->minutes * 60);
71
 
72
        if (isset($session['time']) && $session['time'] >= $expiration) {
73
            return $session['data'];
74
        }
75
 
76
        return '';
77
    }
78
 
79
    /**
80
     * {@inheritdoc}
81
     *
82
     * @return bool
83
     */
84
    public function write($sessionId, $data): bool
85
    {
86
        $this->storage[$sessionId] = [
87
            'data' => $data,
88
            'time' => $this->currentTime(),
89
        ];
90
 
91
        return true;
92
    }
93
 
94
    /**
95
     * {@inheritdoc}
96
     *
97
     * @return bool
98
     */
99
    public function destroy($sessionId): bool
100
    {
101
        if (isset($this->storage[$sessionId])) {
102
            unset($this->storage[$sessionId]);
103
        }
104
 
105
        return true;
106
    }
107
 
108
    /**
109
     * {@inheritdoc}
110
     *
111
     * @return int
112
     */
113
    public function gc($lifetime): int
114
    {
115
        $expiration = $this->calculateExpiration($lifetime);
116
 
117
        $deletedSessions = 0;
118
 
119
        foreach ($this->storage as $sessionId => $session) {
120
            if ($session['time'] < $expiration) {
121
                unset($this->storage[$sessionId]);
122
                $deletedSessions++;
123
            }
124
        }
125
 
126
        return $deletedSessions;
127
    }
128
 
129
    /**
130
     * Get the expiration time of the session.
131
     *
132
     * @param  int  $seconds
133
     * @return int
134
     */
135
    protected function calculateExpiration($seconds)
136
    {
137
        return $this->currentTime() - $seconds;
138
    }
139
}