From: Michael M Slusarz Date: Tue, 9 Feb 2010 01:37:46 +0000 (-0700) Subject: Store notifications events as Horde_Notification_Event objects X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=499b725b8970672ba487fc951a42929c82168727;p=horde.git Store notifications events as Horde_Notification_Event objects --- diff --git a/framework/Notification/lib/Horde/Notification/Event.php b/framework/Notification/lib/Horde/Notification/Event.php index 03bc615b9..3d8305333 100644 --- a/framework/Notification/lib/Horde/Notification/Event.php +++ b/framework/Notification/lib/Horde/Notification/Event.php @@ -17,38 +17,78 @@ class Horde_Notification_Event * * @var string */ - protected $_message = ''; + public $message = ''; /** - * If passed, sets the message for this event. + * The flags for this message. * - * @param string $message The text message for this event. + * @var array */ - public function __construct($message = null) - { - if (!is_null($message)) { - $this->setMessage($message); - } - } + public $flags = array(); /** - * Sets the text message for this event. + * The message type. * - * @param string $message The text message to display. + * @var string */ - public function setMessage($message) + public $type; + + /** + * Constructor. + * + * @param mixed $data Message: either a string or an Exception or + * PEAR_Error object. + * @param string $type The event type. + * @param array $flags The flag array. + */ + public function __construct($data, $type = null, array $flags = array()) { - $this->_message = $message; + $this->flags = $flags; + + if ($data instanceof PEAR_Error) { + // DEPRECATED + if (($userinfo = $ob->getUserInfo()) && + is_array($userinfo)) { + $userinfo_elts = array(); + foreach ($userinfo as $userinfo_elt) { + if (is_scalar($userinfo_elt)) { + $userinfo_elts[] = $userinfo_elt; + } elseif (is_object($userinfo_elt)) { + if (is_callable(array($userinfo_elt, '__toString'))) { + $userinfo_elts[] = $userinfo_elt->__toString(); + } elseif (is_callable(array($userinfo_elt, 'getMessage'))) { + $userinfo_elts[] = $userinfo_elt->getMessage(); + } + } + } + + $this->message = $data->getMessage() . ' : ' . implode(', ', $userinfo_elts); + } else { + $this->message = $data->getMessage(); + } + + if (is_null($type)) { + $type = 'horde.error'; + } + } else { + // String or Exception + $this->message = strval($data); + if (is_null($type)) { + $type = is_string($data) ? 'horde.message' : 'horde.error'; + } + } + + $this->type = $type; } /** - * Gets the text message for this event. + * String representation of this object. * - * @return string The text message to display. + * @return string String representation. */ - public function getMessage() + public function __toString() { - return $this->_message; + return $this->message; } } diff --git a/framework/Notification/lib/Horde/Notification/Handler/Base.php b/framework/Notification/lib/Horde/Notification/Handler/Base.php index bc512a42f..dfb27a0c8 100644 --- a/framework/Notification/lib/Horde/Notification/Handler/Base.php +++ b/framework/Notification/lib/Horde/Notification/Handler/Base.php @@ -144,33 +144,17 @@ implements Horde_Notification_Handler_Interface */ public function push($event, $type = null, array $flags = array()) { - if (!($event instanceof Horde_Notification_Event) && - !($event instanceof PEAR_Error) && - !($event instanceof Exception)) { - /* Transparently create a Horde_Notification_Event object and - * set the message attribute. */ - $event = new Horde_Notification_Event($event); - } - - if (is_null($type)) { - if ($event instanceof PEAR_Error || $event instanceof Exception) { - $type = 'horde.error'; - } else { - $type = 'horde.message'; - } + if ($event instanceof Horde_Notification_Event) { + $event->flags = $flags; + $event->type = $type; + } else { + /* Transparently create a Horde_Notification_Event object. */ + $event = new Horde_Notification_Event($event, $type, $flags); } foreach ($this->_listeners as $listener) { - if ($listener->handles($type)) { - $this->_storage->push( - $listener->getName(), - array( - 'class' => get_class($event), - 'event' => serialize($event), - 'flags' => serialize($flags), - 'type' => $type - ) - ); + if ($listener->handles($event->type)) { + $this->_storage->push($listener->getName(), $event); } } } diff --git a/framework/Notification/lib/Horde/Notification/Listener.php b/framework/Notification/lib/Horde/Notification/Listener.php index 66a4c660a..17fbfc49e 100644 --- a/framework/Notification/lib/Horde/Notification/Listener.php +++ b/framework/Notification/lib/Horde/Notification/Listener.php @@ -61,86 +61,12 @@ abstract class Horde_Notification_Listener /** * Processes one message from the message stack. * - * @param array $message One message hash from the stack. - * @param array $options An array of options. + * @param Horde_Notification_Event $event One event object from the + * stack. + * @param array $options An array of options. * - * @return mixed TODO + * @return mixed The formatted message. */ - abstract public function getMessage($message, $options = array()); - - /** - * Unserialize an event from the message stack, checking to see if the - * appropriate class exists and kludging it into a base Notification_Event - * object if not. - */ - public function getEvent($message) - { - $ob = @unserialize($message['event']); - if (!is_callable(array($ob, 'getMessage'))) { - if (isset($ob->_message)) { - $ob = new Horde_Notification_Event($ob->_message); - } - } - - /* If we've failed to create a valid Notification_Event object - * (or subclass object) so far, return a PEAR_Error. */ - if (!is_callable(array($ob, 'getMessage'))) { - $ob = PEAR::raiseError('Unable to decode message event: ' . $message['event']); - } - - /* Specially handle PEAR_Error objects and add userinfo if - * it's there. */ - if (is_callable(array($ob, 'getUserInfo'))) { - $userinfo = $ob->getUserInfo(); - if ($userinfo) { - if (is_array($userinfo)) { - $userinfo_elts = array(); - foreach ($userinfo as $userinfo_elt) { - if (is_scalar($userinfo_elt)) { - $userinfo_elts[] = $userinfo_elt; - } elseif (is_object($userinfo_elt)) { - if (is_callable(array($userinfo_elt, '__toString'))) { - $userinfo_elts[] = $userinfo_elt->__toString(); - } elseif (is_callable(array($userinfo_elt, 'getMessage'))) { - $userinfo_elts[] = $userinfo_elt->getMessage(); - } - } - } - $userinfo = implode(', ', $userinfo_elts); - } - - $ob->_message = $ob->getMessage() . ' : ' . $userinfo; - } - } - - return $ob; - } - - /** - * Unserialize an array of event flags from the message stack. If this - * event has no flags, or the flags array could not be unserialized, an - * empty array is returned. - * - * @return array An array of flags. - */ - public function getFlags($message) - { - /* If this message doesn't have any flags, return an empty - * array. */ - if (empty($message['flags'])) { - return array(); - } - - /* Unserialize the flags array from the message. */ - $flags = @unserialize($message['flags']); - - /* If we couldn't unserialize the flags array, return an empty - * array. */ - if (!is_array($flags)) { - return array(); - } - - return $flags; - } + abstract public function getMessage($event, $options = array()); } diff --git a/framework/Notification/lib/Horde/Notification/Listener/Audio.php b/framework/Notification/lib/Horde/Notification/Listener/Audio.php index 364e1000a..3aebf9148 100644 --- a/framework/Notification/lib/Horde/Notification/Listener/Audio.php +++ b/framework/Notification/lib/Horde/Notification/Listener/Audio.php @@ -41,18 +41,16 @@ class Horde_Notification_Listener_Audio extends Horde_Notification_Listener } /** - * Outputs one message. + * Processes one message from the message stack. * - * @param array $message One message hash from the stack. - * @param array $options An array of options (not used). + * @param Horde_Notification_Event $event An event object. + * @param array $options An array of options (not used). * - * @return text The message representation. + * @return mixed The formatted message. */ - public function getMessage($message, $options = array()) + public function getMessage($event, $options = array()) { - $event = $this->getEvent($message); - return ''; + return ''; } } diff --git a/framework/Notification/lib/Horde/Notification/Listener/Javascript.php b/framework/Notification/lib/Horde/Notification/Listener/Javascript.php index 0f3625608..edf4a23fd 100644 --- a/framework/Notification/lib/Horde/Notification/Listener/Javascript.php +++ b/framework/Notification/lib/Horde/Notification/Listener/Javascript.php @@ -38,23 +38,32 @@ class Horde_Notification_Listener_Javascript extends Horde_Notification_Listener return; } - if (empty($options['noscript'])) { - echo '\n"; + if (!empty($js_text)) { + echo "//]]>\n"; + } + if (count($files)) { foreach ($files as $file) { echo '' . "\n"; @@ -64,17 +73,16 @@ class Horde_Notification_Listener_Javascript extends Horde_Notification_Listener } /** - * Outputs one message. + * Processes one message from the message stack. * - * @param array $message One message hash from the stack. - * @param array $options An array of options (not used). + * @param Horde_Notification_Event $event An event object. + * @param array $options An array of options (not used). * - * @return string The message text. + * @return mixed The formatted message. */ - public function getMessage($message, $options = array()) + public function getMessage($event, $options = array()) { - $event = $this->getEvent($message); - return $event->getMessage(); + return $event->message; } } diff --git a/framework/Notification/lib/Horde/Notification/Listener/Mobile.php b/framework/Notification/lib/Horde/Notification/Listener/Mobile.php index fbb2a2569..610dc9729 100644 --- a/framework/Notification/lib/Horde/Notification/Listener/Mobile.php +++ b/framework/Notification/lib/Horde/Notification/Listener/Mobile.php @@ -66,20 +66,21 @@ class Horde_Notification_Listener_Mobile extends Horde_Notification_Listener_Sta } /** - * Outputs one message. + * Processes one message from the message stack. * - * @param array $message One message hash from the stack. - * @param array $options An array of options (not used). + * @param Horde_Notification_Event $event An event object. + * @param array $options An array of options (not used). + * + * @return mixed The formatted message. */ - public function getMessage($message, $options = array()) + public function getMessage($event, $options = array()) { if (!$this->_mobile) { $p = new Horde_Notification_Listener_Status(); - return $p->getMessage($message, $options); + return $p->getMessage($event, $options); } - $event = $this->getEvent($message); - $this->_mobile->add(new Horde_Mobile_text($this->_handles[$message['type']] . ': ' . strip_tags($event->getMessage()))); + $this->_mobile->add(new Horde_Mobile_text($this->_handles[$event->type] . ': ' . strip_tags($event->message))); } } diff --git a/framework/Notification/lib/Horde/Notification/Listener/Status.php b/framework/Notification/lib/Horde/Notification/Listener/Status.php index 2f9e0bf0f..1a974a6e8 100644 --- a/framework/Notification/lib/Horde/Notification/Listener/Status.php +++ b/framework/Notification/lib/Horde/Notification/Listener/Status.php @@ -76,10 +76,10 @@ class Horde_Notification_Listener_Status extends Horde_Notification_Listener } /** - * Returns one message. + * Processes one message from the message stack. * - * @param array $message One message hash from the stack. - * @param array $options An array of options. + * @param Horde_Notification_Event $event An event object. + * @param array $options An array of options: *
      * 'data' - (boolean) If false, returns HTML code. If true, returns an
      *                    array of message information. DEFAULT: false
@@ -87,18 +87,15 @@ class Horde_Notification_Listener_Status extends Horde_Notification_Listener
      *
      * @return mixed  TODO
      */
-    public function getMessage($message, $options = array())
+    public function getMessage($event, $options = array())
     {
-        $event = $this->getEvent($message);
-        $flags = $this->getFlags($message);
-        $result = array('type' => $message['type']);
+        $result = array('type' => $event->type);
 
-        if ($event instanceof Horde_Notification_Event &&
-            $message['type'] == 'horde.alarm') {
+        if ($event->type == 'horde.alarm') {
             if (empty($options['data'])) {
-                $text = $this->_getAlarm($flags['alarm']);
+                $text = $this->_getAlarm($event->flags['alarm']);
             } else {
-                $result['alarm'] = $flags['alarm'];
+                $result['alarm'] = $event->flags['alarm'];
                 if (!empty($result['alarm']['params']['notify']['ajax'])) {
                     $result['alarm']['ajax'] = $result['alarm']['params']['notify']['ajax'];
                 } elseif (!empty($result['alarm']['params']['notify']['show'])) {
@@ -108,17 +105,17 @@ class Horde_Notification_Listener_Status extends Horde_Notification_Listener
                       $result['alarm']['methods']);
             }
         } else {
-            $text = $event->getMessage();
+            $text = $event->message;
             if (!empty($options['data'])) {
                 $result['message'] = $text;
             }
-            if (!in_array('content.raw', $this->getFlags($message))) {
+            if (!in_array('content.raw', $event->flags)) {
                 $text = htmlspecialchars($text, ENT_COMPAT, Horde_Nls::getCharset());
             }
         }
 
         return empty($options['data'])
-            ? '
  • ' . Horde::img($this->_handles[$message['type']][0], $this->_handles[$message['type']][1], '', '') . $text . '
  • ' + ? '
  • ' . Horde::img($this->_handles[$event->type][0], $this->_handles[$event->type][1], '', '') . $text . '
  • ' : $result; } diff --git a/framework/Notification/lib/Horde/Notification/Storage/Interface.php b/framework/Notification/lib/Horde/Notification/Storage/Interface.php index 30ef1b235..ffe7e43e7 100644 --- a/framework/Notification/lib/Horde/Notification/Storage/Interface.php +++ b/framework/Notification/lib/Horde/Notification/Storage/Interface.php @@ -61,10 +61,10 @@ interface Horde_Notification_Storage_Interface /** * Store a new event. * - * @param string $listener The event will be stored for this listener. - * @param array $event The event to store. - * - * @return NULL + * @param string $listener The event will be stored for + * this listener. + * @param Horde_Notification_Event $event The event to store. */ - public function push($listener, array $event); -} \ No newline at end of file + public function push($listener, $event); + +} diff --git a/framework/Notification/lib/Horde/Notification/Storage/Session.php b/framework/Notification/lib/Horde/Notification/Storage/Session.php index fc10d24a7..4dcdadc3a 100644 --- a/framework/Notification/lib/Horde/Notification/Storage/Session.php +++ b/framework/Notification/lib/Horde/Notification/Storage/Session.php @@ -96,13 +96,15 @@ implements Horde_Notification_Storage_Interface /** * Store a new event for the given listener stack. * - * @param string $listener The event will be stored for this listener. - * @param array $event The event to store. - * - * @return NULL + * @param string $listener The event will be stored for + * this listener. + * @param Notification_Event_Listener $event The event to store. */ - public function push($listener, array $event) + public function push($listener, $event) { + /* No need to serialize() ourselves - PHP's session handling does + * this automatically. */ $_SESSION[$this->_stack][$listener][] = $event; } -} \ No newline at end of file + +} diff --git a/framework/Notification/test/Horde/Notification/Class/Notification/EventTest.php b/framework/Notification/test/Horde/Notification/Class/Notification/EventTest.php index 43c87419f..9d394cd15 100644 --- a/framework/Notification/test/Horde/Notification/Class/Notification/EventTest.php +++ b/framework/Notification/test/Horde/Notification/Class/Notification/EventTest.php @@ -33,26 +33,19 @@ class Horde_Notification_Class_Notification_EventTest extends PHPUnit_Framework_ public function testMethodConstructHasPostconditionThatTheGivenMessageWasSavedIfItWasNotNull() { $event = new Horde_Notification_Event('test'); - $this->assertEquals('test', $event->getMessage()); - } - - public function testMethodSetmessageHasPostconditionThatTheGivenMessageWasSaved() - { - $event = new Horde_Notification_Event(); - $event->setMessage('test'); - $this->assertEquals('test', $event->getMessage()); + $this->assertEquals('test', $event->message); } public function testMethodGetmessageHasResultStringTheStoredMessage() { $event = new Horde_Notification_Event(); - $event->setMessage('test'); - $this->assertEquals('test', $event->getMessage()); + $event->message = test; + $this->assertEquals('test', $event->message); } public function testMethodGetmessageHasResultStringEmptyIfNoMessageWasStored() { $event = new Horde_Notification_Event(); - $this->assertEquals('', $event->getMessage()); + $this->assertEquals('', $event->message); } -} \ No newline at end of file +} diff --git a/framework/Notification/test/Horde/Notification/Class/Notification/Listener/AudioTest.php b/framework/Notification/test/Horde/Notification/Class/Notification/Listener/AudioTest.php index b629ad4ac..d5efc6538 100644 --- a/framework/Notification/test/Horde/Notification/Class/Notification/Listener/AudioTest.php +++ b/framework/Notification/test/Horde/Notification/Class/Notification/Listener/AudioTest.php @@ -46,12 +46,7 @@ class Horde_Notification_Class_Notification_Listener_AudioTest extends PHPUnit_E { $listener = new Horde_Notification_Listener_Audio(); $event = new Horde_Notification_Event('test'); - $messages = array( - array( - 'class' => 'Horde_Notification_Event', - 'event' => serialize($event) - ) - ); + $messages = array($event); $this->expectOutputString( '' ); diff --git a/framework/Notification/test/Horde/Notification/Class/Notification/Listener/JavascriptTest.php b/framework/Notification/test/Horde/Notification/Class/Notification/Listener/JavascriptTest.php index 0dbaa4696..2761bbafa 100644 --- a/framework/Notification/test/Horde/Notification/Class/Notification/Listener/JavascriptTest.php +++ b/framework/Notification/test/Horde/Notification/Class/Notification/Listener/JavascriptTest.php @@ -53,13 +53,7 @@ class Horde_Notification_Class_Notification_Listener_JavascriptTest extends PHPU { $listener = new Horde_Notification_Listener_Javascript(); $event = new Horde_Notification_Event('test'); - $messages = array( - array( - 'class' => 'Horde_Notification_Event', - 'event' => serialize($event), - 'type' => 'javascript' - ) - ); + $messages = array($event); $this->expectOutputString( '' . "\n" @@ -71,13 +65,7 @@ class Horde_Notification_Class_Notification_Listener_JavascriptTest extends PHPU { $listener = new Horde_Notification_Listener_Javascript(); $event = new Horde_Notification_Event('test'); - $messages = array( - array( - 'class' => 'Horde_Notification_Event', - 'event' => serialize($event), - 'type' => 'javascript' - ) - ); + $messages = array($event); $this->expectOutputString('test' . "\n"); $listener->notify($messages, array('noscript' => true)); } @@ -86,18 +74,12 @@ class Horde_Notification_Class_Notification_Listener_JavascriptTest extends PHPU { $listener = new Horde_Notification_Listener_Javascript(); $event = new Horde_Notification_Event('test'); - $messages = array( - array( - 'class' => 'Horde_Notification_Event', - 'event' => serialize($event), - 'type' => 'javascript-file' - ) - ); + $event->type = 'javascript-file'; + $messages = array($event); $this->expectOutputString( - '' . "\n" . '' . "\n" ); $listener->notify($messages); } + } diff --git a/framework/Notification/test/Horde/Notification/Class/Notification/Listener/MobileTest.php b/framework/Notification/test/Horde/Notification/Class/Notification/Listener/MobileTest.php index 951b8cd83..ce3a1523b 100644 --- a/framework/Notification/test/Horde/Notification/Class/Notification/Listener/MobileTest.php +++ b/framework/Notification/test/Horde/Notification/Class/Notification/Listener/MobileTest.php @@ -78,13 +78,7 @@ class Horde_Notification_Class_Notification_Listener_MobileTest extends PHPUnit_ $this->markTestIncomplete('This is untestable without mocking half of the Horde framework.'); $listener = new Horde_Notification_Listener_Mobile(); $event = new Horde_Notification_Event('test'); - $messages = array( - array( - 'class' => 'Horde_Notification_Event', - 'event' => serialize($event), - 'type' => 'horde.message' - ) - ); + $messages = array($event); $this->expectOutputString( '' ); @@ -112,13 +106,7 @@ class Horde_Notification_Class_Notification_Listener_MobileTest extends PHPUnit_ $listener = new Horde_Notification_Listener_Mobile(); $listener->setMobileObject($this->mobile); $event = new Horde_Notification_Event('test'); - $messages = array( - array( - 'class' => 'Horde_Notification_Event', - 'event' => serialize($event), - 'type' => 'horde.message', - ) - ); + $messages = array($event); $listener->notify($messages); } @@ -126,14 +114,9 @@ class Horde_Notification_Class_Notification_Listener_MobileTest extends PHPUnit_ { $listener = new Horde_Notification_Listener_Mobile(); $event = new Horde_Notification_Event('test'); - $flags = array('content.raw' => true); - $message = array( - 'class' => 'Horde_Notification_Event', - 'event' => serialize($event), - 'type' => 'horde.message', - 'flags' => serialize($flags) - ); - $listener->getMessage($message, array('data' => true)); + $event->flags = array('content.raw' => true); + $messages = array($event); + $listener->getMessage($messages, array('data' => true)); } } diff --git a/framework/Notification/test/Horde/Notification/Class/Notification/Listener/StatusTest.php b/framework/Notification/test/Horde/Notification/Class/Notification/Listener/StatusTest.php index f6095620c..53a3b381f 100644 --- a/framework/Notification/test/Horde/Notification/Class/Notification/Listener/StatusTest.php +++ b/framework/Notification/test/Horde/Notification/Class/Notification/Listener/StatusTest.php @@ -69,13 +69,7 @@ class Horde_Notification_Class_Notification_Listener_StatusTest extends PHPUnit_ $this->markTestIncomplete('This is untestable without mocking half of the Horde framework.'); $listener = new Horde_Notification_Listener_Status(); $event = new Horde_Notification_Event('test'); - $messages = array( - array( - 'class' => 'Horde_Notification_Event', - 'event' => serialize($event), - 'type' => 'horde.message' - ) - ); + $messages = array($event); $this->expectOutputString( '' ); @@ -86,15 +80,8 @@ class Horde_Notification_Class_Notification_Listener_StatusTest extends PHPUnit_ { $listener = new Horde_Notification_Listener_Status(); $event = new Horde_Notification_Event('test'); - $flags = array('content.raw' => true); - $messages = array( - array( - 'class' => 'Horde_Notification_Event', - 'event' => serialize($event), - 'type' => 'horde.message', - 'flags' => serialize($flags) - ) - ); + $event->flags = array('content.raw' => true); + $messages = array($event); $listener->notify($messages, array('store' => true)); $this->expectOutputString(''); } @@ -103,23 +90,11 @@ class Horde_Notification_Class_Notification_Listener_StatusTest extends PHPUnit_ { $listener = new Horde_Notification_Listener_Status(); $event = new Horde_Notification_Event('test'); - $flags = array('content.raw' => true); - $messages = array( - array( - 'class' => 'Horde_Notification_Event', - 'event' => serialize($event), - 'type' => 'horde.message', - 'flags' => serialize($flags) - ) - ); + $event->flags = array('content.raw' => true); + $messages = array($event); $listener->notify($messages, array('store' => true)); $this->assertEquals( - array( - array( - 'message' => 'test', - 'type' => 'horde.message' - ) - ), + $event, $listener->getStack() ); }