123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- <?php
- namespace Cube\Authentication\Storage;
- use Cube\Controller\Front,
- Cube\Session as SessionObject;
- class Session implements StorageInterface
- {
- const SESSION_NAMESPACE = 'CubeAuthentication';
- const DEFAULT_KEY = 'AuthStorage';
-
- protected $_namespace;
-
- protected $_key;
-
- protected $_session;
-
- public function __construct($namespace = null, $key = null, SessionObject $session = null)
- {
- $this->setNamespace($namespace)
- ->setKey($key)
- ->setSession($session);
- }
-
- public function getNamespace()
- {
- return $this->_namespace;
- }
-
- public function setNamespace($namespace = null)
- {
- if ($namespace === null) {
- $namespace = self::SESSION_NAMESPACE;
- }
- $this->_namespace = $namespace;
- return $this;
- }
-
- public function getKey()
- {
- return $this->_key;
- }
-
- public function setKey($key = null)
- {
- if ($key === null) {
- $key = self::DEFAULT_KEY;
- }
- $this->_key = $key;
- return $this;
- }
-
- public function getSession()
- {
- if (!($this->_session instanceof Session)) {
- $this->setSession();
- }
- return $this->_session;
- }
-
- public function setSession(SessionObject $session = null)
- {
- if ($session === null) {
- $session = Front::getInstance()->getBootstrap()->getResource('session');
- }
- if (!$session instanceof SessionObject) {
- $session = new SessionObject();
- $session->setNamespace(
- $this->getNamespace());
- }
- $this->_session = $session;
- return $this;
- }
-
- public function clear()
- {
- $this->_session->unregister($this->_key);
- return $this;
- }
-
- public function isEmpty()
- {
- $data = $this->_session->{$this->_key};
- return !isset($data);
- }
-
- public function read()
- {
- return $this->_session->{$this->_key};
- }
-
- public function write($contents)
- {
- $this->_session->{$this->_key} = $contents;
- return $this;
- }
- }
|