Skip to content

Commit

Permalink
fix: php 8.4 session handler compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
ikkez committed Dec 28, 2024
1 parent cbb6aa0 commit 391d1cb
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 85 deletions.
55 changes: 34 additions & 21 deletions db/jig/session.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

namespace DB\Jig;

use ReturnTypeWillChange;
use SessionAdapter;

//! Jig-managed session handler
class Session extends Mapper {

Expand All @@ -43,26 +46,30 @@ class Session extends Mapper {
* @param $path string
* @param $name string
**/
function open($path,$name) {
return TRUE;
}
function open(string $path, string $name): bool
{
return TRUE;
}

/**
* Close session
* @return TRUE
**/
function close() {
function close(): bool
{
$this->reset();
$this->sid=NULL;
return TRUE;
}

/**
* Return session data in serialized format
* @return string
* @return string|false
* @param $id string
**/
function read($id) {
#[ReturnTypeWillChange]
function read($id)
{
$this->load(['@session_id=?',$this->sid=$id]);
if ($this->dry())
return '';
Expand All @@ -87,7 +94,8 @@ function read($id) {
* @param $id string
* @param $data string
**/
function write($id,$data) {
function write(string $id, string $data): bool
{
$this->set('session_id',$id);
$this->set('data',$data);
$this->set('ip',$this->_ip);
Expand All @@ -102,19 +110,19 @@ function write($id,$data) {
* @return TRUE
* @param $id string
**/
function destroy($id) {
function destroy($id): bool
{
$this->erase(['@session_id=?',$id]);
return TRUE;
}

/**
* Garbage collector
* @return TRUE
* @param $max int
**/
function cleanup($max) {
$this->erase(['@stamp+?<?',$max,time()]);
return TRUE;
#[ReturnTypeWillChange]
function gc(int $max_lifetime): int
{
return (int) $this->erase(['@stamp+?<?',$max_lifetime,time()]);
}

/**
Expand Down Expand Up @@ -169,14 +177,19 @@ function agent() {
function __construct(\DB\Jig $db,$file='sessions',$onsuspect=NULL,$key=NULL) {
parent::__construct($db,$file);
$this->onsuspect=$onsuspect;
session_set_save_handler(
[$this,'open'],
[$this,'close'],
[$this,'read'],
[$this,'write'],
[$this,'destroy'],
[$this,'cleanup']
);
if (version_compare(PHP_VERSION, '8.4.0')>=0) {
// TODO: remove this when php7 support is dropped
session_set_save_handler(new SessionAdapter($this));
} else {
session_set_save_handler(
[$this,'open'],
[$this,'close'],
[$this,'read'],
[$this,'write'],
[$this,'destroy'],
[$this,'gc']
);
}
register_shutdown_function('session_commit');
$fw=\Base::instance();
$headers=$fw->HEADERS;
Expand Down
49 changes: 31 additions & 18 deletions db/mongo/session.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

namespace DB\Mongo;

use ReturnTypeWillChange;
use SessionAdapter;

//! MongoDB-managed session handler
class Session extends Mapper {

Expand All @@ -43,15 +46,17 @@ class Session extends Mapper {
* @param $path string
* @param $name string
**/
function open($path,$name) {
function open(string $path, string $name): bool
{
return TRUE;
}

/**
* Close session
* @return TRUE
**/
function close() {
function close(): bool
{
$this->reset();
$this->sid=NULL;
return TRUE;
Expand All @@ -62,7 +67,9 @@ function close() {
* @return string
* @param $id string
**/
function read($id) {
#[ReturnTypeWillChange]
function read(string $id)
{
$this->load(['session_id'=>$this->sid=$id]);
if ($this->dry())
return '';
Expand All @@ -87,7 +94,8 @@ function read($id) {
* @param $id string
* @param $data string
**/
function write($id,$data) {
function write(string $id, string $data): bool
{
$this->set('session_id',$id);
$this->set('data',$data);
$this->set('ip',$this->_ip);
Expand All @@ -102,19 +110,19 @@ function write($id,$data) {
* @return TRUE
* @param $id string
**/
function destroy($id) {
function destroy($id): bool
{
$this->erase(['session_id'=>$id]);
return TRUE;
}

/**
* Garbage collector
* @return TRUE
* @param $max int
**/
function cleanup($max) {
$this->erase(['$where'=>'this.stamp+'.$max.'<'.time()]);
return TRUE;
#[ReturnTypeWillChange]
function gc(int $max_lifetime): int
{
return (int) $this->erase(['$where'=>'this.stamp+'.$max_lifetime.'<'.time()]);
}

/**
Expand Down Expand Up @@ -169,14 +177,19 @@ function agent() {
function __construct(\DB\Mongo $db,$table='sessions',$onsuspect=NULL,$key=NULL) {
parent::__construct($db,$table);
$this->onsuspect=$onsuspect;
session_set_save_handler(
[$this,'open'],
[$this,'close'],
[$this,'read'],
[$this,'write'],
[$this,'destroy'],
[$this,'cleanup']
);
if (version_compare(PHP_VERSION, '8.4.0')>=0) {
// TODO: remove this when php7 support is dropped
session_set_save_handler(new SessionAdapter($this));
} else {
session_set_save_handler(
[$this,'open'],
[$this,'close'],
[$this,'read'],
[$this,'write'],
[$this,'destroy'],
[$this,'gc']
);
}
register_shutdown_function('session_commit');
$fw=\Base::instance();
$headers=$fw->HEADERS;
Expand Down
51 changes: 32 additions & 19 deletions db/sql/session.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

namespace DB\SQL;

use ReturnTypeWillChange;
use SessionAdapter;

//! SQL-managed session handler
class Session extends Mapper {

Expand All @@ -43,26 +46,30 @@ class Session extends Mapper {
* @param $path string
* @param $name string
**/
function open($path,$name) {
function open(string $path, string $name): bool
{
return TRUE;
}

/**
* Close session
* @return TRUE
**/
function close() {
function close(): bool
{
$this->reset();
$this->sid=NULL;
return TRUE;
}

/**
* Return session data in serialized format
* @return string
* @return string|false
* @param $id string
**/
function read($id) {
#[ReturnTypeWillChange]
function read(string $id)
{
$this->load(['session_id=?',$this->sid=$id]);
if ($this->dry())
return '';
Expand All @@ -86,7 +93,8 @@ function read($id) {
* @param $id string
* @param $data string
**/
function write($id,$data) {
function write(string $id, string $data): bool
{
$this->set('session_id',$id);
$this->set('data',$data);
$this->set('ip',$this->_ip);
Expand All @@ -101,19 +109,19 @@ function write($id,$data) {
* @return TRUE
* @param $id string
**/
function destroy($id) {
function destroy($id): bool
{
$this->erase(['session_id=?',$id]);
return TRUE;
}

/**
* Garbage collector
* @return TRUE
* @param $max int
**/
function cleanup($max) {
$this->erase(['stamp+?<?',$max,time()]);
return TRUE;
#[ReturnTypeWillChange]
function gc(int $max_lifetime): int
{
return (int) $this->erase(['stamp+?<?',$max_lifetime,time()]);
}

/**
Expand Down Expand Up @@ -194,14 +202,19 @@ function __construct(\DB\SQL $db,$table='sessions',$force=TRUE,$onsuspect=NULL,$
}
parent::__construct($db,$table);
$this->onsuspect=$onsuspect;
session_set_save_handler(
[$this,'open'],
[$this,'close'],
[$this,'read'],
[$this,'write'],
[$this,'destroy'],
[$this,'cleanup']
);
if (version_compare(PHP_VERSION, '8.4.0')>=0) {
// TODO: remove this when php7 support is dropped
session_set_save_handler(new SessionAdapter($this));
} else {
session_set_save_handler(
[$this,'open'],
[$this,'close'],
[$this,'read'],
[$this,'write'],
[$this,'destroy'],
[$this,'gc']
);
}
register_shutdown_function('session_commit');
$fw=\Base::instance();
$headers=$fw->HEADERS;
Expand Down
Loading

0 comments on commit 391d1cb

Please sign in to comment.