Projekt

Obecné

Profil

Stáhnout (2.21 KB) Statistiky
| Větev: | Revize:
1
<?php
2

    
3
namespace Illuminate\Session;
4

    
5
use Carbon\Carbon;
6
use SessionHandlerInterface;
7
use Symfony\Component\HttpFoundation\Request;
8
use Illuminate\Contracts\Cookie\QueueingFactory as CookieJar;
9

    
10
class CookieSessionHandler implements SessionHandlerInterface
11
{
12
    /**
13
     * The cookie jar instance.
14
     *
15
     * @var \Illuminate\Contracts\Cookie\Factory
16
     */
17
    protected $cookie;
18

    
19
    /**
20
     * The request instance.
21
     *
22
     * @var \Symfony\Component\HttpFoundation\Request
23
     */
24
    protected $request;
25

    
26
    /**
27
     * Create a new cookie driven handler instance.
28
     *
29
     * @param  \Illuminate\Contracts\Cookie\QueueingFactory  $cookie
30
     * @param  int  $minutes
31
     * @return void
32
     */
33
    public function __construct(CookieJar $cookie, $minutes)
34
    {
35
        $this->cookie = $cookie;
36
        $this->minutes = $minutes;
37
    }
38

    
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function open($savePath, $sessionName)
43
    {
44
        return true;
45
    }
46

    
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function close()
51
    {
52
        return true;
53
    }
54

    
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function read($sessionId)
59
    {
60
        $value = $this->request->cookies->get($sessionId) ?: '';
61

    
62
        if (! is_null($decoded = json_decode($value, true)) && is_array($decoded)) {
63
            if (isset($decoded['expires']) && time() <= $decoded['expires']) {
64
                return $decoded['data'];
65
            }
66
        }
67

    
68
        return '';
69
    }
70

    
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function write($sessionId, $data)
75
    {
76
        $this->cookie->queue($sessionId, json_encode([
77
            'data' => $data,
78
            'expires' => Carbon::now()->addMinutes($this->minutes)->getTimestamp(),
79
        ]), $this->minutes);
80
    }
81

    
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function destroy($sessionId)
86
    {
87
        $this->cookie->queue($this->cookie->forget($sessionId));
88
    }
89

    
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function gc($lifetime)
94
    {
95
        return true;
96
    }
97

    
98
    /**
99
     * Set the request instance.
100
     *
101
     * @param  \Symfony\Component\HttpFoundation\Request  $request
102
     * @return void
103
     */
104
    public function setRequest(Request $request)
105
    {
106
        $this->request = $request;
107
    }
108
}
(2-2/13)