Add helper to get window-aware session namespaces

This commit is contained in:
Johannes Meyer 2014-03-25 10:31:52 +01:00
parent 04cab7e0f8
commit 9ab53558f0
4 changed files with 62 additions and 21 deletions

View File

@ -78,6 +78,28 @@ abstract class Session extends SessionNamespace
return $this->namespaces[$identifier];
}
/**
* Return whether the given session namespace exists
*
* @param string $identifier The namespace's identifier to check
*
* @return bool
*/
public function hasNamespace($identifier)
{
return isset($this->namespaces[$identifier]);
}
/**
* Remove the given session namespace
*
* @param string $identifier The identifier of the namespace to remove
*/
public function removeNamespace($identifier)
{
unset($this->namespaces[$identifier]);
}
/**
* Clear all values and namespaces from the session cache
*/

View File

@ -30,11 +30,13 @@
namespace Icinga\Session;
use \Exception;
use \ArrayIterator;
use \IteratorAggregate;
/**
* Container for session values
*/
class SessionNamespace
class SessionNamespace implements IteratorAggregate
{
/**
* The actual values stored in this container
@ -43,6 +45,16 @@ class SessionNamespace
*/
protected $values = array();
/**
* Return an iterator for all values in this namespace
*
* @return ArrayIterator
*/
public function getIterator()
{
return ArrayIterator($this->values);
}
/**
* Set a session value by property access
*

View File

@ -41,10 +41,10 @@ use Icinga\Util\Translator;
use Icinga\Web\Widget\Tabs;
use Icinga\Web\Url;
use Icinga\Web\Notification;
use Icinga\Logger\Logger;
use Icinga\Web\Request;
use Icinga\File\Pdf;
use Icinga\Exception\ProgrammingError;
use Icinga\Web\Session;
use Icinga\Session\SessionNamespace;
/**
* Base class for all core action controllers
@ -149,6 +149,31 @@ class ActionController extends Zend_Controller_Action
return $this->windowId;
}
/**
* Return a window-aware session by using the given prefix
*
* @param string $prefix The prefix to use
*
* @return SessionNamespace
*/
public function getWindowSession($prefix)
{
$session = Session::getSession();
$windowId = $this->getWindowId();
$namespace = $session->getNamespace($prefix . '_' . $windowId);
if ($windowId !== 'undefined' && $session->hasNamespace($prefix . '_undefined')) {
// We do not have any window-id on the very first request. Now we add all values from the
// namespace, that has been created in this case, to the new one and remove it afterwards.
foreach ($session->getNamespace($prefix . '_undefined') as $name => $value) {
$namespace->set($name, $value);
}
$session->removeNamespace($prefix . '_undefined');
}
return $namespace;
}
/**
* Return restriction information for an eventually authenticated user
*

View File

@ -30,7 +30,6 @@
namespace Icinga\Web;
use Icinga\Session\PhpSession;
use Icinga\Session\SessionNamespace;
use Icinga\Session\Session as BaseSession;
use Icinga\Exception\ProgrammingError;
@ -78,21 +77,4 @@ class Session
return self::$session;
}
/**
* Get or create a new session namespace
*
* @param string $identifier The namespace's identifier
*
* @return SessionNamespace
* @throws ProgrammingError
*/
public static function getNamespace($identifier)
{
if (self::$session === null) {
throw new ProgrammingError('No session created yet');
}
return self::$session->getNamespace($identifier);
}
}