1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bytic\Cookie; |
4
|
|
|
|
5
|
|
|
use Nip\Cookie\Cookie; |
|
|
|
|
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* |
9
|
|
|
*/ |
10
|
|
|
class CookieJar |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The default path (if specified). |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $path = '/'; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The default domain (if specified). |
21
|
|
|
* |
22
|
|
|
* @var string|null |
23
|
|
|
*/ |
24
|
|
|
protected $domain; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The default secure setting (defaults to null). |
28
|
|
|
* |
29
|
|
|
* @var bool|null |
30
|
|
|
*/ |
31
|
|
|
protected $secure; |
32
|
|
|
|
33
|
|
|
protected $expireTimer = 6 * 60 * 60; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The default SameSite option (defaults to lax). |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $sameSite = 'lax'; |
41
|
|
|
|
42
|
|
|
public function __construct() |
43
|
|
|
{ |
44
|
|
|
$this->initDefaults(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return Cookie |
49
|
|
|
* @deprecated use make with proper name |
50
|
|
|
*/ |
51
|
|
|
public function newCookie($name = null, $value = null) |
52
|
|
|
{ |
53
|
|
|
$name = $name ?: 'cookie' . microtime(); |
54
|
|
|
return $this->make($name, $value); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Create a new cookie instance. |
59
|
|
|
* |
60
|
|
|
* @param string $name |
61
|
|
|
* @param string $value |
62
|
|
|
* @param int $time |
63
|
|
|
* @param string|null $path |
64
|
|
|
* @param string|null $domain |
65
|
|
|
* @param bool|null $secure |
66
|
|
|
* @param bool $httpOnly |
67
|
|
|
* @param bool $raw |
68
|
|
|
* @param string|null $sameSite |
69
|
|
|
* @return \Symfony\Component\HttpFoundation\Cookie |
70
|
|
|
*/ |
71
|
|
|
public function make( |
72
|
|
|
$name, |
73
|
|
|
$value, |
74
|
|
|
$time = 0, |
75
|
|
|
$path = null, |
76
|
|
|
$domain = null, |
77
|
|
|
$secure = null, |
78
|
|
|
$httpOnly = true, |
79
|
|
|
$raw = false, |
80
|
|
|
$sameSite = null |
81
|
|
|
) { |
82
|
|
|
$path = $path ?: $this->path; |
83
|
|
|
$domain = $domain ?: $this->domain; |
84
|
|
|
$secure = is_bool($secure) ? $secure : $this->secure; |
85
|
|
|
$sameSite = $sameSite ?: $this->sameSite; |
86
|
|
|
$time = $time ?: time() + $this->expireTimer; |
87
|
|
|
return new Cookie($name, $value, $time, $path, $domain, $secure, $httpOnly, $raw, $sameSite); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function initDefaults() |
91
|
|
|
{ |
92
|
|
|
$this->domain = $_SERVER['SERVER_NAME']; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param $defaults |
97
|
|
|
* @return void |
98
|
|
|
*/ |
99
|
|
|
public function setDefaults($defaults) |
100
|
|
|
{ |
101
|
|
|
foreach ($defaults as $name => $value) { |
102
|
|
|
$this->setDefault($name, $value); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param $name |
108
|
|
|
* @param $value |
109
|
|
|
* @return void |
110
|
|
|
*/ |
111
|
|
|
public function setDefault($name, $value = null) |
112
|
|
|
{ |
113
|
|
|
if ($value !== null) { |
114
|
|
|
$this->{$name} = $value; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: