githubtrue/backend/vendor/illuminate/session/LegacyDatabaseSessionHandler.php @ b50f8ebd
1 |
<?php
|
---|---|
2 |
|
3 |
namespace Illuminate\Session; |
4 |
|
5 |
use Carbon\Carbon; |
6 |
use SessionHandlerInterface; |
7 |
use Illuminate\Database\ConnectionInterface; |
8 |
|
9 |
/**
|
10 |
* @deprecated since version 5.2. Use Illuminate\Session\DatabaseSessionHandler.
|
11 |
*/
|
12 |
class LegacyDatabaseSessionHandler implements SessionHandlerInterface, ExistenceAwareInterface |
13 |
{
|
14 |
/**
|
15 |
* The database connection instance.
|
16 |
*
|
17 |
* @var \Illuminate\Database\ConnectionInterface
|
18 |
*/
|
19 |
protected $connection; |
20 |
|
21 |
/**
|
22 |
* The name of the session table.
|
23 |
*
|
24 |
* @var string
|
25 |
*/
|
26 |
protected $table; |
27 |
|
28 |
/*
|
29 |
* The number of minutes the session should be valid.
|
30 |
*
|
31 |
* @var int
|
32 |
*/
|
33 |
protected $minutes; |
34 |
|
35 |
/**
|
36 |
* The existence state of the session.
|
37 |
*
|
38 |
* @var bool
|
39 |
*/
|
40 |
protected $exists; |
41 |
|
42 |
/**
|
43 |
* Create a new database session handler instance.
|
44 |
*
|
45 |
* @param \Illuminate\Database\ConnectionInterface $connection
|
46 |
* @param string $table
|
47 |
* @param int $minutes
|
48 |
* @return void
|
49 |
*/
|
50 |
public function __construct(ConnectionInterface $connection, $table, $minutes) |
51 |
{
|
52 |
$this->table = $table; |
53 |
$this->minutes = $minutes; |
54 |
$this->connection = $connection; |
55 |
}
|
56 |
|
57 |
/**
|
58 |
* {@inheritdoc}
|
59 |
*/
|
60 |
public function open($savePath, $sessionName) |
61 |
{
|
62 |
return true; |
63 |
}
|
64 |
|
65 |
/**
|
66 |
* {@inheritdoc}
|
67 |
*/
|
68 |
public function close() |
69 |
{
|
70 |
return true; |
71 |
}
|
72 |
|
73 |
/**
|
74 |
* {@inheritdoc}
|
75 |
*/
|
76 |
public function read($sessionId) |
77 |
{
|
78 |
$session = (object) $this->getQuery()->find($sessionId); |
79 |
|
80 |
if (isset($session->last_activity)) { |
81 |
if ($session->last_activity < Carbon::now()->subMinutes($this->minutes)->getTimestamp()) { |
82 |
$this->exists = true; |
83 |
|
84 |
return; |
85 |
}
|
86 |
}
|
87 |
|
88 |
if (isset($session->payload)) { |
89 |
$this->exists = true; |
90 |
|
91 |
return base64_decode($session->payload); |
92 |
}
|
93 |
}
|
94 |
|
95 |
/**
|
96 |
* {@inheritdoc}
|
97 |
*/
|
98 |
public function write($sessionId, $data) |
99 |
{
|
100 |
if ($this->exists) { |
101 |
$this->getQuery()->where('id', $sessionId)->update([ |
102 |
'payload' => base64_encode($data), 'last_activity' => time(), |
103 |
]);
|
104 |
} else { |
105 |
$this->getQuery()->insert([ |
106 |
'id' => $sessionId, 'payload' => base64_encode($data), 'last_activity' => time(), |
107 |
]);
|
108 |
}
|
109 |
|
110 |
$this->exists = true; |
111 |
}
|
112 |
|
113 |
/**
|
114 |
* {@inheritdoc}
|
115 |
*/
|
116 |
public function destroy($sessionId) |
117 |
{
|
118 |
$this->getQuery()->where('id', $sessionId)->delete(); |
119 |
}
|
120 |
|
121 |
/**
|
122 |
* {@inheritdoc}
|
123 |
*/
|
124 |
public function gc($lifetime) |
125 |
{
|
126 |
$this->getQuery()->where('last_activity', '<=', time() - $lifetime)->delete(); |
127 |
}
|
128 |
|
129 |
/**
|
130 |
* Get a fresh query builder instance for the table.
|
131 |
*
|
132 |
* @return \Illuminate\Database\Query\Builder
|
133 |
*/
|
134 |
protected function getQuery() |
135 |
{
|
136 |
return $this->connection->table($this->table); |
137 |
}
|
138 |
|
139 |
/**
|
140 |
* Set the existence state for the session.
|
141 |
*
|
142 |
* @param bool $value
|
143 |
* @return $this
|
144 |
*/
|
145 |
public function setExists($value) |
146 |
{
|
147 |
$this->exists = $value; |
148 |
|
149 |
return $this; |
150 |
}
|
151 |
}
|