Skip to content

Commit

Permalink
Initial commit, Session class
Browse files Browse the repository at this point in the history
  • Loading branch information
SosoRicsi committed Nov 26, 2024
0 parents commit 5c36d4d
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
/test.php
19 changes: 19 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "basicphp/superglobals",
"type": "library",
"autoload": {
"psr-4": {
"BasicPHP\\Superglobals\\": "superglobals/"
},
"files": [
"./helpers.php"
]
},
"authors": [
{
"name": "Jarkó Richárd",
"email": "[email protected]"
}
],
"require": {}
}
15 changes: 15 additions & 0 deletions helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

use BasicPHP\Superglobals\Session;

if (!function_exists("session")) {
function session(string $key, $default = '') {
return Session::get($key, $default);
}
}

if (!function_exists("flashed")) {
function flashed(string $key, $default = '') {
return Session::flashed($key, $default);
}
}
10 changes: 10 additions & 0 deletions superglobals/Cookie.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace BasicPHP\Superglobals;

class Cookie
{

//

}
167 changes: 167 additions & 0 deletions superglobals/Session.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
<?php declare(strict_types=1);

namespace BasicPHP\Superglobals;

class Session
{
/**
* Prefix for flash messages stored in the session.
*/
private const FLASH_PREFIX = "flashed_";

/**
* Initializes the session if it is not already started.
*/
public static function init()
{
if (session_id() === "") {
session_start();
}
}

/**
* Sets a session variable with the specified key and value.
*
* @param string $key The session variable name.
* @param mixed $value The value to store in the session.
*/
public static function set(string $key, mixed $value): void
{
$_SESSION[$key] = $value;
}

/**
* Sets a session variable with an expiration time.
*
* @param string $key The session variable name.
* @param mixed $value The value to store in the session.
* @param int $exp Expiration time in seconds (default: 3600 seconds).
* @throws \InvalidArgumentException If the expiration time is not positive.
*/
public static function withExpire(string $key, mixed $value, int $exp = 3600): void
{
if ($exp <= 0) {
throw new \InvalidArgumentException("Expiration time must be a positive integer!");
}

$_SESSION[$key] = [
'data' => $value,
'exp' => time() + $exp
];
}

/**
* Checks if a session variable with an expiration time has expired.
*
* @param string $key The session variable name.
* @return mixed|null The stored value if not expired, or null if expired.
*/
protected static function expired(string $key): mixed
{
$data = $_SESSION[$key] ?? null;

if (isset($data['exp']) && $data['exp'] < time()) {
unset($_SESSION[$key]);
return null;
}

return $data['data'] ?? $data;
}

/**
* Retrieves a session variable by its key.
*
* @param string $key The session variable name.
* @param mixed $default Default value if the variable is not set or expired.
* @return mixed The stored value or the default value.
*/
public static function get(string $key, $default = ''): mixed
{
return self::expired($key) ?? $default;
}

/**
* Deletes one or more session variables.
*
* @param string|array $keys The session variable name(s) to delete.
*/
public static function delete(string|array $keys): void
{
foreach ((array)$keys as $key) {
unset($_SESSION[$key]);
}
}

/**
* Retrieves all session variables.
*
* @return array An associative array of all session variables.
*/
public static function all(): array
{
return $_SESSION ? array_merge([], $_SESSION) : [];
}

/**
* Destroys the current session and clears all session data.
*/
public static function destroy(): void
{
if (session_id() !== '') {
session_unset();
session_destroy();

if (ini_get("session.use_cookies")) {
$params = session_get_cookie_params();
setcookie(session_name(), '', time() - 420000, $params['path'], $params['domain'], $params['secure'], $params['httponly']);
}
}
}

/**
* Counts the number of variables currently stored in the session.
*
* @return int The number of session variables.
*/
public static function count(): int
{
return count($_SESSION);
}

/**
* Sets a flash message in the session.
*
* @param string $value The message to store.
* @param string $key The key to identify the flash message.
*/
public static function flash(string $value, string $key): void
{
$_SESSION[self::FLASH_PREFIX . $key] = $value;
}

/**
* Retrieves and removes a flash message from the session.
*
* @param string $key The key identifying the flash message.
* @param mixed $default Default value if the flash message is not set.
* @return mixed The flash message value or the default value.
*/
public static function flashed(string $key, $default = ''): mixed
{
$data = $_SESSION[self::FLASH_PREFIX . $key] ?? $default;

unset($_SESSION[self::FLASH_PREFIX . $key]);

return $data;
}

/**
* Retrieves the current session ID.
*
* @return string|null The session ID or null if no session is active.
*/
public static function sessid(): string|null
{
return session_id();
}
}

0 comments on commit 5c36d4d

Please sign in to comment.