Guest User

PureMVC - Facade Class

a guest
Feb 7th, 2012
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 18.25 KB | None | 0 0
  1. /**
  2.  * PureMVC PHP Port using namespace by Tommy Pham <[email protected]>
  3.  *
  4.  * PureMVC - Copyright(c) 2006-08 Futurescale, Inc., Some rights reserved.
  5.  * Your reuse is governed by the Creative Commons Attribution 3.0 United States License
  6.  */
  7. namespace org\puremvc\php\patterns\facade
  8. {
  9.     use \org\puremvc\php\interfaces\IFacade;
  10.     //use \org\puremvc\php\interfaces\IController;
  11.     //use \org\puremvc\php\interfaces\IModel;
  12.     //use \org\puremvc\php\interfaces\IView;
  13.     use \org\puremvc\php\interfaces\IProxy;
  14.     use \org\puremvc\php\interfaces\IMediator;
  15.     use \org\puremvc\php\interfaces\INotification;
  16.     use \org\puremvc\php\core\controller\Controller;
  17.     use \org\puremvc\php\core\model\Model;
  18.     use \org\puremvc\php\core\view\View;
  19.     use \org\puremvc\php\patterns\observer\Notification;
  20.     /**
  21.      * A base Singleton <code>IFacade</code> implementation.
  22.      *
  23.      * <P>
  24.      * In PureMVC, the <code>Facade</code> class assumes these
  25.      * responsibilities:
  26.      * <UL>
  27.      * <LI>Initializing the <code>Model</code>, <code>View</code>
  28.      * and <code>Controller</code> Singletons.</LI>
  29.      * <LI>Providing all the methods defined by the <code>IModel,
  30.      * IView, & IController</code> interfaces.</LI>
  31.      * <LI>Providing the ability to override the specific <code>Model</code>,
  32.      * <code>View</code> and <code>Controller</code> Singletons created.</LI>
  33.      * <LI>Providing a single point of contact to the application for
  34.      * registering <code>Commands</code> and notifying <code>Observers</code></LI>
  35.      * </UL>
  36.      * <P>
  37.      * Example usage:
  38.      * <listing>
  39.      *  use \org\puremvc\php\patterns\facade\&lowast;;
  40.      *
  41.      *  import com.me.myapp.model.~~;
  42.      *  import com.me.myapp.view.~~;
  43.      *  import com.me.myapp.controller.~~;
  44.      *
  45.      *  public class MyFacade extends Facade
  46.      *  {
  47.      *      // Notification constants. The Facade is the ideal
  48.      *      // location for these constants, since any part
  49.      *      // of the application participating in PureMVC
  50.      *      // Observer Notification will know the Facade.
  51.      *      public static const GO_COMMAND:String = "go";
  52.      *
  53.      *      // Override Singleton Factory method
  54.      *      public static function getInstance() : MyFacade {
  55.      *          if (instance == null) instance = new MyFacade();
  56.      *          return instance as MyFacade;
  57.      *      }
  58.      *
  59.      *      // optional initialization hook for Facade
  60.      *      override public function initializeFacade() : void {
  61.      *          super.initializeFacade();
  62.      *          // do any special subclass initialization here
  63.      *      }
  64.      *
  65.      *      // optional initialization hook for Controller
  66.      *      override public function initializeController() : void {
  67.      *          // call super to use the PureMVC Controller Singleton.
  68.      *          super.initializeController();
  69.      *
  70.      *          // Otherwise, if you're implmenting your own
  71.      *          // IController, then instead do:
  72.      *          // if ( controller != null ) return;
  73.      *          // controller = MyAppController.getInstance();
  74.      *
  75.      *          // do any special subclass initialization here
  76.      *          // such as registering Commands
  77.      *          registerCommand( GO_COMMAND, com.me.myapp.controller.GoCommand )
  78.      *      }
  79.      *
  80.      *      // optional initialization hook for Model
  81.      *      override public function initializeModel() : void {
  82.      *          // call super to use the PureMVC Model Singleton.
  83.      *          super.initializeModel();
  84.      *
  85.      *          // Otherwise, if you're implmenting your own
  86.      *          // IModel, then instead do:
  87.      *          // if ( model != null ) return;
  88.      *          // model = MyAppModel.getInstance();
  89.      *
  90.      *          // do any special subclass initialization here
  91.      *          // such as creating and registering Model proxys
  92.      *          // that don't require a facade reference at
  93.      *          // construction time, such as fixed type lists
  94.      *          // that never need to send Notifications.
  95.      *          regsiterProxy( new USStateNamesProxy() );
  96.      *
  97.      *          // CAREFUL: Can't reference Facade instance in constructor
  98.      *          // of new Proxys from here, since this step is part of
  99.      *          // Facade construction!  Usually, Proxys needing to send
  100.      *          // notifications are registered elsewhere in the app
  101.      *          // for this reason.
  102.      *      }
  103.      *
  104.      *      // optional initialization hook for View
  105.      *      override public function initializeView() : void {
  106.      *          // call super to use the PureMVC View Singleton.
  107.      *          super.initializeView();
  108.      *
  109.      *          // Otherwise, if you're implmenting your own
  110.      *          // IView, then instead do:
  111.      *          // if ( view != null ) return;
  112.      *          // view = MyAppView.getInstance();
  113.      *
  114.      *          // do any special subclass initialization here
  115.      *          // such as creating and registering Mediators
  116.      *          // that do not need a Facade reference at construction
  117.      *          // time.
  118.      *          registerMediator( new LoginMediator() );
  119.      *
  120.      *          // CAREFUL: Can't reference Facade instance in constructor
  121.      *          // of new Mediators from here, since this is a step
  122.      *          // in Facade construction! Usually, all Mediators need
  123.      *          // receive notifications, and are registered elsewhere in
  124.      *          // the app for this reason.
  125.      *      }
  126.      *  }
  127.      * </listing>
  128.      *
  129.      * @see org\puremvc\php\core\model\Model Model
  130.      * @see org\puremvc\php\core\view\View View
  131.      * @see org\puremvc\php\core\controller\Controller Controller
  132.      * @see org\puremvc\php\patterns\observer\Notification Notification
  133.      * @see org\puremvc\php\patterns\mediator\Mediator Mediator
  134.      * @see org\puremvc\php\patterns\proxy\Proxy Proxy
  135.      * @see org\puremvc\php\patterns\command\SimpleCommand SimpleCommand
  136.      * @see org\puremvc\php\patterns\command\MacroCommand MacroCommand
  137.      */
  138.     class Facade implements IFacade
  139.     {
  140.         // Private references to Model, View and Controller
  141.         protected $controller; // IController
  142.         protected $model; // IModel
  143.         protected $view; // IView
  144.  
  145.         // The Singleton Facade instance.
  146.         protected static $instance; // IFacade
  147.  
  148.         // Message Constants
  149.         const SINGLETON_MSG = 'Facade Singleton already constructed!';
  150.         /**
  151.          * Constructor.
  152.          *
  153.          * <P>
  154.          * This <code>IFacade</code> implementation is a Singleton,
  155.          * so you should not call the constructor
  156.          * directly, but instead call the static Singleton
  157.          * Factory method <code>Facade.getInstance()</code>
  158.          *
  159.          * @throws Error Error if Singleton instance has already been constructed
  160.          *
  161.          */
  162.         public function __construct()
  163.         {
  164.             //if(self::$instance instanceof IFacade)
  165.             if(self::$instance != null)
  166.                 throw new \Exception(SINGLETON_MSG);
  167.             self::$instance = $this;
  168.             $this->initializeFacade();
  169.         }
  170.         /**
  171.          * Initialize the Singleton <code>Facade</code> instance.
  172.          *
  173.          * <P>
  174.          * Called automatically by the constructor. Override in your
  175.          * subclass to do any subclass specific initializations. Be
  176.          * sure to call <code>super.initializeFacade()</code>, though.</P>
  177.          */
  178.         //      protected function initializeFacade(  ):void {
  179.         //          initializeModel();
  180.         //          initializeController();
  181.         //          initializeView();
  182.         //      }
  183.         protected function initializeFacade()
  184.         {
  185.             $this->initializeModel();
  186.             $this->initializeController();
  187.             $this->initializeView();
  188.         }
  189.         /**
  190.          * Facade Singleton Factory method
  191.          *
  192.          * @return the Singleton instance of the Facade
  193.          */
  194.         //      public static function getInstance():IFacade {
  195.         //          if (instance == null) instance = new Facade( );
  196.         //          return instance;
  197.         //      }
  198.         public static function getInstance()
  199.         {
  200.             //if(!(self::$instance instanceof IFacade))
  201.             if(self::$instance == null)
  202.                 new Facade();
  203.             return self::$instance;
  204.         }
  205.         /**
  206.          * Initialize the <code>Controller</code>.
  207.          *
  208.          * <P>
  209.          * Called by the <code>initializeFacade</code> method.
  210.          * Override this method in your subclass of <code>Facade</code>
  211.          * if one or both of the following are true:
  212.          * <UL>
  213.          * <LI> You wish to initialize a different <code>IController</code>.</LI>
  214.          * <LI> You have <code>Commands</code> to register with the <code>Controller</code> at startup.</code>. </LI>
  215.          * </UL>
  216.          * If you don't want to initialize a different <code>IController</code>,
  217.          * call <code>super.initializeController()</code> at the beginning of your
  218.          * method, then register <code>Command</code>s.
  219.          * </P>
  220.          */
  221.         //      protected function initializeController() : void
  222.         //      {
  223.         //          if(controller != null) return;
  224.         //          controller = Controller.getInstance();
  225.         //      }
  226.         protected function initializeController()
  227.         {
  228.             //if(!($this->controller instanceof IController))
  229.             $this->controller = Controller::getInstance();
  230.         }
  231.         /**
  232.          * Initialize the <code>Model</code>.
  233.          *
  234.          * <P>
  235.          * Called by the <code>initializeFacade</code> method.
  236.          * Override this method in your subclass of <code>Facade</code>
  237.          * if one or both of the following are true:
  238.          * <UL>
  239.          * <LI> You wish to initialize a different <code>IModel</code>.</LI>
  240.          * <LI> You have <code>Proxy</code>s to register with the Model that do not
  241.          * retrieve a reference to the Facade at construction time.</code></LI>
  242.          * </UL>
  243.          * If you don't want to initialize a different <code>IModel</code>,
  244.          * call <code>super.initializeModel()</code> at the beginning of your
  245.          * method, then register <code>Proxy</code>s.
  246.          * <P>
  247.          * Note: This method is <i>rarely</i> overridden; in practice you are more
  248.          * likely to use a <code>Command</code> to create and register <code>Proxy</code>s
  249.          * with the <code>Model</code>, since <code>Proxy</code>s with mutable data will likely
  250.          * need to send <code>INotification</code>s and thus will likely want to fetch a reference to
  251.          * the <code>Facade</code> during their construction.
  252.          * </P>
  253.          */
  254.         //      protected function initializeModel() : void
  255.         //      {
  256.         //          if(model != null)
  257.         //              return;
  258.         //          model = Model.getInstance();
  259.         //      }
  260.         protected function initializeModel()
  261.         {
  262.             //if(!($this->model instanceof IModel))
  263.             $this->model = Model::getInstance();
  264.         }
  265.         /**
  266.          * Initialize the <code>View</code>.
  267.          *
  268.          * <P>
  269.          * Called by the <code>initializeFacade</code> method.
  270.          * Override this method in your subclass of <code>Facade</code>
  271.          * if one or both of the following are true:
  272.          * <UL>
  273.          * <LI> You wish to initialize a different <code>IView</code>.</LI>
  274.          * <LI> You have <code>Observers</code> to register with the <code>View</code></LI>
  275.          * </UL>
  276.          * If you don't want to initialize a different <code>IView</code>,
  277.          * call <code>super.initializeView()</code> at the beginning of your
  278.          * method, then register <code>IMediator</code> instances.
  279.          * <P>
  280.          * Note: This method is <i>rarely</i> overridden; in practice you are more
  281.          * likely to use a <code>Command</code> to create and register <code>Mediator</code>s
  282.          * with the <code>View</code>, since <code>IMediator</code> instances will need to send
  283.          * <code>INotification</code>s and thus will likely want to fetch a reference
  284.          * to the <code>Facade</code> during their construction.
  285.          * </P>
  286.          */
  287.         //      protected function initializeView() : void
  288.         //      {
  289.         //          if(view != null)
  290.         //              return;
  291.         //          view = View.getInstance();
  292.         //      }
  293.         protected function initializeView()
  294.         {
  295.             //if(!($this->view instanceof IView))
  296.             $this->view = View::getInstance();
  297.         }
  298.         /**
  299.          * Register an <code>ICommand</code> with the <code>Controller</code> by Notification name.
  300.          *
  301.          * @param $notificationName the name of the <code>INotification</code> to associate the <code>ICommand</code> with
  302.          * @param $commandClassRef a reference to the Class of the <code>ICommand</code>
  303.          */
  304.         //      public function registerCommand(notificationName : string, commandClassRef : class ) : void
  305.         //      {
  306.         //          controller.registerCommand(notificationName, commandClassRef);
  307.         //      }
  308.         public function registerCommand($notificationName, $commandClassRef)
  309.         {
  310.             $this->controller->registerCommand($notificationName, $commandClassRef);
  311.         }
  312.         /**
  313.          * Remove a previously registered <code>ICommand</code> to <code>INotification</code> mapping from the Controller.
  314.          *
  315.          * @param $notificationName the name of the <code>INotification</code> to remove the <code>ICommand</code> mapping for
  316.          */
  317.         //      public function removeCommand(notificationName : string ) : void
  318.         //      {
  319.         //          controller.removeCommand(notificationName);
  320.         //      }
  321.         public function removeCommand($notificationName)
  322.         {
  323.             $this->controller->removeCommand($notificationName);
  324.         }
  325.         /**
  326.          * Check if a Command is registered for a given Notification
  327.          *
  328.          * @param $notificationName
  329.          * @return whether a Command is currently registered for the given <code>notificationName</code>.
  330.          */
  331.         //      public function hasCommand(notificationName : string ) : boolean
  332.         //      {
  333.         //          return controller.hasCommand(notificationName);
  334.         //      }
  335.         public function hasCommand($notificationName)
  336.         {
  337.             return $this->controller->hasCommand($notificationName);
  338.         }
  339.         /**
  340.          * Register an <code>IProxy</code> with the <code>Model</code> by name.
  341.          *
  342.          * @param $proxyName the name of the <code>IProxy</code>.
  343.          * @param $proxy the <code>IProxy</code> instance to be registered with the <code>Model</code>.
  344.          */
  345.         //      public function registerProxy(proxy : IProxy) : void
  346.         //      {
  347.         //          model.registerProxy(proxy);
  348.         //      }
  349.         public function registerProxy(IProxy $proxy)
  350.         {
  351.             $this->model->registerProxy($proxy);
  352.         }
  353.         /**
  354.          * Retrieve an <code>IProxy</code> from the <code>Model</code> by name.
  355.          *
  356.          * @param $proxyName the name of the proxy to be retrieved.
  357.          * @return the <code>IProxy</code> instance previously registered with the given <code>$proxyName</code>.
  358.          */
  359.         //      public function retrieveProxy(proxyName : string ) : IProxy
  360.         //      {
  361.         //          return model.retrieveProxy(proxyName);
  362.         //      }
  363.         public function retrieveProxy($proxyName)
  364.         {
  365.             return $this->model->retrieveProxy($proxyName);
  366.         }
  367.         /**
  368.          * Remove an <code>IProxy</code> from the <code>Model</code> by name.
  369.          *
  370.          * @param $proxyName the <code>IProxy</code> to remove from the <code>Model</code>.
  371.          * @return the <code>IProxy</code> that was removed from the <code>Model</code>
  372.          */
  373.         //      public function removeProxy(proxyName : string ) : IProxy
  374.         //      {
  375.         //          var proxy : IProxy;
  376.         //          if(model != null)
  377.         //              proxy = model.removeProxy(proxyName);
  378.         //          return proxy
  379.         //      }
  380.         public function removeProxy($proxyName)
  381.         {
  382.             //$proxy = null;
  383.             //if($this->model instanceof IModel)
  384.             //  $proxy = $this->model->removeProxy($proxyName);
  385.             //return $proxy;
  386.             return $this->model->removeProxy($proxyName);
  387.         }
  388.         /**
  389.          * Check if a Proxy is registered
  390.          *
  391.          * @param $proxyName
  392.          * @return whether a Proxy is currently registered with the given <code>proxyName</code>.
  393.          */
  394.         //      public function hasProxy(proxyName : string ) : boolean
  395.         //      {
  396.         //          return model.hasProxy(proxyName);
  397.         //      }
  398.         public function hasProxy($proxyName)
  399.         {
  400.             return $this->model->hasProxy($proxyName);
  401.         }
  402.         /**
  403.          * Register a <code>IMediator</code> with the <code>View</code>.
  404.          *
  405.          * @param $mediatorName the name to associate with this <code>IMediator</code>
  406.          * @param $mediator a reference to the <code>IMediator</code>
  407.          */
  408.         //      public function registerMediator(IMediator $mediator )
  409.         //      {
  410.         //          if($this->view instanceof View)
  411.         //              $this->view->registerMediator($mediator);
  412.         //      }
  413.         public function registerMediator(IMediator $mediator)
  414.         {
  415.             //if($this->view instanceof IView)
  416.             $this->view->registerMediator($mediator);
  417.         }
  418.         /**
  419.          * Retrieve an <code>IMediator</code> from the <code>View</code>.
  420.          *
  421.          * @param $mediatorName
  422.          * @return the <code>IMediator</code> previously registered with the given <code>$mediatorName</code>.
  423.          */
  424.         //      public function retrieveMediator(mediatorName : string ) : IMediator
  425.         //      {
  426.         //          return view.retrieveMediator(mediatorName) as IMediator;
  427.         //      }
  428.         public function retrieveMediator($mediatorName)
  429.         {
  430.             return $this->view->retrieveMediator($mediatorName);
  431.         }
  432.         /**
  433.          * Remove an <code>IMediator</code> from the <code>View</code>.
  434.          *
  435.          * @param $mediatorName name of the <code>IMediator</code> to be removed.
  436.          * @return the <code>IMediator</code> that was removed from the <code>View</code>
  437.          */
  438.         //      public function removeMediator(mediatorName : string ) : IMediator
  439.         //      {
  440.         //          var mediator : IMediator;
  441.         //          if(view != null)
  442.         //              mediator = view.removeMediator(mediatorName);
  443.         //          return mediator;
  444.         //      }
  445.         public function removeMediator($mediatorName)
  446.         {
  447.             //$mediator = null;
  448.             //if($this->view instanceof IView)
  449.             //  $mediator = $this->view->removeMediator($mediatorName);
  450.             //return $mediator;
  451.             return $this->view->removeMediator($mediatorName);
  452.         }
  453.         /**
  454.          * Check if a Mediator is registered or not
  455.          *
  456.          * @param $mediatorName
  457.          * @return whether a Mediator is registered with the given <code>mediatorName</code>.
  458.          */
  459.         //      public function hasMediator(mediatorName : string ) : boolean
  460.         //      {
  461.         //          return view.hasMediator(mediatorName);
  462.         //      }
  463.         public function hasMediator($mediatorName)
  464.         {
  465.             return $this->view->hasMediator($mediatorName);
  466.         }
  467.         /**
  468.          * Create and send an <code>INotification</code>.
  469.          *
  470.          * <P>
  471.          * Keeps us from having to construct new notification
  472.          * instances in our implementation code.
  473.          * @param $notificationName the name of the notiification to send
  474.          * @param $body the body of the notification (optional)
  475.          * @param $type the type of the notification (optional)
  476.          */
  477.         //      public function sendNotification(notificationName : string, body : object = null, type : string = null) : void
  478.         //      {
  479.         //          notifyObservers(new Notification(notificationName, body, type));
  480.         //      }
  481.         public function sendNotification($notificationName, $body = null, $type = null)
  482.         {
  483.             $this->notifyObservers(new Notification($notificationName, $body, $type));
  484.         }
  485.         /**
  486.          * Notify <code>Observer</code>s.
  487.          * <P>
  488.          * This method is left public mostly for backward
  489.          * compatibility, and to allow you to send custom
  490.          * notification classes using the facade.</P>
  491.          *<P>
  492.          * Usually you should just call sendNotification
  493.          * and pass the parameters, never having to
  494.          * construct the notification yourself.</P>
  495.          *
  496.          * @param $notification the <code>INotification</code> to have the <code>View</code> notify <code>Observers</code> of.
  497.          */
  498.         //      public function notifyObservers(notification : INotification) : void
  499.         //      {
  500.         //          if(view != null)
  501.         //              view.notifyObservers(notification);
  502.         //      }
  503.         public function notifyObservers(INotification $notification)
  504.         {
  505.             //if($this->view instanceof IView)
  506.             $this->view->notifyObservers($notification);
  507.         }
  508.     }
  509. }
Advertisement
Add Comment
Please, Sign In to add comment