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

Source for file Facade.php

Documentation is available at Facade.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/core/Controller.php';
  23. require_once 'org/puremvc/php/multicore/core/Model.php';
  24. require_once 'org/puremvc/php/multicore/core/View.php';
  25. require_once 'org/puremvc/php/multicore/interfaces/IFacade.php';
  26. require_once 'org/puremvc/php/multicore/interfaces/IMediator.php';
  27. require_once 'org/puremvc/php/multicore/interfaces/INotification.php';
  28. require_once 'org/puremvc/php/multicore/patterns/observer/Notification.php';
  29.  
  30. /**
  31.  * A base Multiton <b>IFacade</b> implementation.
  32.  *
  33.  * The Facade Pattern suggests providing a single
  34.  * class to act as a central point of communication
  35.  * for a subsystem.
  36.  *
  37.  * In PureMVC, the Facade acts as an interface between
  38.  * the core MVC actors ( Model, View, Controller) and
  39.  * the rest of your application.
  40.  *
  41.  * @package org.puremvc.php.multicore
  42.  * @see Model
  43.         org\puremvc\php\multicore\core\Model.php
  44.  * @see View
  45.         org\puremvc\php\multicore\core\View.php
  46.  * @see Controller
  47.         org\puremvc\php\multicore\core\Controller.php
  48.  */
  49. class Facade implements IFacade
  50. {
  51.     /**
  52.      * Define the message content for the duplicate instance exception
  53.      * @var string 
  54.      */
  55.     const MULTITON_MSG "Facade instance for this Multiton key already constructed!";
  56.  
  57.     /**
  58.      * The controller instance for this Core
  59.      * @var Controller 
  60.      */
  61.     protected $controller;
  62.  
  63.     /**
  64.      * The model instance for this Core
  65.      * @var Model 
  66.      */
  67.     protected $model;
  68.  
  69.     /**
  70.      * The view instance for this Core
  71.      * @var View 
  72.      */
  73.     protected $view;
  74.  
  75.     /**
  76.      * The Multiton Key for this Core
  77.      * @var string 
  78.      */
  79.     protected $multitonKey;
  80.  
  81.     /**
  82.      * The Multiton Facade instances stack.
  83.      * @var array 
  84.      */
  85.     protected static $instanceMap array();
  86.  
  87.     /**
  88.      * Instance constructor
  89.      *
  90.      * This <b>IFacade</b> implementation is a Multiton,
  91.      * so you should not call the constructor
  92.      * directly, but instead call the static Factory method,
  93.      * passing the unique key for this instance
  94.      *
  95.      * <code>
  96.      * Facade::getInstance( 'multitonKey' )
  97.      * </code>
  98.      *
  99.      * @param string $key Unique key for this instance.
  100.      * @throws Exception if instance for this key has already been constructed.
  101.      * @return IFacade Instance for this key
  102.      */
  103.     protected function __construct$key )
  104.     {
  105.         if (isset(self::$instanceMap$key ]))
  106.         {
  107.             throw new Exception(MULTITON_MSG);
  108.         }
  109.         $this->initializeNotifier$key );
  110.         self::$instanceMap$this->multitonKey $this;
  111.         $this->initializeFacade();
  112.     }
  113.  
  114.     /**
  115.      * Initialize the <b>Facade</b> instance.
  116.      *
  117.      * Called automatically by the constructor. Override in your
  118.      * subclass to do any subclass specific initializations. Be
  119.      * sure to call <samp>parent::initializeFacade()</samp>, though.
  120.      *
  121.      * @return void 
  122.      */
  123.     protected function initializeFacade(  )
  124.     {
  125.         $this->initializeModel();
  126.         $this->initializeController();
  127.         $this->initializeView();
  128.     }
  129.  
  130.     /**
  131.      * <b>Facade</b> Multiton Factory method
  132.      *
  133.      * This <b>IFacade</b> implementation is a Multiton,
  134.      * so you MUST not call the constructor
  135.      * directly, but instead call this static Factory method,
  136.      * passing the unique key for this instance
  137.      *
  138.      * @param string $key Unique key for this instance.
  139.      * @return IFacade Instance for this key
  140.      */
  141.     public static function getInstance$key )
  142.     {
  143.         if (!issetself::$instanceMap$key ) )
  144.         {
  145.             self::$instanceMap$key new Facade$key );
  146.         }
  147.         return self::$instanceMap$key ];
  148.     }
  149.  
  150.     /**
  151.      * Initialize the <b>Controller</b>.
  152.      *
  153.      * Called by the <b>initializeFacade()</b> method.
  154.      *
  155.      * Override this method in your subclass of <b>Facade</b> if
  156.      * one or both of the following are true:
  157.      *
  158.      * - You wish to initialize a different <b>Controller</b>.
  159.      * - You have <b>Commands</b> to register with the <b>Controller</b> at startup.
  160.      *
  161.      * If you don't want to initialize a different <b>Controller</b>,
  162.      * call <samp>parent::initializeController()</samp> at the beginning of your
  163.      * method, then register Commands.
  164.      *
  165.      * @return void 
  166.      */
  167.     protected function initializeController)
  168.     {
  169.         if isset$this->controller ) ) return;
  170.  
  171.         $this->controller = Controller::getInstance$this->multitonKey );
  172.     }
  173.  
  174.     /**
  175.      * Initialize the <b>Model</b>.
  176.      *
  177.      * Called by the <b>initializeFacade()</b> method.
  178.      *
  179.      * Override this method in your subclass of <b>Facade</b> if
  180.      * one or both of the following are true:
  181.      *
  182.      * - You wish to initialize a different <b>Model</b>.
  183.      * - You have <b>Proxys</b> to register with the <b>Model</b> that do not<br>
  184.      *   retrieve a reference to the <b>Facade</b> at construction time.
  185.      *
  186.      * If you don't want to initialize a different <b>Model</b>,
  187.      * call <b>parent::initializeModel()</b> at the beginning of your
  188.      * method, then register <b>Proxys</b>.
  189.      *
  190.      * <b>Note:</b><br>
  191.      * This method is <i>rarely</i> overridden; in practice you are more
  192.      * likely to use a <b>Command</b> to create and register <b>Proxys</b>
  193.      * with the <b>Model</b>, since <b>Proxys</b> with mutable data will likely
  194.      * need to send <b>Notifications</b> and thus will likely want to fetch a reference to
  195.      * the <b>Facade</b> during their construction.
  196.      *
  197.      * @return void 
  198.      */
  199.     protected function initializeModel)
  200.     {
  201.         if isset$this->model ) ) return;
  202.  
  203.         $this->model = Model::getInstance$this->multitonKey );
  204.     }
  205.  
  206.  
  207.     /**
  208.      * Initialize the <b>View</b>.
  209.      *
  210.      * Called by the <b>initializeFacade()</b> method.
  211.      *
  212.      * Override this method in your subclass of <b>Facade</b>
  213.      * if one or both of the following are true:
  214.      *
  215.      * - You wish to initialize a different <b>IView</b> implementation.
  216.      * - You have <b>Observer</b>s to register with the <b>View</b>
  217.      *
  218.      * If you don't want to initialize a different <code>IView</code>,
  219.      * call <code>super.initializeView()</code> at the beginning of your
  220.      * method, then register <code>IMediator</code> instances.
  221.      *
  222.      * <b>Note:</b><br>
  223.      * This method is <i>rarely</i> overridden; in practice you are more
  224.      * likely to use a Command to create and register Mediators
  225.      * with the View, since IMediator instances will need to send
  226.      * INotifications and thus will likely want to fetch a reference
  227.      * to the Facade during their construction.
  228.      *
  229.      * @return void 
  230.      */
  231.     protected function initializeView)
  232.     {
  233.         if isset$this->view ) ) return;
  234.  
  235.         $this->view = View::getInstance$this->multitonKey );
  236.     }
  237.  
  238.     /**
  239.      * Register Proxy
  240.      *
  241.      * Register an <b>IProxy</b> with the <b>Model</b>.
  242.      *
  243.      * @param IProxy $proxy The <b>IProxy</b> to be registered with the <b>Model</b>.
  244.      * @return void 
  245.      */
  246.     public function registerProxy IProxy $proxy )
  247.     {
  248.         ifisset$this->model ) )
  249.         {
  250.             $this->model->registerProxy$proxy );
  251.         }
  252.     }
  253.  
  254.     /**
  255.      * Retreive Proxy
  256.      *
  257.      * Retrieve a previously registered <b>IProxy</b> from the <b>Model</b> by name.
  258.      *
  259.      * @param string $proxyName Name of the <b>IProxy</b> instance to be retrieved.
  260.      * @return IProxy The <b>IProxy</b> previously regisetered by <var>proxyName</var> with the <b>Model</b>.
  261.      */
  262.     public function retrieveProxy $proxyName )
  263.     {
  264.         return isset$this->model $this->model->retrieveProxy$proxyName null );
  265.     }
  266.  
  267.     /**
  268.      * Remove Proxy
  269.      *
  270.      * Remove a previously registered <b>IProxy</b> instance from the <b>Model</b> by name.
  271.      *
  272.      * @param string $proxyName Name of the <b>IProxy</b> to remove from the <b>Model</b>.
  273.      * @return IProxy The <b>IProxy</b> that was removed from the <b>Model</b>.
  274.      */
  275.     public function removeProxy $proxyName )
  276.     {
  277.         return isset$this->model $this->model->removeProxy$proxyName )null);
  278.     }
  279.  
  280.     /**
  281.      * Has Proxy
  282.      *
  283.      * Check if a Proxy is registered for the given <var>proxyName</var>.
  284.      *
  285.      * @param string $proxyName Name of the <b>Proxy</b> to check for.
  286.      * @return bool Boolean: Whether a <b>Proxy</b> is currently registered with the given <var>proxyName</var>.
  287.      */
  288.     public function hasProxy$proxyName )
  289.     {
  290.         return isset$this->model $this->model->hasProxy$proxyName false );
  291.     }
  292.  
  293.     /**
  294.      * Register Command
  295.      *
  296.      * Register an <b>ICommand</b> with the <b>Controller</b>.
  297.      *
  298.      * @param string $noteName Name of the <b>INotification</b>
  299.      * @param object|string$commandClassRef <b>ICommand</b> object to register. Can be an object OR a class name.
  300.      * @return void 
  301.      */
  302.     public function registerCommand$notificationName$commandClassRef)
  303.     {
  304.         ifisset$this->controller ) )
  305.         {
  306.             $this->controller->registerCommand$notificationName$commandClassRef );
  307.         }
  308.     }
  309.     /**
  310.      * Remove Command
  311.      *
  312.      * Remove a previously registered <b>ICommand</b> to <b>INotification</b> mapping.
  313.      *
  314.      * @param string $notificationName Name of the <b>INotification</b> to remove the <b>ICommand</b> mapping for.
  315.      */
  316.     public function removeCommand$notificationName )
  317.     {
  318.         ifisset$this->controller ) )
  319.         {
  320.             $this->controller->removeCommand$notificationName );
  321.         }
  322.     }
  323.  
  324.     /**
  325.      * Has Command
  326.      *
  327.      * Check if a <b>Command</b> is registered for a given <b>Notification</b>
  328.      *
  329.      * @param string $notificationName Name of the <b>INotification</b> to check for.
  330.      * @return bool Whether a <b>Command</b> is currently registered for the given <var>notificationName</var>.
  331.      */
  332.     public function hasCommand$notificationName )
  333.     {
  334.         return isset$this->controller $this->controller->hasCommand($notificationNamefalse );
  335.     }
  336.  
  337.     /**
  338.      * Register Mediator
  339.      *
  340.      * Register an <b>IMediator</b> instance with the <b>View</b>.
  341.      *
  342.      * @param IMediator $mediator Reference to the <b>IMediator</b> instance.
  343.      */
  344.     public function registerMediatorIMediator $mediator )
  345.     {
  346.         if isset$this->view ) )
  347.         {
  348.             $this->view->registerMediator$mediator );
  349.         }
  350.     }
  351.  
  352.     /**
  353.      * Retreive Mediator
  354.      *
  355.      * Retrieve a previously registered <b>IMediator</b> instance from the <b>View</b>.
  356.      *
  357.      * @param string $mediatorName Name of the <b>IMediator</b> instance to retrieve.
  358.      * @return IMediator The <b>IMediator</b> previously registered with the given <var>mediatorName</var>.
  359.      */
  360.     public function retrieveMediator$mediatorName )
  361.     {
  362.         return isset$this->view $this->view->retrieveMediator$mediatorName null );
  363.     }
  364.  
  365.     /**
  366.      * Remove Mediator
  367.      *
  368.      * Remove a previously registered <b>IMediator</b> instance from the <b>View</b>.
  369.      *
  370.      * @param string $mediatorName Name of the <b>IMediator</b> instance to be removed.
  371.      * @return IMediator The <b>IMediator</b> instance previously registered with the given <var>mediatorName</var>.
  372.      */
  373.     public function removeMediator$mediatorName )
  374.     {
  375.         return isset$this->view $this->view->removeMediator$mediatorName )null);
  376.     }
  377.  
  378.     /**
  379.      * Has Mediator
  380.      *
  381.      * Check if a <b>IMediator</b> is registered or not.
  382.      *
  383.      * @param string $mediatorName The name of the <b>IMediator</b> to check for.
  384.      * @return bool Boolean: Whether a <b>IMediator</b> is registered with the given <var>mediatorName</var>.
  385.      */
  386.     public function hasMediator$mediatorName )
  387.     {
  388.         return isset$this->view $this->view->hasMediator$mediatorName false );
  389.     }
  390.  
  391.     /**
  392.      * Create and send an <b>INotification</b>.
  393.      *
  394.      * Keeps us from having to construct new notification
  395.      * instances in our implementation code.
  396.      *
  397.      * @param string $notificationName The name of the notification to send.
  398.      * @param mixed $body The body of the notification (optional).
  399.      * @param string $type The type of the notification (optional).
  400.      * @return void 
  401.      */
  402.     public function sendNotification$notificationName $body =null$type=null )
  403.     {
  404.         $this->notifyObserversnew Notification$notificationName$body$type ) );
  405.     }
  406.  
  407.     /**
  408.      * Notify <b>Observer</b>s.
  409.      *
  410.      * This method is left public mostly for backward
  411.      * compatibility, and to allow you to send custom
  412.      * notification classes using the facade.
  413.      *
  414.      * Usually you should just call sendNotification
  415.      * and pass the parameters, never having to
  416.      * construct the notification yourself.
  417.      *
  418.      * @param INotification $notification The <b>INotification</b> to have the <b>View</b> notify <b>Observers</b> of.
  419.      * @return void 
  420.      */
  421.     public function notifyObservers INotification $notification )
  422.     {
  423.         if isset$this->view ) )
  424.         {
  425.             $this->view->notifyObservers$notification );
  426.         }
  427.     }
  428.  
  429.     /**
  430.      * Set the Multiton key for this facade instance.
  431.      *
  432.      * Not called directly, but instead from the
  433.      * constructor when getInstance is invoked.
  434.      * It is necessary to be public in order to
  435.      * implement INotifier.
  436.      *
  437.      * @param string $key Unique key for this instance.
  438.      * @return void 
  439.      */
  440.     public function initializeNotifier$key )
  441.     {
  442.         $this->multitonKey = $key;
  443.     }
  444.  
  445.     /**
  446.      * Check if a Core is registered or not
  447.      *
  448.      * @param string $key the multiton key for the Core in question
  449.      * @return bool Whether a Core is registered with the given <code>key</code>.
  450.      */
  451.     public static function hasCore$key )
  452.     {
  453.         return issetself::$instanceMap$key ) );
  454.     }
  455.  
  456.     /**
  457.      * Remove a Core.
  458.      *
  459.      * Remove the Model, View, Controller and Facade
  460.      * instances for the given key.
  461.      *
  462.      * @param string $key MultitonKey of the Core to remove
  463.      * @return void 
  464.      */
  465.     public static function removeCore$key )
  466.     {
  467.         if !self::hasCore$key ) ) return;
  468.  
  469.         Model::removeModel$key );
  470.         View::removeView$key );
  471.         Controller::removeController$key );
  472.         self::$instanceMap$key null;
  473.     }
  474.  
  475. }

Documentation generated on Mon, 03 Aug 2009 04:57:53 +0000 by phpDocumentor 1.4.2