org-puremvc-php-multicore
[ class tree: org-puremvc-php-multicore ] [ index: org-puremvc-php-multicore ] [ all elements ]

Source for file Notifier.php

Documentation is available at Notifier.php

  1. <?php
  2. /**
  3.  * PureMVC Multicore Port to PHP
  4.  *
  5.  * Partly based on PureMVC Port to PHP by:
  6.  * - Omar Gonzalez <omar@almerblank.com>
  7.  * - and Hasan Otuome <hasan@almerblank.com>
  8.  *
  9.  * Created on Jully 24, 2009
  10.  *
  11.  * @version 1.0
  12.  * @author Michel Chouinard <michel.chouinard@gmail.com>
  13.  * @copyright PureMVC - Copyright(c) 2006-2008 Futurescale, Inc., Some rights reserved.
  14.  * @license http://creativecommons.org/licenses/by/3.0/ Creative Commons Attribution 3.0 Unported License
  15.  * @package org.puremvc.php.multicore
  16.  *
  17.  */
  18. /**
  19.  *
  20.  */
  21.  
  22. require_once 'org/puremvc/php/multicore/interfaces/INotifier.php';
  23. require_once 'org/puremvc/php/multicore/patterns/facade/Facade.php';
  24.  
  25. /**
  26.  * A Base <code>INotifier</code> implementation.
  27.  *
  28.  * <b>MacroCommand, SimpleCommand, Mediator</b> and <b>Proxy</b>
  29.  * all have a need to send <b>Notifications</b>.
  30.  *
  31.  * The <b>INotifier</b> interface provides a common method called
  32.  * <b>sendNotification</b> that relieves implementation code of
  33.  * the necessity to actually construct <b>Notifications</b>.
  34.  *
  35.  * The <b>Notifier</b> class, which all of the above mentioned classes
  36.  * extend, also provides an initialized reference to the <b>Facade</b>
  37.  * Singleton, which is required for the convienience method
  38.  * for sending <b>Notifications</b>, but also eases implementation as these
  39.  * classes have frequent <b>Facade</b> interactions and usually require
  40.  * access to the facade anyway.
  41.  *
  42.  * <b>NOTE:</b><br>
  43.  * In the MultiCore version of the framework, there is one caveat to
  44.  * notifiers, they cannot send notifications or reach the facade until they
  45.  * have a valid multitonKey.
  46.  *
  47.  * The multitonKey is set:
  48.  *  - on a Command when it is executed by the Controller
  49.  *  - on a Mediator is registered with the View
  50.  *  - on a Proxy is registered with the Model.
  51.  *
  52.  * @see Proxy
  53.         org\puremvc\php\multicore\patterns\proxy\Proxy.php
  54.  * @see Facade
  55.         org\puremvc\php\multicore\patterns\facade\Facade.php
  56.  * @see Mediator
  57.         org\puremvc\php\multicore\patterns\mediator\Mediator.php
  58.  * @see MacroCommand
  59.         org\puremvc\php\multicore\patterns\command\MacroCommand.php
  60.  * @see SimpleCommand
  61.         org\puremvc\php\multicore\patterns\command\SimpleCommand.php
  62.  * @package org.puremvc.php.multicore
  63.  */
  64. class Notifier implements INotifier
  65. {
  66.     /**
  67.      * Define the message content for the inexistant instance exception
  68.      * @var string 
  69.      */
  70.     const MULTITON_MSG "multitonKey for this Notifier not yet initialized!";
  71.  
  72.     /**
  73.      * The Multiton Key for this Core
  74.      * @var string 
  75.      */
  76.     protected $multitonKey = null;
  77.  
  78.     public function __construct()
  79.     {
  80.     }
  81.  
  82.     /**
  83.      * Send a <b>INotification</b>.
  84.      *
  85.      * Convenience method to prevent having to construct new
  86.      * notification instances in our implementation code.
  87.      *
  88.      * @param string $notificationName The name of the notification to send.
  89.      * @param mixed $body The body of the notification (optional).
  90.      * @param string $type The type of the notification (optional).
  91.      * @return void 
  92.      */
  93.     public function sendNotification$notificationName$body=null$type=null )
  94.     {
  95.         if !is_null$this->facade() ) )
  96.         {
  97.             $this->facade()->sendNotification$notificationName$body$type );
  98.         }
  99.     }
  100.  
  101.     /**
  102.      * Initialize this <b>INotifier</b> instance.
  103.      *
  104.      * This is how a Notifier gets its multitonKey.
  105.      * Calls to sendNotification or to access the
  106.      * facade will fail until after this method
  107.      * has been called.
  108.      *
  109.      * Mediators, Commands or Proxies may override
  110.      * this method in order to send notifications
  111.      * or access the Multiton Facade instance as
  112.      * soon as possible. They CANNOT access the facade
  113.      * in their constructors, since this method will not
  114.      * yet have been called.
  115.      *
  116.      * @param string $key The multitonKey for this <b>INotifier</b> to use.
  117.      * @return void 
  118.      */
  119.     public function initializeNotifier$key )
  120.     {
  121.         $this->multitonKey = $key;
  122.     }
  123.  
  124.     /**
  125.      * Return the Multiton Facade instance
  126.      *
  127.      * @throws Exception if multitonKey for this Notifier is not yet initialized.
  128.      * @return Facade The Facade instance for this Notifier multitonKey.
  129.      */
  130.     protected function facade()
  131.     {
  132.         if !isset$this->multitonKey ) )
  133.         {
  134.             throw new Exceptionself::MULTITON_MSG );
  135.         }
  136.         return Facade::getInstance$this->multitonKey );
  137.     }
  138.  
  139. }

Documentation generated on Mon, 03 Aug 2009 04:58:01 +0000 by phpDocumentor 1.4.2