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

View File

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