diff --git a/wire/core/Session.php b/wire/core/Session.php index e3c02ffd..07cc42df 100644 --- a/wire/core/Session.php +++ b/wire/core/Session.php @@ -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; + } + } diff --git a/wire/core/WireSessionHandler.php b/wire/core/WireSessionHandler.php index 3aa53241..a4a933b1 100644 --- a/wire/core/WireSessionHandler.php +++ b/wire/core/WireSessionHandler.php @@ -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 *