Skip to content

Commit 03d4255

Browse files
committed
feat: add main class facade
1 parent c57ed44 commit 03d4255

File tree

1 file changed

+214
-0
lines changed

1 file changed

+214
-0
lines changed

src/Facades/Session.php

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of https://github.com/josantonius/php-session repository.
7+
*
8+
* (c) Josantonius <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Josantonius\Session\Facades;
15+
16+
use Josantonius\Session\Exceptions\SessionException;
17+
use Josantonius\Session\Session as SessionInstance;
18+
19+
/**
20+
* Session handler.
21+
*/
22+
class Session
23+
{
24+
private static ?SessionInstance $session;
25+
26+
private static function getInstance()
27+
{
28+
return self::$session ?? new SessionInstance();
29+
}
30+
31+
/**
32+
* Starts the session.
33+
*
34+
* List of available $options with their default values.
35+
*
36+
* @see https://php.net/session.configuration
37+
*
38+
* cache_expire: "180",
39+
* cache_limiter: "nocache",
40+
* cookie_domain: "",
41+
* cookie_httponly: "0",
42+
* cookie_lifetime: "0",
43+
* cookie_path: "/",
44+
* cookie_samesite: "",
45+
* cookie_secure: "0",
46+
* gc_divisor: "100",
47+
* gc_maxlifetime: "1440",
48+
* gc_probability: "1",
49+
* lazy_write: "1",
50+
* name: "PHPSESSID",
51+
* read_and_close: "0",
52+
* referer_check: "",
53+
* save_handler: "files",
54+
* save_path: "",
55+
* serialize_handler: "php",
56+
* sid_bits_per_character: "4",
57+
* sid_length: "32",
58+
* trans_sid_hosts: $_SERVER['HTTP_HOST'],
59+
* trans_sid_tags: "a=href,area=href,frame=src,form=",
60+
* use_cookies: "1",
61+
* use_only_cookies: "1",
62+
* use_strict_mode: "0",
63+
* use_trans_sid: "0".
64+
*
65+
* @throws SessionException if headers already sent
66+
* @throws SessionException if session already started
67+
* @throws SessionException If setting options failed
68+
*/
69+
public static function start(array $options = []): bool
70+
{
71+
return self::getInstance()->start($options);
72+
}
73+
74+
/**
75+
* Gets all attributes.
76+
*/
77+
public static function all(): array
78+
{
79+
return self::getInstance()->all();
80+
}
81+
82+
/**
83+
* Check if an attribute exists in the session.
84+
*/
85+
public static function has(string $name): bool
86+
{
87+
return self::getInstance()->has($name);
88+
}
89+
90+
/**
91+
* Gets an attribute by name.
92+
* Optionally defines a default value when the attribute does not exist.
93+
*/
94+
public static function get(string $name, mixed $default = null): mixed
95+
{
96+
return self::getInstance()->get($name, $default);
97+
}
98+
99+
/**
100+
* Sets an attribute by name
101+
*
102+
* @throws SessionException if session is unstarted
103+
*/
104+
public static function set(string $name, mixed $value): void
105+
{
106+
self::getInstance()->set($name, $value);
107+
}
108+
109+
/**
110+
* Sets several attributes at once.
111+
* If attributes exist they are replaced, if they do not exist they are created.
112+
*
113+
* @throws SessionException if session is unstarted
114+
*/
115+
public static function replace(array $data): void
116+
{
117+
self::getInstance()->replace($data);
118+
}
119+
120+
/**
121+
* Deletes an attribute by name and returns its value.
122+
* Optionally defines a default value when the attribute does not exist.
123+
*
124+
* @throws SessionException if session is unstarted
125+
*/
126+
public static function pull(string $name, mixed $default = null): mixed
127+
{
128+
return self::getInstance()->pull($name, $default);
129+
}
130+
131+
/**
132+
* Deletes an attribute by name.
133+
*
134+
* @throws SessionException if session is unstarted
135+
*/
136+
public static function remove(string $name): void
137+
{
138+
self::getInstance()->remove($name);
139+
}
140+
141+
/**
142+
* Free all session variables.
143+
*
144+
* @throws SessionException if session is unstarted
145+
*/
146+
public static function clear(): void
147+
{
148+
self::getInstance()->clear();
149+
}
150+
151+
/**
152+
* Gets the session ID.
153+
*/
154+
public static function getId(): string
155+
{
156+
return self::getInstance()->getId();
157+
}
158+
159+
/**
160+
* Sets the session ID.
161+
*
162+
* @throws SessionException if session already started
163+
*/
164+
public static function setId(string $sessionId): void
165+
{
166+
session_id($sessionId);
167+
}
168+
169+
/**
170+
* Update the current session id with a newly generated one.
171+
*
172+
* @throws SessionException if session is unstarted
173+
*/
174+
public static function regenerateId(bool $deleteOldSession = false): bool
175+
{
176+
return self::getInstance()->regenerateId($deleteOldSession);
177+
}
178+
179+
/**
180+
* Gets the session name.
181+
*/
182+
public static function getName(): string
183+
{
184+
return self::getInstance()->getName();
185+
}
186+
187+
/**
188+
* Sets the session name.
189+
*
190+
* @throws SessionException if session already started
191+
*/
192+
public static function setName(string $name): void
193+
{
194+
self::getInstance()->setName($name);
195+
}
196+
197+
/**
198+
* Destroys the session.
199+
*
200+
* @throws SessionException if session is unstarted
201+
*/
202+
public static function destroy(): bool
203+
{
204+
return self::getInstance()->destroy();
205+
}
206+
207+
/**
208+
* Check if the session is started.
209+
*/
210+
public static function isStarted(): bool
211+
{
212+
return self::getInstance()->isStarted();
213+
}
214+
}

0 commit comments

Comments
 (0)