Source for file Renderer.php

Documentation is available at Renderer.php

  1. <?php
  2. // vim: foldmethod=marker
  3. /**
  4.  *  Renderer.php
  5.  *
  6.  *  @author     Kazuhiro Hosoi <hosoi@gree.co.jp>
  7.  *  @license    http://www.opensource.org/licenses/bsd-license.php The BSD License
  8.  *  @package    Ethna
  9.  *  @version    $Id: e3e9c8f68ab4a40e933215e67c8a90c383e7c09f $
  10.  */
  11.  
  12. // {{{ Ethna_Renderer
  13. /**
  14.  *  Template Renderer
  15.  *
  16.  *  @author     Kazuhiro Hosoi <hosoi@gree.co.jp>
  17.  *  @access     public
  18.  *  @package    Ethna
  19.  */
  20. {
  21.     /**#@+
  22.      *  @access private
  23.      */
  24.  
  25.     /** @protected    object  Ethna_Controller    controllerオブジェクト */
  26.     protected $controller;
  27.  
  28.     /** @protected    object  Ethna_Controller    controllerオブジェクト($controllerの省略形) */
  29.     protected $ctl;
  30.  
  31.     /** @protected    array   [appid]-ini.phpのレンダラ設定 */
  32.     protected $config;
  33.  
  34.     /** @protected    string  template directory  */
  35.     protected $template_dir;
  36.  
  37.     /** @protected    string  template engine */
  38.     protected $engine;
  39.  
  40.     /** @protected    string  path of template engine */
  41.     protected $engine_path = false;
  42.  
  43.     /** @protected    string  template file */
  44.     protected $template;
  45.  
  46.     /** @protected    string  テンプレート変数 */
  47.     protected $prop;
  48.  
  49.     /** @protected    string  レンダラプラグイン(Ethna_Pluginとは関係なし) */
  50.     protected $plugin_registry;
  51.  
  52.     /** @protected    object  Ethna_Logger    ログオブジェクト */
  53.     protected $logger;
  54.  
  55.  
  56.     /**
  57.      *  Ethna_Rendererクラスのコンストラクタ
  58.      *
  59.      *  @access public
  60.      */
  61.     public function __construct($controller)
  62.     {
  63.         $this->controller = $controller;
  64.         $this->ctl = $this->controller;
  65.         $this->template_dir = null;
  66.         $this->engine = null;
  67.         $this->template = null;
  68.         $this->prop = array();
  69.         $this->plugin_registry = array();
  70.         $config $this->ctl->getConfig();
  71.         $this->config = $config->get('renderer');
  72.         $this->logger = $this->controller->getBackend()->getLogger();
  73.     }
  74.  
  75.     /**
  76.      *  ビューを出力する
  77.      *
  78.      *  @param string   $template   テンプレート
  79.      *  @param  bool    $capture    true ならば出力を表示せずに返す
  80.      *
  81.      *  @access public
  82.      */
  83.     function perform($template null$capture false)
  84.     {
  85.         if ($template == null && $this->template == null{
  86.             return Ethna::raiseWarning('template is not defined');
  87.         }
  88.  
  89.         if ($template != null{
  90.             $this->template = $template;
  91.         }
  92.  
  93.         // テンプレートの有無のチェック
  94.         if (is_readable($this->template_dir . $this->template=== false{
  95.             return Ethna::raiseWarning("template is not found: " $this->template);
  96.         }
  97.  
  98.         if ($capture === true{
  99.             ob_start();
  100.             include_once $this->template_dir . $this->template;
  101.             $captured ob_get_contents();
  102.             ob_end_clean();
  103.             return $captured;
  104.         else {
  105.             include_once $this->template_dir . $this->template;
  106.             return true;
  107.         }
  108.     }
  109.  
  110.     /**
  111.      *  テンプレートエンジンを取得する
  112.      * 
  113.      *  @return object   Template Engine.
  114.      * 
  115.      *  @access public
  116.      */
  117.     function getEngine()
  118.     {
  119.         return $this->engine;
  120.     }
  121.  
  122.     /**
  123.      *  テンプレートディレクトリを取得する
  124.      * 
  125.      *  @return string   Template Directory
  126.      * 
  127.      *  @access public
  128.      */
  129.     function getTemplateDir()
  130.     {
  131.         return $this->template_dir;
  132.     }
  133.  
  134.     /**
  135.      *  テンプレート変数を取得する
  136.      * 
  137.      *  @param string $name  変数名
  138.      * 
  139.      *  @return mixed    変数
  140.      * 
  141.      *  @access public
  142.      */
  143.     function getProp($name)
  144.     {
  145.         if (isset($this->prop[$name])) {
  146.             return $this->prop[$name];
  147.         }
  148.  
  149.         return null;
  150.     }
  151.  
  152.     /**
  153.      *  テンプレート変数を削除する
  154.      * 
  155.      *  @param name    変数名
  156.      * 
  157.      *  @access public
  158.      */
  159.     function removeProp($name)
  160.     {
  161.         if (isset($this->prop[$name])) {
  162.             unset($this->prop[$name]);
  163.         }
  164.     }
  165.  
  166.     /**
  167.      *  テンプレート変数に配列を割り当てる
  168.      * 
  169.      *  @param array $array 
  170.      * 
  171.      *  @access public
  172.      */
  173.     function setPropArray($array)
  174.     {
  175.         $this->prop = array_merge($this->prop$array);
  176.     }
  177.  
  178.     /**
  179.      *  テンプレート変数に配列を参照として割り当てる
  180.      * 
  181.      *  @param array $array 
  182.      * 
  183.      *  @access public
  184.      */
  185.     function setPropArrayByRef(&$array)
  186.     {
  187.         $keys  array_keys($array);
  188.         $count sizeof($keys);
  189.  
  190.         for ($i 0$i $count$i++{
  191.             $this->prop[$keys[$i]] $array[$keys[$i]];
  192.         }
  193.     }
  194.  
  195.     /**
  196.      * テンプレート変数を割り当てる
  197.      * 
  198.      * @param string $name 変数名
  199.      * @param mixed $value 
  200.      * 
  201.      * @access public
  202.      */
  203.     function setProp($name$value)
  204.     {
  205.         $this->prop[$name$value;
  206.     }
  207.  
  208.     /**
  209.      *  テンプレート変数に参照を割り当てる
  210.      * 
  211.      *  @param string $name 変数名
  212.      *  @param mixed $value 
  213.      * 
  214.      *  @access public
  215.      */
  216.     function setPropByRef($name&$value)
  217.     {
  218.         $this->prop[$name$value;
  219.     }
  220.  
  221.     /**
  222.      *  テンプレートを割り当てる
  223.      * 
  224.      *  @param string $template テンプレート名
  225.      * 
  226.      *  @access public
  227.      */
  228.     function setTemplate($template)
  229.     {
  230.         $this->template = $template;
  231.     }
  232.  
  233.     /**
  234.      *  テンプレートディレクトリを割り当てる
  235.      * 
  236.      *  @param string $dir ディレクトリ名
  237.      * 
  238.      *  @access public
  239.      */
  240.     function setTemplateDir($dir)
  241.     {
  242.         $this->template_dir = $dir;
  243.  
  244.         if (substr($this->template_dir-1!= '/'{
  245.             $this->template_dir .= '/';
  246.         }
  247.     }
  248.     
  249.     /**
  250.      *  テンプレートの有無をチェックする
  251.      * 
  252.      *  @param string $template テンプレート名
  253.      * 
  254.      *  @access public
  255.      */
  256.     function templateExists($template)
  257.     {
  258.         if (substr($this->template_dir-1!= '/'{
  259.             $this->template_dir .= '/';
  260.         }
  261.  
  262.         return (is_readable($this->template_dir . $template));
  263.     }
  264.  
  265.     /**
  266.      *  プラグインをセットする
  267.      * 
  268.      *  @param string $name プラグイン名 
  269.      *  @param string $type プラグインタイプ
  270.      *  @param string $plugin プラグイン本体
  271.      * 
  272.      *  @access public
  273.      */
  274.     function setPlugin($name$type$plugin)
  275.     {
  276.         $this->plugin_registry[$type][$name$plugin;
  277.     }
  278.  
  279.     // {{{ proxy methods (for B.C.)
  280.     /**
  281.      *  テンプレート変数を割り当てる(後方互換)
  282.      *
  283.      *  @access public
  284.      */
  285.     function assign($name$value)
  286.     {
  287.         $this->setProp($name$value);
  288.     }
  289.     // }}}
  290.  
  291.     /**
  292.      *  テンプレート変数に参照を割り当てる(後方互換)
  293.      *
  294.      *  @access public
  295.      */
  296.     function assign_by_ref($name&$value)
  297.     {
  298.         $this->setPropByRef($name$value);
  299.     }
  300.  
  301.     /**
  302.      *  ビューを出力する
  303.      *
  304.      *  @access public
  305.      */
  306.     function display($template null)
  307.     {
  308.         return $this->perform($template);
  309.     }
  310.     // }}}
  311.  
  312.     // {{{ loadEngine
  313.     /**
  314.      *  ビューを出力する
  315.      *
  316.      *  @access public
  317.      */
  318.     protected function loadEngine(array $config)
  319.     {
  320.         // load template engine
  321.         $engine_path = isset($config['path'])
  322.             ? $config['path']
  323.             : $this->engine_path;
  324.         if ($engine_path{
  325.             if (file_exists_ex($engine_path)) {
  326.                 require_once $engine_path;
  327.             }
  328.             else {
  329.                 trigger_error("template engine is not available: path=" $engine_pathE_USER_ERROR);
  330.             }
  331.         }
  332.     }
  333.     // }}}
  334. }
  335. // }}}

Documentation generated on Fri, 11 Nov 2011 03:59:00 +0900 by phpDocumentor 1.4.3