OSDN Git Service

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