1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-07 15:26:54 +02:00

Update Session API var to add a $session->sessionHandler() method that returns an instance to the current WireSessionHandler instance, or null if using PHP file-based sessions.

This commit is contained in:
Ryan Cramer
2020-09-11 14:23:16 -04:00
parent 2e81d39275
commit 2ed6494494
2 changed files with 38 additions and 1 deletions

View File

@@ -119,6 +119,14 @@ class Session extends Wire implements \IteratorAggregate {
*/
protected $sessionAllow = null;
/**
* Instance of custom session handler module, when in use (null when not)
*
* @var WireSessionHandler|null
*
*/
protected $sessionHandler = null;
/**
* Name of key/index within $_SESSION where PW keeps its session data
*
@@ -1486,5 +1494,22 @@ class Session extends Wire implements \IteratorAggregate {
if(is_null($this->CSRF)) $this->CSRF = $this->wire(new SessionCSRF());
return $this->CSRF;
}
/**
* Get or set current session handler instance (WireSessionHandler)
*
* #pw-internal
*
* @param WireSessionHandler|null $sessionHandler Specify only when setting, omit to get session handler.
* @return null|WireSessionHandler Returns WireSessionHandler instance, or…
* returns null when session handler is not yet known or is PHP (file system)
* @since 3.0.166
*
*/
public function sessionHandler(WireSessionHandler $sessionHandler = null) {
if($sessionHandler) $this->sessionHandler = $sessionHandler;
return $this->sessionHandler;
}
}

View File

@@ -20,7 +20,7 @@ abstract class WireSessionHandler extends WireData implements Module {
*/
public function wired() {
if(!$this->sessionExists()) {
$this->addHookBefore('Session::init', $this, 'attach');
$this->addHookBefore('Session::init', $this, 'hookSessionInit');
register_shutdown_function('session_write_close');
}
}
@@ -31,6 +31,18 @@ abstract class WireSessionHandler extends WireData implements Module {
*/
public function init() { }
/**
* Hook before Session::init
*
* @param HookEvent $event
*
*/
public function hookSessionInit(HookEvent $event) {
$session = $event->object; /** @var Session $session */
$this->attach();
$session->sessionHandler($this);
}
/**
* Attach this as the session handler
*