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

Documentation generated on Fri, 11 Nov 2011 04:00:48 +0900 by phpDocumentor 1.4.3