      1: <?php
      2:
      3: /**
      4:  *
      5:  * A factory for creating rendering provider implementations that implement the
      6:  * AlphaRendererProviderInterface interface.
      7:  *
      8:  * @package alpha::view::renderers
      9:  * @since 1.2
     10:  * @author John Collins <dev@alphaframework.org>
     11:  * @version $Id: AlphaRendererProviderFactory.inc 1548 2012-07-29 17:07:07Z alphadevx $
     12:  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
     13:  * @copyright Copyright (c) 2012, John Collins (founder of Alpha Framework).
     14:  * All rights reserved.
     15:  *
     16:  * <pre>
     17:  * Redistribution and use in source and binary forms, with or
     18:  * without modification, are permitted provided that the
     19:  * following conditions are met:
     20:  *
     21:  * * Redistributions of source code must retain the above
     22:  *   copyright notice, this list of conditions and the
     23:  *   following disclaimer.
     24:  * * Redistributions in binary form must reproduce the above
     25:  *   copyright notice, this list of conditions and the
     26:  *   following disclaimer in the documentation and/or other
     27:  *   materials provided with the distribution.
     28:  * * Neither the name of the Alpha Framework nor the names
     29:  *   of its contributors may be used to endorse or promote
     30:  *   products derived from this software without specific
     31:  *   prior written permission.
     32:  *
     33:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
     34:  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
     35:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     36:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     37:  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     38:  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     39:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     40:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     41:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     42:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     43:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     44:  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     45:  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     46:  * </pre>
     47:  *
     48:  */
     49: class AlphaRendererProviderFactory {
     50:     /**
     51:      * Trace logger
     52:      *
     53:      * @var Logger
     54:      * @since 1.2
     55:      */
     56:     private static $logger = null;
     57:
     58:     /**
     59:      * A static method that attempts to return a AlphaRendererProviderInterface instance
     60:      * based on the name of the provider class supplied.
     61:      *
     62:      * @param $providerName The class name of the provider class, should be in one of the alpha::view::renderers::[html|json|xml] packages.
     63:      * @param $BO The AlphaDAO instance to pass to the renderer provider for passing data.
     64:      * @throws IllegalArguementException
     65:      * @return AlphaRendererProviderInterface
     66:      * @since 1.2
     67:      */
     68:     public static function getInstance($providerName, $BO) {
     69:         if(self::$logger == null)
     70:             self::$logger = new Logger('AlphaRendererProviderFactory');
     71:
     72:         self::$logger->debug('>>getInstance(providerName=['.$providerName.'])');
     73:
     74:         global $config;
     75:
     76:         if($providerName == 'auto') {
     77:             if(strpos($_SERVER['REQUEST_URI'], '/api/') !== false) {
     78:                 // inspection of the Accept header will be placed here once we add REST API support in later versions of Alpha
     79:             }else{
     80:                 // use the HTML renderer
     81:                 $providerName = 'AlphaRendererProviderHTML';
     82:             }
     83:         }
     84:
     85:         if(file_exists($config->get('app.root').'alpha/view/renderers/html/'.$providerName.'.inc')) {
     86:             require_once $config->get('app.root').'alpha/view/renderers/html/'.$providerName.'.inc';
     87:         }elseif(file_exists($config->get('app.root').'alpha/view/renderers/xml/'.$providerName.'.inc')) {
     88:                         require_once $config->get('app.root').'alpha/view/renderers/xml/'.$providerName.'.inc';
     89:                 }elseif(file_exists($config->get('app.root').'alpha/view/renderers/json/'.$providerName.'.inc')) {
     90:                         require_once $config->get('app.root').'alpha/view/renderers/json/'.$providerName.'.inc';
     91:         }else{
     92:             throw new IllegalArguementException('The class ['.$providerName.'] is not defined anywhere!');
     93:         }
     94:
     95:         $instance = new $providerName;
     96:                 $instance->setBO($BO);
     97:
     98:                 if(!$instance instanceof AlphaRendererProviderInterface)
     99:                     throw new IllegalArguementException('The class ['.$providerName.'] does not implement the expected AlphaRendererProviderInterface interface!');
    100:
    101:                 self::$logger->debug('<<getInstance: [Object '.$providerName.']');
    102:                 return $instance;
    103:
    104:         self::$logger->debug('<<getInstance');
    105:     }
    106: }
    107:
    108: ?>
    109: