Session: reworked to avoid duplicate notifications

* clear session on initialization, store once on shutdown
* less static method calls
* fixes erraneous cli checks
This commit is contained in:
Thomas Gelf 2015-03-13 13:49:45 +01:00
parent e90200160f
commit 63f87da53d
2 changed files with 45 additions and 15 deletions

View File

@ -16,6 +16,7 @@ use Icinga\Authentication\Manager;
use Icinga\User; use Icinga\User;
use Icinga\Util\TimezoneDetect; use Icinga\Util\TimezoneDetect;
use Icinga\Util\Translator; use Icinga\Util\Translator;
use Icinga\Web\Notification;
use Icinga\Web\Request; use Icinga\Web\Request;
use Icinga\Web\Response; use Icinga\Web\Response;
use Icinga\Web\Session; use Icinga\Web\Session;
@ -89,6 +90,7 @@ class Web extends ApplicationBootstrap
->loadConfig() ->loadConfig()
->setupResourceFactory() ->setupResourceFactory()
->setupSession() ->setupSession()
->setupNotifications()
->setupUser() ->setupUser()
->setupTimezone() ->setupTimezone()
->setupLogger() ->setupLogger()
@ -195,6 +197,17 @@ class Web extends ApplicationBootstrap
return $this; return $this;
} }
/**
* Initialize notifications to remove them immediately from session
*
* @return $this
*/
private function setupNotifications()
{
Notification::getInstance();
return $this;
}
/** /**
* Inject dependencies into request * Inject dependencies into request
* *

View File

@ -18,8 +18,13 @@ use Icinga\Web\Session;
class Notification class Notification
{ {
protected static $instance; protected static $instance;
protected $isCli = false; protected $isCli = false;
protected $session;
protected $messages = array();
public static function info($msg) public static function info($msg)
{ {
self::getInstance()->addMessage($msg, 'info'); self::getInstance()->addMessage($msg, 'info');
@ -74,8 +79,7 @@ class Notification
return; return;
} }
$messages = & Session::getSession()->getByRef('messages'); $this->messages[] = (object) array(
$messages[] = (object) array(
'type' => $type, 'type' => $type,
'message' => $message, 'message' => $message,
); );
@ -83,30 +87,30 @@ class Notification
public function hasMessages() public function hasMessages()
{ {
$session = Session::getSession(); return false === empty($this->messages);
return false === empty($session->messages);
} }
public function getMessages() public function getMessages()
{ {
$session = Session::getSession(); $messages = $this->messages;
$messages = $session->messages; $this->messages = array();
if (false === empty($messages)) {
$session->messages = array();
}
return $messages; return $messages;
} }
final private function __construct() final private function __construct()
{ {
$session = Session::getSession(); if (Platform::isCli()) {
if (!is_array($session->get('messages'))) { $this->isCli = true;
$session->messages = array(); return;
} }
if (Platform::isCli()) { $this->session = Session::getSession();
$this->is_cli = true;
$stored = $this->session->get('messages');
if (is_array($stored)) {
$this->messages = $stored;
$this->session->set('messages', array());
$this->session->write();
} }
} }
@ -117,4 +121,17 @@ class Notification
} }
return self::$instance; return self::$instance;
} }
final public function __destruct()
{
if ($this->isCli) {
return;
}
if ($this->session->get('messages') !== $this->messages) {
$this->session->set('messages', $this->messages);
}
$this->session->write();
unset($this->session);
}
} }