1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CodeZero\Cookie\Laravel; |
4
|
|
|
|
5
|
|
|
use CodeZero\Cookie\Cookie; |
6
|
|
|
use Illuminate\Cookie\CookieJar; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
|
9
|
|
|
class LaravelCookie implements Cookie |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Laravel's Request Class |
13
|
|
|
* |
14
|
|
|
* @var \Illuminate\Http\Request |
15
|
|
|
*/ |
16
|
|
|
protected $request; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Laravel's CookieJar Class |
20
|
|
|
* |
21
|
|
|
* @var \Illuminate\Cookie\CookieJar |
22
|
|
|
*/ |
23
|
|
|
protected $cookie; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Create a new instance of LaravelCookie |
27
|
|
|
* |
28
|
|
|
* @param \Illuminate\Http\Request $request |
29
|
|
|
* @param \Illuminate\Cookie\CookieJar $cookie |
30
|
|
|
*/ |
31
|
|
|
public function __construct(Request $request, CookieJar $cookie) |
32
|
|
|
{ |
33
|
|
|
$this->request = $request; |
34
|
|
|
$this->cookie = $cookie; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get the value of a cookie |
39
|
|
|
* |
40
|
|
|
* @param string $cookieName |
41
|
|
|
* |
42
|
|
|
* @return null|string |
43
|
|
|
*/ |
44
|
|
|
public function get($cookieName) |
45
|
|
|
{ |
46
|
|
|
return $this->request->cookie($cookieName); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Store a cookie |
51
|
|
|
* |
52
|
|
|
* @param string $cookieName |
53
|
|
|
* @param string $cookieValue |
54
|
|
|
* @param int $minutes |
55
|
|
|
* @param string $path |
56
|
|
|
* @param string $domain |
57
|
|
|
* @param bool $secure |
58
|
|
|
* @param bool $httpOnly |
59
|
|
|
* |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
|
public function store($cookieName, $cookieValue, $minutes = 60, $path = "/", $domain = null, $secure = true, $httpOnly = true) |
63
|
|
|
{ |
64
|
|
|
$this->cookie->queue($cookieName, $cookieValue, $minutes, $path, $domain, $secure, $httpOnly); |
65
|
|
|
|
66
|
|
|
return true; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Store a cookie for a long, long time |
71
|
|
|
* |
72
|
|
|
* @param string $cookieName |
73
|
|
|
* @param string $cookieValue |
74
|
|
|
* @param string $path |
75
|
|
|
* @param string $domain |
76
|
|
|
* @param bool $secure |
77
|
|
|
* @param bool $httpOnly |
78
|
|
|
* |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
public function forever($cookieName, $cookieValue, $path = '/', $domain = null, $secure = null, $httpOnly = true) |
82
|
|
|
{ |
83
|
|
|
$cookie = $this->cookie->forever($cookieName, $cookieValue, 60 * 24 * 365 * 5, $path, $domain, $secure, $httpOnly); |
84
|
|
|
$this->cookie->queue($cookie); |
85
|
|
|
|
86
|
|
|
return true; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Delete a cookie |
91
|
|
|
* |
92
|
|
|
* @param string $cookieName |
93
|
|
|
* @param string $path |
94
|
|
|
* @param string $domain |
95
|
|
|
* |
96
|
|
|
* @return null|bool |
97
|
|
|
*/ |
98
|
|
|
public function delete($cookieName, $path = '/', $domain = null) |
99
|
|
|
{ |
100
|
|
|
$cookie = $this->cookie->forget($cookieName, $path, $domain); |
101
|
|
|
$this->cookie->queue($cookie); |
102
|
|
|
|
103
|
|
|
return true; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Check if a cookie exists |
108
|
|
|
* |
109
|
|
|
* @param string $cookieName |
110
|
|
|
* |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
|
public function exists($cookieName) |
114
|
|
|
{ |
115
|
|
|
return $this->get($cookieName) !== null; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|