Make it possible to save the session by its associated session namespaces
This commit is contained in:
parent
98b640f1e0
commit
8be3ccc527
|
@ -143,7 +143,7 @@ class PhpSession extends Session
|
|||
|
||||
foreach ($_SESSION as $key => $value) {
|
||||
if (strpos($key, self::NAMESPACE_PREFIX) === 0) {
|
||||
$namespace = new SessionNamespace();
|
||||
$namespace = new SessionNamespace($this);
|
||||
$namespace->setAll($value);
|
||||
$this->namespaces[substr($key, strlen(self::NAMESPACE_PREFIX))] = $namespace;
|
||||
} else {
|
||||
|
|
|
@ -29,6 +29,8 @@
|
|||
|
||||
namespace Icinga\Web\Session;
|
||||
|
||||
use Icinga\Exception\NotImplementedError;
|
||||
|
||||
/**
|
||||
* Base class for handling sessions
|
||||
*/
|
||||
|
@ -56,7 +58,9 @@ abstract class Session extends SessionNamespace
|
|||
/**
|
||||
* Persists changes to the underlying session implementation
|
||||
*/
|
||||
abstract public function write();
|
||||
public function write() {
|
||||
throw new NotImplementedError('You are required to implement write() in your session implementation');
|
||||
}
|
||||
|
||||
/**
|
||||
* Purge session
|
||||
|
@ -82,7 +86,7 @@ abstract class Session extends SessionNamespace
|
|||
unset($this->removedNamespaces[array_search($identifier, $this->removedNamespaces)]);
|
||||
}
|
||||
|
||||
$this->namespaces[$identifier] = new SessionNamespace();
|
||||
$this->namespaces[$identifier] = new SessionNamespace($this);
|
||||
}
|
||||
|
||||
return $this->namespaces[$identifier];
|
||||
|
|
|
@ -38,6 +38,13 @@ use \IteratorAggregate;
|
|||
*/
|
||||
class SessionNamespace implements IteratorAggregate
|
||||
{
|
||||
/**
|
||||
* The session this namespace is associated to
|
||||
*
|
||||
* @var Session
|
||||
*/
|
||||
protected $session;
|
||||
|
||||
/**
|
||||
* The actual values stored in this container
|
||||
*
|
||||
|
@ -52,6 +59,16 @@ class SessionNamespace implements IteratorAggregate
|
|||
*/
|
||||
protected $removed = array();
|
||||
|
||||
/**
|
||||
* Create a new session namespace
|
||||
*
|
||||
* @param Session $session The session this namespace is associated to
|
||||
*/
|
||||
public function __construct(Session $session = null)
|
||||
{
|
||||
$this->session = $session;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an iterator for all values in this namespace
|
||||
*
|
||||
|
@ -169,4 +186,16 @@ class SessionNamespace implements IteratorAggregate
|
|||
$this->set($key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the session this namespace is associated to
|
||||
*/
|
||||
public function write()
|
||||
{
|
||||
if (!$this->session) {
|
||||
throw new Exception('Cannot save, session not set');
|
||||
}
|
||||
|
||||
$this->session->write();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -278,7 +278,7 @@ class TimeLine implements IteratorAggregate
|
|||
|
||||
if ($this->session !== null) {
|
||||
$this->session->calculationBase = $new;
|
||||
Session::getSession()->write(); // TODO: Should it be possible to call write() on the namespace?
|
||||
$this->session->write();
|
||||
}
|
||||
} else {
|
||||
$this->calculationBase = $calculationBase;
|
||||
|
|
Loading…
Reference in New Issue