mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-04-08 17:15:08 +02:00
lib: Don't write the empty notifications array into the session
I combined this w/ introducing some constants for notification types and PHPDoc. refs #9660
This commit is contained in:
parent
9aa1599a18
commit
d545e1f7d1
@ -17,86 +17,72 @@ use Icinga\Web\Session;
|
||||
*/
|
||||
class Notification
|
||||
{
|
||||
/**
|
||||
* Notification type info
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const INFO = 'info';
|
||||
|
||||
/**
|
||||
* Notification type error
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const ERROR = 'error';
|
||||
|
||||
/**
|
||||
* Notification type success
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const SUCCESS = 'success';
|
||||
|
||||
/**
|
||||
* Notification type warning
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const WARNING = 'warning';
|
||||
|
||||
/**
|
||||
* Name of the session key for notification messages
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const SESSION_KEY = 'session';
|
||||
|
||||
/**
|
||||
* Singleton instance
|
||||
*
|
||||
* @var self
|
||||
*/
|
||||
protected static $instance;
|
||||
|
||||
/**
|
||||
* Whether the platform is CLI
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $isCli = false;
|
||||
|
||||
protected $session;
|
||||
|
||||
/**
|
||||
* Notification messages
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $messages = array();
|
||||
|
||||
public static function info($msg)
|
||||
{
|
||||
self::getInstance()->addMessage($msg, 'info');
|
||||
}
|
||||
|
||||
public static function success($msg)
|
||||
{
|
||||
self::getInstance()->addMessage($msg, 'success');
|
||||
}
|
||||
|
||||
public static function warning($msg)
|
||||
{
|
||||
self::getInstance()->addMessage($msg, 'warning');
|
||||
}
|
||||
|
||||
public static function error($msg)
|
||||
{
|
||||
self::getInstance()->addMessage($msg, 'error');
|
||||
}
|
||||
|
||||
protected function addMessage($message, $type = 'info')
|
||||
{
|
||||
if (! in_array(
|
||||
$type,
|
||||
array(
|
||||
'info',
|
||||
'error',
|
||||
'warning',
|
||||
'success'
|
||||
)
|
||||
)) {
|
||||
throw new ProgrammingError(
|
||||
'"%s" is not a valid notification type',
|
||||
$type
|
||||
);
|
||||
}
|
||||
|
||||
if ($this->isCli) {
|
||||
$msg = sprintf('[%s] %s', $type, $message);
|
||||
switch ($type) {
|
||||
case 'info':
|
||||
case 'success':
|
||||
Logger::info($msg);
|
||||
break;
|
||||
case 'warning':
|
||||
Logger::warn($msg);
|
||||
break;
|
||||
case 'error':
|
||||
Logger::error($msg);
|
||||
break;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
$this->messages[] = (object) array(
|
||||
'type' => $type,
|
||||
'message' => $message,
|
||||
);
|
||||
}
|
||||
|
||||
public function hasMessages()
|
||||
{
|
||||
return false === empty($this->messages);
|
||||
}
|
||||
|
||||
public function getMessages()
|
||||
{
|
||||
$messages = $this->messages;
|
||||
$this->messages = array();
|
||||
return $messages;
|
||||
}
|
||||
/**
|
||||
* Session
|
||||
*
|
||||
* @var Session
|
||||
*/
|
||||
protected $session;
|
||||
|
||||
/**
|
||||
* Create the notification instance
|
||||
*/
|
||||
final private function __construct()
|
||||
{
|
||||
if (Platform::isCli()) {
|
||||
@ -105,33 +91,130 @@ class Notification
|
||||
}
|
||||
|
||||
$this->session = Session::getSession();
|
||||
|
||||
$stored = $this->session->get('messages');
|
||||
if (is_array($stored)) {
|
||||
$this->messages = $stored;
|
||||
$this->session->set('messages', array());
|
||||
$messages = $this->session->get(self::SESSION_KEY);
|
||||
if (is_array($messages)) {
|
||||
$this->messages = $messages;
|
||||
$this->session->delete(self::SESSION_KEY);
|
||||
$this->session->write();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Notification instance
|
||||
*
|
||||
* @return Notification
|
||||
*/
|
||||
public static function getInstance()
|
||||
{
|
||||
if (self::$instance === null) {
|
||||
self::$instance = new Notification();
|
||||
self::$instance = new self();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add info notification
|
||||
*
|
||||
* @param string $msg
|
||||
*/
|
||||
public static function info($msg)
|
||||
{
|
||||
self::getInstance()->addMessage($msg, self::INFO);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add error notification
|
||||
*
|
||||
* @param string $msg
|
||||
*/
|
||||
public static function error($msg)
|
||||
{
|
||||
self::getInstance()->addMessage($msg, self::ERROR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add success notification
|
||||
*
|
||||
* @param string $msg
|
||||
*/
|
||||
public static function success($msg)
|
||||
{
|
||||
self::getInstance()->addMessage($msg, self::SUCCESS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add warning notification
|
||||
*
|
||||
* @param string $msg
|
||||
*/
|
||||
public static function warning($msg)
|
||||
{
|
||||
self::getInstance()->addMessage($msg, self::WARNING);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a notification message
|
||||
*
|
||||
* @param string $message
|
||||
* @param string $type
|
||||
*/
|
||||
protected function addMessage($message, $type = self::INFO)
|
||||
{
|
||||
if ($this->isCli) {
|
||||
$msg = sprintf('[%s] %s', $type, $message);
|
||||
switch ($type) {
|
||||
case self::INFO:
|
||||
case self::SUCCESS:
|
||||
Logger::info($msg);
|
||||
break;
|
||||
case self::ERROR:
|
||||
Logger::error($msg);
|
||||
break;
|
||||
case self::WARNING:
|
||||
Logger::warning($msg);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
$this->messages[] = (object) array(
|
||||
'type' => $type,
|
||||
'message' => $message,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the notification messages
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getMessages()
|
||||
{
|
||||
$messages = $this->messages;
|
||||
$this->messages = array();
|
||||
return $messages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get whether notification messages have been added
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function hasMessages()
|
||||
{
|
||||
return ! empty($this->messages);
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy the notification instance
|
||||
*/
|
||||
final public function __destruct()
|
||||
{
|
||||
if ($this->isCli) {
|
||||
return;
|
||||
}
|
||||
if ($this->session->get('messages') !== $this->messages) {
|
||||
$this->session->set('messages', $this->messages);
|
||||
if ($this->hasMessages() && $this->session->get('messages') !== $this->messages) {
|
||||
$this->session->set(self::SESSION_KEY, $this->messages);
|
||||
$this->session->write();
|
||||
}
|
||||
$this->session->write();
|
||||
|
||||
unset($this->session);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user