/
CookieJar.php
113 lines (99 loc) · 2.7 KB
/
CookieJar.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
namespace Concrete\Core\Cookie;
use Concrete\Core\Http\Request;
use Symfony\Component\HttpFoundation\Cookie as CookieObject;
class CookieJar
{
protected $cookies = array();
protected $clearedCookies = array();
protected $request;
/**
* Adds a CookieObject to the cookie pantry.
*
* @param string $name The cookie name
* @param string|null $value The value of the cookie
* @param int $expire The number of seconds until the cookie expires
* @param string $path The path for the cookie
* @param null|string $domain The domain the cookie is available to
* @param bool $secure whether the cookie should only be transmitted over a HTTPS connection from the client
* @param bool $httpOnly Whether the cookie will be made accessible only through the HTTP protocol
*
* @return \Symfony\Component\HttpFoundation\Cookie
*/
public function set(
$name,
$value = null,
$expire = 0,
$path = '/',
$domain = null,
$secure = false,
$httpOnly = true
) {
$cookie = new CookieObject($name, $value, $expire, $path, $domain, $secure, $httpOnly);
$this->add($cookie);
return $cookie;
}
/**
* Adds a CookieObject to the array of cookies for the object.
*
* @param CookieObject $cookie
*/
public function add($cookie)
{
$this->cookies[] = $cookie;
}
/**
* Used to determine if the cookie key exists in the pantry.
*
* @param string $cookie
*
* @return bool
*/
public function has($cookie)
{
return $this->getRequest()->cookies->has($cookie);
}
public function clear($cookie)
{
$this->clearedCookies[] = $cookie;
}
/**
* @param string $name The key the cookie is stored under
* @param mixed $default A value to return if the cookie isn't set
*
* @return mixed
*/
public function get($name, $default = null)
{
if (!$this->has($name)) {
return $default;
}
return $this->getRequest()->cookies->get($name);
}
/**
* @return CookieObject[]
*/
public function getCookies()
{
return $this->cookies;
}
public function getClearedCookies()
{
return $this->clearedCookies;
}
/**
* Set a request for this cookie jar
* @param \Concrete\Core\Cookie\Request $request
*/
public function setRequest(Request $request)
{
$this->request = $request;
}
protected function getRequest()
{
if (!$this->request) {
$this->request = \Request::getInstance();
}
return $this->request;
}
}