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\Contracts\Cookie\QueueingFactory as CookieJar;
6
use Illuminate\Support\InteractsWithTime;
7
use SessionHandlerInterface;
8
use Symfony\Component\HttpFoundation\Request;
9
 
10
class CookieSessionHandler implements SessionHandlerInterface
11
{
12
    use InteractsWithTime;
13
 
14
    /**
15
     * The cookie jar instance.
16
     *
17
     * @var \Illuminate\Contracts\Cookie\Factory
18
     */
19
    protected $cookie;
20
 
21
    /**
22
     * The request instance.
23
     *
24
     * @var \Symfony\Component\HttpFoundation\Request
25
     */
26
    protected $request;
27
 
28
    /**
29
     * The number of minutes the session should be valid.
30
     *
31
     * @var int
32
     */
33
    protected $minutes;
34
 
35
    /**
36
     * Create a new cookie driven handler instance.
37
     *
38
     * @param  \Illuminate\Contracts\Cookie\QueueingFactory  $cookie
39
     * @param  int  $minutes
40
     * @return void
41
     */
42
    public function __construct(CookieJar $cookie, $minutes)
43
    {
44
        $this->cookie = $cookie;
45
        $this->minutes = $minutes;
46
    }
47
 
48
    /**
49
     * {@inheritdoc}
50
     *
51
     * @return bool
52
     */
53
    public function open($savePath, $sessionName): bool
54
    {
55
        return true;
56
    }
57
 
58
    /**
59
     * {@inheritdoc}
60
     *
61
     * @return bool
62
     */
63
    public function close(): bool
64
    {
65
        return true;
66
    }
67
 
68
    /**
69
     * {@inheritdoc}
70
     *
71
     * @return string|false
72
     */
73
    public function read($sessionId): string|false
74
    {
75
        $value = $this->request->cookies->get($sessionId) ?: '';
76
 
77
        if (! is_null($decoded = json_decode($value, true)) && is_array($decoded) &&
78
            isset($decoded['expires']) && $this->currentTime() <= $decoded['expires']) {
79
            return $decoded['data'];
80
        }
81
 
82
        return '';
83
    }
84
 
85
    /**
86
     * {@inheritdoc}
87
     *
88
     * @return bool
89
     */
90
    public function write($sessionId, $data): bool
91
    {
92
        $this->cookie->queue($sessionId, json_encode([
93
            'data' => $data,
94
            'expires' => $this->availableAt($this->minutes * 60),
95
        ]), $this->minutes);
96
 
97
        return true;
98
    }
99
 
100
    /**
101
     * {@inheritdoc}
102
     *
103
     * @return bool
104
     */
105
    public function destroy($sessionId): bool
106
    {
107
        $this->cookie->queue($this->cookie->forget($sessionId));
108
 
109
        return true;
110
    }
111
 
112
    /**
113
     * {@inheritdoc}
114
     *
115
     * @return int
116
     */
117
    public function gc($lifetime): int
118
    {
119
        return 0;
120
    }
121
 
122
    /**
123
     * Set the request instance.
124
     *
125
     * @param  \Symfony\Component\HttpFoundation\Request  $request
126
     * @return void
127
     */
128
    public function setRequest(Request $request)
129
    {
130
        $this->request = $request;
131
    }
132
}