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) { foreach ($_SESSION as $key => $value) {
if (strpos($key, self::NAMESPACE_PREFIX) === 0) { if (strpos($key, self::NAMESPACE_PREFIX) === 0) {
$namespace = new SessionNamespace(); $namespace = new SessionNamespace($this);
$namespace->setAll($value); $namespace->setAll($value);
$this->namespaces[substr($key, strlen(self::NAMESPACE_PREFIX))] = $namespace; $this->namespaces[substr($key, strlen(self::NAMESPACE_PREFIX))] = $namespace;
} else { } else {

View File

@ -29,6 +29,8 @@
namespace Icinga\Web\Session; namespace Icinga\Web\Session;
use Icinga\Exception\NotImplementedError;
/** /**
* Base class for handling sessions * Base class for handling sessions
*/ */
@ -56,7 +58,9 @@ abstract class Session extends SessionNamespace
/** /**
* Persists changes to the underlying session implementation * 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 * Purge session
@ -82,7 +86,7 @@ abstract class Session extends SessionNamespace
unset($this->removedNamespaces[array_search($identifier, $this->removedNamespaces)]); unset($this->removedNamespaces[array_search($identifier, $this->removedNamespaces)]);
} }
$this->namespaces[$identifier] = new SessionNamespace(); $this->namespaces[$identifier] = new SessionNamespace($this);
} }
return $this->namespaces[$identifier]; return $this->namespaces[$identifier];

View File

@ -38,6 +38,13 @@ use \IteratorAggregate;
*/ */
class SessionNamespace implements IteratorAggregate class SessionNamespace implements IteratorAggregate
{ {
/**
* The session this namespace is associated to
*
* @var Session
*/
protected $session;
/** /**
* The actual values stored in this container * The actual values stored in this container
* *
@ -52,6 +59,16 @@ class SessionNamespace implements IteratorAggregate
*/ */
protected $removed = array(); 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 * Return an iterator for all values in this namespace
* *
@ -169,4 +186,16 @@ class SessionNamespace implements IteratorAggregate
$this->set($key, $value); $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) { if ($this->session !== null) {
$this->session->calculationBase = $new; $this->session->calculationBase = $new;
Session::getSession()->write(); // TODO: Should it be possible to call write() on the namespace? $this->session->write();
} }
} else { } else {
$this->calculationBase = $calculationBase; $this->calculationBase = $calculationBase;