Files
bedrock/Functions/Session.php
2024-08-09 01:00:02 -04:00

131 lines
4.0 KiB
PHP

<?php
/**
* functions/session.php
*
* This class is used for management of session data.
*
* @version 3.0
* @author Joey Kimsey <Joey@thetempusproject.com>
* @link https://TheTempusProject.com/Core
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
*/
namespace TheTempusProject\Bedrock\Functions;
use TheTempusProject\Canary\Bin\Canary as Debug;
class Session {
/**
* Checks if a session exists.
*
* @param {string} [$name] - The name of the session being checked for.
* @return {bool}
*/
public static function exists( $name ) {
if ( !Check::sessionName( $name ) ) {
return false;
}
$sessionName = DEFAULT_SESSION_PREFIX . $name;
if ( isset( $_SESSION[$sessionName] ) ) {
return true;
}
Debug::info("Session::exists - Session not found: $sessionName");
return false;
}
/**
* Retrieves the value of a session if it exists
*
* @param {string} [$name] - The name of the session variable you are trying to retrieve.
* @return {string|bool} - Returns the data from the session or false if nothing is found..
*/
public static function get( $name ) {
if ( !Check::sessionName( $name ) ) {
return false;
}
if ( self::exists( $name ) ) {
$sessionName = DEFAULT_SESSION_PREFIX . $name;
return $_SESSION[$sessionName];
}
Debug::info("Session::get - Session not found: $name");
return false;
}
/**
* Creates a session.
*
* @param {string} [$name] - Session name.
* @param {string} [$data] - Session data.
* @return {bool}
*/
public static function put( $name, $data ) {
if ( !Check::sessionName( $name ) ) {
return false;
}
$sessionName = DEFAULT_SESSION_PREFIX . $name;
$_SESSION[$sessionName] = $data;
Debug::info("Session: Created: $sessionName");
return true;
}
/**
* Deletes the specified session.
*
* @param {string} [$name] - The name of the session to be destroyed.
* @return {bool}
*/
public static function delete( $name ) {
if ( !Check::sessionName( $name ) ) {
return false;
}
if ( self::exists( $name ) ) {
$sessionName = DEFAULT_SESSION_PREFIX . $name;
unset( $_SESSION[$sessionName] );
Debug::info("Session::deleted: $sessionName");
return true;
}
Debug::error("Session::delete - Session not found.");
return false;
}
/**
* Intended as a self-destruct session. If the specified session does not
* exist, it is created. If the specified session does exist, it will be
* destroyed and returned.
*
* @param {string} [$name] - Session name to be created or checked
* @param {string} [$data] - The string to be used if session needs to be created. (optional)
* @return bool|string - Returns bool if creating, and a string if the check is successful.
*/
public static function checkFlash( $name ) {
if ( !Check::sessionName( $name ) ) {
return false;
}
if ( self::exists( $name ) ) {
Debug::info("Session::flash - Exists");
$session = self::get( $name );
self::delete( $name );
return $session;
}
return;
}
public static function flash( $name, $data = null ) {
if ( !Check::sessionName( $name ) ) {
return false;
}
if ( !empty( $data ) ) {
self::put( $name, $data );
Debug::info("Session::flash - Session created.");
return true;
}
if ( self::exists( $name ) ) {
Debug::info("Session::flash - Exists");
$session = self::get( $name );
self::delete( $name );
return $session;
}
Debug::error("Session::flash - null return");
return;
}
}