nystudio107 /
craft-cookies
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * Cookies plugin for Craft CMS |
||
| 5 | * |
||
| 6 | * @link https://nystudio107.com/ |
||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 7 | * @copyright Copyright (c) nystudio107 |
||
|
0 ignored issues
–
show
|
|||
| 8 | * @license MIT License https://opensource.org/licenses/MIT |
||
| 9 | */ |
||
|
0 ignored issues
–
show
|
|||
| 10 | |||
| 11 | namespace nystudio107\cookies\twigextensions; |
||
| 12 | |||
| 13 | use nystudio107\cookies\Cookies; |
||
| 14 | use Twig\Extension\AbstractExtension; |
||
| 15 | use Twig\TwigFilter; |
||
| 16 | use Twig\TwigFunction; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Cookies twig extension |
||
| 20 | * |
||
| 21 | * @author nystudio107 |
||
|
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
|
|||
| 22 | * @package Cookies |
||
|
0 ignored issues
–
show
|
|||
| 23 | * @since 1.1.0 |
||
|
0 ignored issues
–
show
|
|||
| 24 | */ |
||
|
0 ignored issues
–
show
|
|||
| 25 | class CookiesTwigExtension extends AbstractExtension |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * Return our Twig Extension name |
||
| 29 | */ |
||
|
0 ignored issues
–
show
|
|||
| 30 | public function getName(): string |
||
| 31 | { |
||
| 32 | return 'Cookies'; |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
|
0 ignored issues
–
show
|
|||
| 36 | * @inheritdoc |
||
| 37 | */ |
||
|
0 ignored issues
–
show
|
|||
| 38 | public function getFilters(): array |
||
| 39 | { |
||
| 40 | return [ |
||
| 41 | new TwigFilter('setCookie', fn(string $name = "", string $value = "", int $expire = 0, string $path = "/", string $domain = "", bool $secure = false, bool $httpOnly = false, string $sameSite = 'Lax') => $this->setCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly, $sameSite)), |
||
| 42 | new TwigFilter('getCookie', fn($name) => $this->getCookie($name)), |
||
| 43 | new TwigFilter('setSecureCookie', fn(string $name = "", string $value = "", int $expire = 0, string $path = "/", string $domain = "", bool $secure = false, bool $httpOnly = false, string $sameSite = 'Lax') => $this->setSecureCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly, $sameSite)), |
||
| 44 | new TwigFilter('getSecureCookie', fn($name) => $this->getSecureCookie($name)), |
||
| 45 | ]; |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
|
0 ignored issues
–
show
|
|||
| 49 | * @inheritdoc |
||
| 50 | */ |
||
|
0 ignored issues
–
show
|
|||
| 51 | public function getFunctions(): array |
||
| 52 | { |
||
| 53 | return [ |
||
| 54 | new TwigFunction('setCookie', fn(string $name = "", string $value = "", int $expire = 0, string $path = "/", string $domain = "", bool $secure = false, bool $httpOnly = false, string $sameSite = 'Lax') => $this->setCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly, $sameSite)), |
||
| 55 | new TwigFunction('getCookie', fn($name) => $this->getCookie($name)), |
||
| 56 | new TwigFunction('setSecureCookie', fn(string $name = "", string $value = "", int $expire = 0, string $path = "/", string $domain = "", bool $secure = false, bool $httpOnly = false, string $sameSite = 'Lax') => $this->setSecureCookie($name, $value, $expire, $path, $domain, $secure, $httpOnly, $sameSite)), |
||
| 57 | new TwigFunction('getSecureCookie', fn($name) => $this->getSecureCookie($name)), |
||
| 58 | ]; |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
|
0 ignored issues
–
show
|
|||
| 62 | * Set a cookie |
||
| 63 | */ |
||
|
0 ignored issues
–
show
|
|||
| 64 | public function setCookie( |
||
| 65 | string $name = "", |
||
| 66 | string $value = "", |
||
| 67 | int $expire = 0, |
||
| 68 | string $path = "/", |
||
| 69 | string $domain = "", |
||
| 70 | bool $secure = false, |
||
| 71 | bool $httpOnly = false, |
||
| 72 | string $sameSite = 'Lax', |
||
| 73 | ): void { |
||
| 74 | Cookies::$plugin->cookies->set( |
||
| 75 | $name, |
||
| 76 | $value, |
||
| 77 | $expire, |
||
| 78 | $path, |
||
| 79 | $domain, |
||
| 80 | $secure, |
||
| 81 | $httpOnly, |
||
| 82 | $sameSite |
||
| 83 | ); |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
|
0 ignored issues
–
show
|
|||
| 87 | * Get a cookie |
||
| 88 | */ |
||
|
0 ignored issues
–
show
|
|||
| 89 | public function getCookie(string $name): string |
||
| 90 | { |
||
| 91 | return Cookies::$plugin->cookies->get($name); |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
|
0 ignored issues
–
show
|
|||
| 95 | * Set a secure cookie |
||
| 96 | */ |
||
|
0 ignored issues
–
show
|
|||
| 97 | public function setSecureCookie( |
||
| 98 | string $name = "", |
||
| 99 | string $value = "", |
||
| 100 | int $expire = 0, |
||
| 101 | string $path = "/", |
||
| 102 | string $domain = "", |
||
| 103 | bool $secure = false, |
||
| 104 | bool $httpOnly = false, |
||
| 105 | string $sameSite = 'Lax', |
||
| 106 | ): void { |
||
| 107 | Cookies::$plugin->cookies->setSecure( |
||
| 108 | $name, |
||
| 109 | $value, |
||
| 110 | $expire, |
||
| 111 | $path, |
||
| 112 | $domain, |
||
| 113 | $secure, |
||
| 114 | $httpOnly, |
||
| 115 | $sameSite |
||
| 116 | ); |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
|
0 ignored issues
–
show
|
|||
| 120 | * Get a secure cookie |
||
| 121 | */ |
||
|
0 ignored issues
–
show
|
|||
| 122 | public function getSecureCookie(string $name): string |
||
| 123 | { |
||
| 124 | return Cookies::$plugin->cookies->getSecure($name); |
||
| 125 | } |
||
| 126 | } |
||
| 127 |