Make it possible to save the session by its associated session namespaces

This commit is contained in:
Johannes Meyer 2014-04-04 11:13:07 +02:00
parent 98b640f1e0
commit 8be3ccc527
4 changed files with 37 additions and 4 deletions

View File

@ -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 {

View File

@ -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];

View File

@ -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();
}
}

View File

@ -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;