$notification) { if (Notification::TYPE_PERSISTENT != $notification->type) { static::removeNotification($id); } } } /** * Determine all notifications that needs to be displayed. They are sorted by priority. Highest priorities first. * @return \ArrayObject */ public static function getAllNotificationsToDisplay() { $notifications = static::getAllNotifications(); uasort($notifications, function ($n1, $n2) { /** @var Notification $n1 */ /** @var Notification $n2 */ if ($n1->getPriority() == $n2->getPriority()) { return 0; } return $n1->getPriority() > $n2->getPriority() ? -1 : 1; }); return $notifications; } /** * @param $id * @throws \Exception In case id is empty or if id contains non word characters */ private static function checkId($id) { if (empty($id)) { throw new \Exception('Notification ID is empty.'); } if (!preg_match('/^(\w)*$/', $id)) { throw new \Exception('Invalid Notification ID given. Only word characters (AlNum + underscore) allowed.'); } } private static function addNotification($id, Notification $notification) { self::saveNotificationAcrossUiRequestsIfNeeded($id, $notification); // we store all kinda notifications here so in case the session is not enabled or disabled later there is still // a chance it gets delivered to the UI during the same request. self::$notifications[$id] = $notification; } private static function saveNotificationAcrossUiRequestsIfNeeded($id, Notification $notification) { if (self::isSessionEnabled()) { // we need to save even non persistent notifications if possible. Otherwise if there's a redirect // a notification is not shown on the next page view $session = static::getSession(); $session->notifications[$id] = $notification; } } private static function removeOldestNotificationsIfThereAreTooMany() { if (!self::isSessionEnabled()) { return; } $maxNotificationsInSession = 30; $session = static::getSession(); while (count($session->notifications) >= $maxNotificationsInSession) { array_shift($session->notifications); } } private static function getAllNotifications() { if (!self::isSessionEnabled()) { return array(); } $notifications = self::$notifications; foreach ($notifications as $id => $notification) { // we copy them over to the session if possible and persist it in case the session was not yet // writable / enabled at the time the notification was added. self::saveNotificationAcrossUiRequestsIfNeeded($id, $notification); } if (self::isSessionEnabled()) { $session = static::getSession(); foreach ($session->notifications as $id => $notification) { $notifications[$id] = $notification; } } return $notifications; } private static function removeNotification($id) { if (array_key_exists($id, self::$notifications)) { unset(self::$notifications[$id]); } if (self::isSessionEnabled()) { $session = static::getSession(); if (array_key_exists($id, $session->notifications)) { unset($session->notifications[$id]); } } } private static function isSessionEnabled() { return Session::isWritable() && Session::isReadable(); } /** * @return SessionNamespace */ private static function getSession() { if (!isset(static::$session)) { static::$session = new SessionNamespace('notification'); } if (empty(static::$session->notifications) && self::isSessionEnabled()) { static::$session->notifications = array(); } return static::$session; } }