-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
CookiesTwigExtension.php
129 lines (118 loc) · 3.83 KB
/
CookiesTwigExtension.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/**
* Cookies plugin for Craft CMS 3.x
*
* @link https://nystudio107.com/
* @copyright Copyright (c) 2017 nystudio107
* @license MIT License https://opensource.org/licenses/MIT
*/
namespace nystudio107\cookies\twigextensions;
use nystudio107\cookies\Cookies;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
/**
* Cookies twig extension
*
* @author nystudio107
* @package Cookies
* @since 1.1.0
*/
class CookiesTwigExtension extends AbstractExtension
{
/**
* Return our Twig Extension name
*/
public function getName(): string
{
return 'Cookies';
}
/**
* @inheritdoc
*/
public function getFilters(): array
{
return [
new TwigFilter('setCookie', fn(string $name = "", string $value = "", int $expire = 0, string $path = "/", string $domain = "", bool $secure = false, bool $httpOnly = false, ?string $sameSite = null) => $this->setCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly, $sameSite)),
new TwigFilter('getCookie', fn($name) => $this->getCookie($name)),
new TwigFilter('setSecureCookie', fn(string $name = "", string $value = "", int $expire = 0, string $path = "/", string $domain = "", bool $secure = false, bool $httpOnly = false, ?string $sameSite = null) => $this->setSecureCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly, $sameSite)),
new TwigFilter('getSecureCookie', fn($name) => $this->getSecureCookie($name)),
];
}
/**
* @inheritdoc
*/
public function getFunctions(): array
{
return [
new TwigFunction('setCookie', fn(string $name = "", string $value = "", int $expire = 0, string $path = "/", string $domain = "", bool $secure = false, bool $httpOnly = false, ?string $sameSite = null) => $this->setCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly, $sameSite)),
new TwigFunction('getCookie', fn($name) => $this->getCookie($name)),
new TwigFunction('setSecureCookie', fn(string $name = "", string $value = "", int $expire = 0, string $path = "/", string $domain = "", bool $secure = false, bool $httpOnly = false, ?string $sameSite = null) => $this->setSecureCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly, $sameSite)),
new TwigFunction('getSecureCookie', fn($name) => $this->getSecureCookie($name)),
];
}
/**
* Set a cookie
*/
public function setCookie(
string $name = "",
string $value = "",
int $expire = 0,
string $path = "/",
string $domain = "",
bool $secure = false,
bool $httpOnly = false,
bool $sameSite = false
): void
{
Cookies::$plugin->cookies->set(
$name,
$value,
$expire,
$path,
$domain,
$secure,
$httpOnly,
$sameSite
);
}
/**
* Get a cookie
*/
public function getCookie(string $name): string
{
return Cookies::$plugin->cookies->get($name);
}
/**
* Set a secure cookie
*/
public function setSecureCookie(
string $name = "",
string $value = "",
int $expire = 0,
string $path = "/",
string $domain = "",
bool $secure = false,
bool $httpOnly = false,
bool $sameSite = false
): void
{
Cookies::$plugin->cookies->setSecure(
$name,
$value,
$expire,
$path,
$domain,
$secure,
$httpOnly,
$sameSite
);
}
/**
* Get a secure cookie
*/
public function getSecureCookie(string $name): string
{
return Cookies::$plugin->cookies->getSecure($name);
}
}