diff --git a/library/Icinga/Application/Web.php b/library/Icinga/Application/Web.php index 0c7ea0ad8..0add09089 100644 --- a/library/Icinga/Application/Web.php +++ b/library/Icinga/Application/Web.php @@ -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 * diff --git a/library/Icinga/Web/Notification.php b/library/Icinga/Web/Notification.php index 1e719c4e7..1dc35e981 100644 --- a/library/Icinga/Web/Notification.php +++ b/library/Icinga/Web/Notification.php @@ -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); + } }