From 8be3ccc527854be690eee3ab032b74c9bc465e7a Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Fri, 4 Apr 2014 11:13:07 +0200 Subject: [PATCH] Make it possible to save the session by its associated session namespaces --- library/Icinga/Web/Session/PhpSession.php | 2 +- library/Icinga/Web/Session/Session.php | 8 +++-- .../Icinga/Web/Session/SessionNamespace.php | 29 +++++++++++++++++++ .../library/Monitoring/Timeline/TimeLine.php | 2 +- 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/library/Icinga/Web/Session/PhpSession.php b/library/Icinga/Web/Session/PhpSession.php index 5df9e8744..df04183bd 100644 --- a/library/Icinga/Web/Session/PhpSession.php +++ b/library/Icinga/Web/Session/PhpSession.php @@ -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 { diff --git a/library/Icinga/Web/Session/Session.php b/library/Icinga/Web/Session/Session.php index e81274e73..a70ba6723 100644 --- a/library/Icinga/Web/Session/Session.php +++ b/library/Icinga/Web/Session/Session.php @@ -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]; diff --git a/library/Icinga/Web/Session/SessionNamespace.php b/library/Icinga/Web/Session/SessionNamespace.php index 4360e2828..bfe7e6433 100644 --- a/library/Icinga/Web/Session/SessionNamespace.php +++ b/library/Icinga/Web/Session/SessionNamespace.php @@ -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(); + } } diff --git a/modules/monitoring/library/Monitoring/Timeline/TimeLine.php b/modules/monitoring/library/Monitoring/Timeline/TimeLine.php index 5bfa32ff3..6aeb1a327 100644 --- a/modules/monitoring/library/Monitoring/Timeline/TimeLine.php +++ b/modules/monitoring/library/Monitoring/Timeline/TimeLine.php @@ -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;