Source for file Ethna_Renderer_Rhaco.php

Documentation is available at Ethna_Renderer_Rhaco.php

  1. <?php
  2. /**
  3.  *  Ethna_Renderer_Rhaco.php (experimental)
  4.  *
  5.  *  @author     TSURUOKA Naoya <tsuruoka@php.net>
  6.  *  @license    http://www.opensource.org/licenses/bsd-license.php The BSD License
  7.  *  @package    Ethna
  8.  *  @version    $Id: Ethna_Renderer_Rhaco.php,v 1.16 2007/01/17 18:38:51 ichii386 Exp $
  9.  */
  10.  
  11.  
  12. require_once 'rhaco/Rhaco.php';
  13. require_once 'rhaco/tag/TemplateParser.php';
  14. require_once ETHNA_BASE '/class/Ethna_SmartyPlugin.php';
  15.  
  16. /**
  17.  *  Rhacoレンダラクラス
  18.  *
  19.  *  @access     public
  20.  *  @package    Ethna
  21.  */
  22. {
  23.     /** @var    string compile directory  */
  24.     var $compile_dir = '';
  25.  
  26.     var $template_dir = '';
  27.  
  28.     /**
  29.      * plugin list
  30.      * @var     array 
  31.      * @access  protected
  32.      */
  33.     var $smarty_plugin_list = array();
  34.  
  35.     /**
  36.      * Rhaco TemplateParser
  37.      * @var     TemplateParser_Ethna $engine 
  38.      * @access  protected
  39.      */
  40.     var $engine;
  41.     
  42.     /**
  43.      *  Ethna_Renderer_Rhacoクラスのコンストラクタ
  44.      *
  45.      *  @access public
  46.      */
  47.     function Ethna_Renderer_Rhaco(&$controller)
  48.     {
  49.         parent::Ethna_Renderer($controller);
  50.         
  51.         $this->template_dir = $controller->getTemplatedir('/';
  52.         $this->compile_dir = $controller->getDirectory('template_c');
  53.  
  54.         Rhaco::constant('TEMPLATE_PATH'$this->template_dir);
  55.         
  56.         $this->engine =new TemplateParser_Ethna();
  57.  
  58.         /*
  59.         $this->setTemplateDir($template_dir);
  60.         $this->compile_dir = $compile_dir;
  61.          */
  62.         $this->engine->template_dir $this->template_dir;
  63.         $this->engine->compile_dir $this->compile_dir;
  64.         $this->engine->compile_id md5($this->template_dir);
  65.  
  66.         // 一応がんばってみる
  67.         if (is_dir($this->engine->compile_dir=== false{
  68.             Ethna_Util::mkdir($this->engine->compile_dir0755);
  69.         }
  70.  
  71.         $this->_setDefaultPlugin();
  72.     }
  73.     
  74.     /**
  75.      *  ビューを出力する
  76.      *
  77.      *  @access public
  78.      *  @param  string  $template   テンプレート名
  79.      *  @param  bool    $capture    true ならば出力を表示せずに返す
  80.      */
  81.     function perform($template null$capture false)
  82.     {
  83.         if ($template === null && $this->template === null{
  84.             return Ethna::raiseWarning('template is not defined');
  85.         }
  86.  
  87.         if ($template !== null{
  88.             $this->template $template;
  89.         }
  90.  
  91.         if ((is_absolute_path($this->template&& is_readable($this->template))
  92.             || is_readable($this->template_dir . $this->template)) {
  93.                 if ($capture === true{
  94.                     $captured $this->engine->read($this->template);
  95.                     return $captured;
  96.                 else {
  97.                     $captured $this->engine->read($this->template);
  98.                     print($captured);
  99.                 }
  100.         else {
  101.             return Ethna::raiseWarning('template not found ' $this->template);
  102.         }
  103.     }
  104.     
  105.     /**
  106.      * テンプレート変数を取得する
  107.      * 
  108.      *  @todo fixme
  109.      *  @access public
  110.      *  @param string $name  変数名
  111.      *  @return mixed 変数 
  112.      */
  113.     function &getProp($name null)
  114.     {
  115.         $property =$this->engine->variables[$name];
  116.  
  117.         if ($property !== null{
  118.             return $property;
  119.         }
  120.  
  121.         return null;
  122.     }
  123.  
  124.     /**
  125.      *  テンプレート変数を削除する
  126.      * 
  127.      *  @param name    変数名
  128.      *  @todo
  129.      *  @access public
  130.      */
  131.     function removeProp()
  132.     {
  133.         $this->engine->clearVariable(func_num_args());
  134.     }
  135.  
  136.     /**
  137.      *  テンプレート変数に配列を割り当てる
  138.      * 
  139.      *  @param array $array 
  140.      *  @access public
  141.      */
  142.     function setPropArray($array)
  143.     {
  144.         $this->engine->setVariable($array);
  145.     }
  146.  
  147.     /**
  148.      *  テンプレート変数に配列を参照として割り当てる
  149.      * 
  150.      *  @param array $array 
  151.      *  @todo no implement
  152.      *  @access public
  153.      */
  154.     function setPropArrayByRef(&$array)
  155.     {
  156.         //$this->engine->assign_by_ref($array);
  157.     }
  158.  
  159.     /**
  160.      *  テンプレート変数を割り当てる
  161.      * 
  162.      *  @param string $name 変数名
  163.      *  @param mixed $value 
  164.      * 
  165.      *  @access public
  166.      */
  167.     function setProp($name$value)
  168.     {
  169.         $this->engine->setVariable($name$value);
  170.     }
  171.  
  172.     /**
  173.      *  テンプレート変数に参照を割り当てる
  174.      * 
  175.      *  @access public
  176.      *  @todo fixme
  177.      *  @param string $name 変数名
  178.      *  @param mixed $value 
  179.      */
  180.     function setPropByRef($name&$value)
  181.     {
  182.         $this->engine->setVariable($name$value);
  183.         //$this->engine->assign_by_ref($name, $value);
  184.     }
  185.  
  186.     /**
  187.      * setPlugin
  188.      *
  189.      * @access public
  190.      */
  191.     function setPlugin($name$type$plugin)
  192.     {
  193.         //Smartyプラグイン関数の有無をチェック
  194.         if (is_callable($plugin=== false{
  195.             return Ethna::raiseWarning('Does not exists.');
  196.         }
  197.  
  198.         $this->smarty_plugin_list[$namearray(
  199.             'plugin' => $plugin,
  200.             'type' => $type
  201.         );
  202.     }
  203.  
  204.     /**
  205.      * getPluginList
  206.      *
  207.      * @access public
  208.      */
  209.     function getPluginList()
  210.     {
  211.         return $this->smarty_plugin_list;
  212.     }
  213.  
  214.     /**
  215.      *  デフォルトの設定.
  216.      *
  217.      *  @access public
  218.      */
  219.     function _setDefaultPlugin()
  220.     {
  221.         /*
  222.         // default modifiers
  223.         $this->setPlugin('number_format','modifier','smarty_modifier_number_format');
  224.         $this->setPlugin('strftime','modifier','smarty_modifier_strftime');
  225.         $this->setPlugin('count','modifier','smarty_modifier_count');
  226.         $this->setPlugin('join','modifier','smarty_modifier_join');
  227.         $this->setPlugin('filter','modifier', 'smarty_modifier_filter');
  228.         $this->setPlugin('unique','modifier','smarty_modifier_unique');
  229.         $this->setPlugin('wordwrap_i18n','modifier','smarty_modifier_wordwrap_i18n');
  230.         $this->setPlugin('truncate_i18n','modifier','smarty_modifier_truncate_i18n');
  231.         $this->setPlugin('i18n','modifier','smarty_modifier_i18n');
  232.         $this->setPlugin('checkbox','modifier','smarty_modifier_checkbox');
  233.         $this->setPlugin('select','modifier','smarty_modifier_select');
  234.         $this->setPlugin('form_value','modifier','smarty_modifier_form_value');
  235.          */
  236.  
  237.         // default functions
  238.         $this->setPlugin('is_error','function','smarty_function_is_error');
  239.         $this->setPlugin('message','function','smarty_function_message');
  240.         $this->setPlugin('uniqid','function','smarty_function_uniqid');
  241.         $this->setPlugin('select','function','smarty_function_select');
  242.         $this->setPlugin('checkbox_list','function','smarty_function_checkbox_list');
  243.         $this->setPlugin('form_name','function','smarty_function_form_name');
  244.         $this->setPlugin('form_input','function','smarty_function_form_input');
  245.         $this->setPlugin('form_submit','function','smarty_function_form_submit');
  246.         $this->setPlugin('url','function','smarty_function_url');
  247.         $this->setPlugin('csrfid','function','smarty_function_csrfid');
  248.  
  249.         // default blocks
  250.         $this->setPlugin('form','block','smarty_block_form');       
  251.  
  252.         $this->engine->setSmartyPluginList($this->getPluginList());
  253.     }
  254.  
  255.  
  256. }
  257.  
  258. /**
  259.  * TemplateParser_Ethna
  260.  */
  261. class TemplateParser_Ethna extends TemplateParser
  262. {
  263.     /**
  264.      * smarty_function list
  265.      * @var     array 
  266.      * @access  protected
  267.      */
  268.     var $smarty_plugin_list = array();
  269.     
  270.     /**
  271.      * fake property for Smaty
  272.      *
  273.      * @access public
  274.      */
  275.     var $_tag_stack = array();
  276.  
  277.     /**
  278.      * setSmartyPluginList
  279.      *
  280.      */
  281.     function setSmartyPluginList($plugin_list)
  282.     {
  283.         if (!is_array($plugin_list)) {
  284.             return false;
  285.         }
  286.         $this->smarty_plugin_list = $plugin_list;
  287.     }
  288.  
  289.     /**
  290.      * getSmartyPluginList
  291.      *
  292.      * @access public
  293.      */
  294.     function getSmartyPluginList()
  295.     {
  296.         return $this->smarty_plugin_list;
  297.     }
  298.  
  299.     /**
  300.      * smarty_function dispatcher
  301.      *
  302.      * @access protected
  303.      * @param string $src 
  304.      */
  305.     function _exec9002_smartyfunctions($src){
  306.  
  307.         $tag new SimpleTag();
  308.         $smarty_plugin_list $this->getSmartyPluginList();
  309.  
  310.         foreach($smarty_plugin_list as $name => $plugin_config{
  311.             
  312.             while ($tag->set($src$this->_getTagName($name))) {
  313.  
  314.                 if ($plugin_config['type'== 'function'{
  315.  
  316.                     $param $tag->toHash();
  317.                     $src str_replace(
  318.                         $tag->getPlain(),
  319.                         $plugin_config['plugin']($param$this),
  320.                         $src
  321.                     );
  322.  
  323.                 else if ($plugin_config['type'== 'block'{
  324.                     
  325.                     $repeat_before true;
  326.                     $repeat_after false;
  327.                     $param_list $tag->getParameter();
  328.                     foreach ($param_list as $param_tag{
  329.                         $param[$param_tag->getName()$param_tag->getValue();
  330.                     }
  331.                     $content $tag->getValue();
  332.  
  333.                     //before(not return value)
  334.                     $result $plugin_config['plugin']($param$content$this$repeat_before);
  335.  
  336.                     //after
  337.                     $result $plugin_config['plugin']($param$content$this$repeat_after);
  338.                     $src str_replace(
  339.                         $tag->getPlain(),
  340.                         $result,
  341.                         $src
  342.                     );
  343.  
  344.                 }
  345.             }
  346.  
  347.         }
  348.  
  349.         return $src;
  350.     }
  351.  
  352. }
  353.  
  354. ?>

Documentation generated on Thu, 08 May 2008 00:15:25 +0900 by phpDocumentor 1.4.2