--- /dev/null
+<?php
+/**
+ * Ethna_Renderer_Rhaco.php (experimental)
+ *
+ * @author TSURUOKA Naoya <tsuruoka@php.net>
+ * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
+ * @package Ethna
+ * @version $Id: Ethna_Renderer_Rhaco.php,v 1.16 2007/01/17 18:38:51 ichii386 Exp $
+ */
+
+set_include_path(get_include_path() . PATH_SEPARATOR . BASE . '/lib/rhaco');
+
+require_once 'rhaco/Rhaco.php';
+require_once 'rhaco/tag/TemplateParser.php';
+require_once ETHNA_BASE . '/class/Ethna_SmartyPlugin.php';
+
+/**
+ * Rhacoレンダラクラス
+ *
+ * @access public
+ * @package Ethna
+ */
+class Ethna_Renderer_Rhaco extends Ethna_Renderer
+{
+ /** @var string compile directory */
+ var $compile_dir = '';
+
+ var $template_dir = '';
+
+ /**
+ * plugin list
+ * @var array
+ * @access protected
+ */
+ var $smarty_plugin_list = array();
+
+ /**
+ * Rhaco TemplateParser
+ * @var TemplateParser_Ethna $engine
+ * @access protected
+ */
+ var $engine;
+
+ /**
+ * Ethna_Renderer_Rhacoクラスのコンストラクタ
+ *
+ * @access public
+ */
+ function Ethna_Renderer_Rhaco(&$controller)
+ {
+ parent::Ethna_Renderer($controller);
+
+ $this->template_dir = $controller->getTemplatedir() . '/';
+ $this->compile_dir = $controller->getDirectory('template_c');
+
+ Rhaco::constant('TEMPLATE_PATH', $this->template_dir);
+
+ $this->engine =& new TemplateParser_Ethna();
+
+ /*
+ $this->setTemplateDir($template_dir);
+ $this->compile_dir = $compile_dir;
+ */
+ $this->engine->template_dir = $this->template_dir;
+ $this->engine->compile_dir = $this->compile_dir;
+ $this->engine->compile_id = md5($this->template_dir);
+
+ // 一応がんばってみる
+ if (is_dir($this->engine->compile_dir) === false) {
+ Ethna_Util::mkdir($this->engine->compile_dir, 0755);
+ }
+
+ $this->_setDefaultPlugin();
+ }
+
+ /**
+ * ビューを出力する
+ *
+ * @access public
+ * @param string $template テンプレート名
+ * @param bool $capture true ならば出力を表示せずに返す
+ */
+ function perform($template = null, $capture = false)
+ {
+ if ($template === null && $this->template === null) {
+ return Ethna::raiseWarning('template is not defined');
+ }
+
+ if ($template !== null) {
+ $this->template = $template;
+ }
+
+ if ((is_absolute_path($this->template) && is_readable($this->template))
+ || is_readable($this->template_dir . $this->template)) {
+ if ($capture === true) {
+ $captured = $this->engine->read($this->template);
+ return $captured;
+ } else {
+ $captured = $this->engine->read($this->template);
+ print($captured);
+ }
+ } else {
+ return Ethna::raiseWarning('template not found ' . $this->template);
+ }
+ }
+
+ /**
+ * テンプレート変数を取得する
+ *
+ * @todo fixme
+ * @access public
+ * @param string $name 変数名
+ * @return mixed 変数
+ */
+ function &getProp($name = null)
+ {
+ $property =& $this->engine->variables[$name];
+
+ if ($property !== null) {
+ return $property;
+ }
+
+ return null;
+ }
+
+ /**
+ * テンプレート変数を削除する
+ *
+ * @param name 変数名
+ * @todo
+ * @access public
+ */
+ function removeProp()
+ {
+ $this->engine->clearVariable(func_num_args());
+ }
+
+ /**
+ * テンプレート変数に配列を割り当てる
+ *
+ * @param array $array
+ * @access public
+ */
+ function setPropArray($array)
+ {
+ $this->engine->setVariable($array);
+ }
+
+ /**
+ * テンプレート変数に配列を参照として割り当てる
+ *
+ * @param array $array
+ * @todo no implement
+ * @access public
+ */
+ function setPropArrayByRef(&$array)
+ {
+ //$this->engine->assign_by_ref($array);
+ }
+
+ /**
+ * テンプレート変数を割り当てる
+ *
+ * @param string $name 変数名
+ * @param mixed $value 値
+ *
+ * @access public
+ */
+ function setProp($name, $value)
+ {
+ $this->engine->setVariable($name, $value);
+ }
+
+ /**
+ * テンプレート変数に参照を割り当てる
+ *
+ * @access public
+ * @todo fixme
+ * @param string $name 変数名
+ * @param mixed $value 値
+ */
+ function setPropByRef($name, &$value)
+ {
+ $this->engine->setVariable($name, $value);
+ //$this->engine->assign_by_ref($name, $value);
+ }
+
+ /**
+ * setPlugin
+ *
+ * @access public
+ */
+ function setPlugin($name, $type, $plugin)
+ {
+ //Smartyプラグイン関数の有無をチェック
+ if (is_callable($plugin) === false) {
+ return Ethna::raiseWarning('Does not exists.');
+ }
+
+ $this->smarty_plugin_list[$name] = array(
+ 'plugin' => $plugin,
+ 'type' => $type
+ );
+ }
+
+ /**
+ * getPluginList
+ *
+ * @access public
+ */
+ function getPluginList()
+ {
+ return $this->smarty_plugin_list;
+ }
+
+ /**
+ * デフォルトの設定.
+ *
+ * @access public
+ */
+ function _setDefaultPlugin()
+ {
+ /*
+ // default modifiers
+ $this->setPlugin('number_format','modifier','smarty_modifier_number_format');
+ $this->setPlugin('strftime','modifier','smarty_modifier_strftime');
+ $this->setPlugin('count','modifier','smarty_modifier_count');
+ $this->setPlugin('join','modifier','smarty_modifier_join');
+ $this->setPlugin('filter','modifier', 'smarty_modifier_filter');
+ $this->setPlugin('unique','modifier','smarty_modifier_unique');
+ $this->setPlugin('wordwrap_i18n','modifier','smarty_modifier_wordwrap_i18n');
+ $this->setPlugin('truncate_i18n','modifier','smarty_modifier_truncate_i18n');
+ $this->setPlugin('i18n','modifier','smarty_modifier_i18n');
+ $this->setPlugin('checkbox','modifier','smarty_modifier_checkbox');
+ $this->setPlugin('select','modifier','smarty_modifier_select');
+ $this->setPlugin('form_value','modifier','smarty_modifier_form_value');
+ */
+
+ // default functions
+ $this->setPlugin('is_error','function','smarty_function_is_error');
+ $this->setPlugin('message','function','smarty_function_message');
+ $this->setPlugin('uniqid','function','smarty_function_uniqid');
+ $this->setPlugin('select','function','smarty_function_select');
+ $this->setPlugin('checkbox_list','function','smarty_function_checkbox_list');
+ $this->setPlugin('form_name','function','smarty_function_form_name');
+ $this->setPlugin('form_input','function','smarty_function_form_input');
+ $this->setPlugin('form_submit','function','smarty_function_form_submit');
+ $this->setPlugin('url','function','smarty_function_url');
+ $this->setPlugin('csrfid','function','smarty_function_csrfid');
+
+ // default blocks
+ $this->setPlugin('form','block','smarty_block_form');
+
+ $this->engine->setSmartyPluginList($this->getPluginList());
+ }
+
+
+}
+
+/**
+ * TemplateParser_Ethna
+ */
+class TemplateParser_Ethna extends TemplateParser
+{
+ /**
+ * smarty_function list
+ * @var array
+ * @access protected
+ */
+ var $smarty_plugin_list = array();
+
+ /**
+ * fake property for Smaty
+ *
+ * @access public
+ */
+ var $_tag_stack = array();
+
+ /**
+ * setSmartyPluginList
+ *
+ */
+ function setSmartyPluginList($plugin_list)
+ {
+ if (!is_array($plugin_list)) {
+ return false;
+ }
+ $this->smarty_plugin_list = $plugin_list;
+ }
+
+ /**
+ * getSmartyPluginList
+ *
+ * @access public
+ */
+ function getSmartyPluginList()
+ {
+ return $this->smarty_plugin_list;
+ }
+
+ /**
+ * smarty_function dispatcher
+ *
+ * @access protected
+ * @param string $src
+ */
+ function _exec9002_smartyfunctions($src){
+
+ $tag = new SimpleTag();
+ $smarty_plugin_list = $this->getSmartyPluginList();
+
+ foreach($smarty_plugin_list as $name => $plugin_config) {
+
+ while ($tag->set($src, $this->_getTagName($name))) {
+
+ if ($plugin_config['type'] == 'function') {
+
+ $param = $tag->toHash();
+ $src = str_replace(
+ $tag->getPlain(),
+ $plugin_config['plugin']($param, $this),
+ $src
+ );
+
+ } else if ($plugin_config['type'] == 'block') {
+
+ $repeat_before = true;
+ $repeat_after = false;
+ $param_list = $tag->getParameter();
+ foreach ($param_list as $param_tag) {
+ $param[$param_tag->getName()] = $param_tag->getValue();
+ }
+ $content = $tag->getValue();
+
+ //before(not return value)
+ $result = $plugin_config['plugin']($param, $content, $this, $repeat_before);
+
+ //after
+ $result = $plugin_config['plugin']($param, $content, $this, $repeat_after);
+ $src = str_replace(
+ $tag->getPlain(),
+ $result,
+ $src
+ );
+
+ }
+ }
+
+ }
+
+ return $src;
+ }
+
+}
+
+?>
'type' => VAR_TYPE_STRING,
),
- // minimun input(ascii)
+ // minimun input
'string_min_input' => array(
'required' => true,
'type' => VAR_TYPE_STRING,
'min' => 5,
),
- // maximum input(ascii)
+ // maximum input
'string_max_input' => array(
'required' => true,
'type' => VAR_TYPE_STRING,
'max' => 5,
),
-
+
// regexp input
'string_regexp_input' => array(
'required' => true,
'type' => VAR_TYPE_STRING,
'regexp' => '/^b+$/',
),
-
- // minimum input(multibyte)
- 'string_min_input_multi' => array(
- 'required' => true,
- 'type' => VAR_TYPE_STRING,
- 'min' => 5,
- ),
-
- // maximum input(multibyte)
- 'string_max_input_multi' => array(
- 'required' => true,
- 'type' => VAR_TYPE_STRING,
- 'max' => 5,
- ),
-
- // regexp input(mutlibyte)
- 'string_regexp_input_multi' => array(
- 'required' => true,
- 'type' => VAR_TYPE_STRING,
- 'mbregexp' => '/^う+$/',
- 'mbregexp_encoding' => 'UTF-8',
- ),
);
}
// }}}
// }}}
-// {{{ Test Default ActionForm Classes
-// {{{ Test_Default_ActionForm_Integer
-/**
- * integer type filling default value Test ActionForm
- *
- * @access private
- */
-class Test_Default_ActionForm_Integer extends Ethna_ActionForm
-{
- var $form = array(
-
- //
- // integer type.
- //
-
- // integer type error
- 'integer_type_error_input' => array(
- 'required' => false,
- 'type' => VAR_TYPE_INT,
- 'default' => 99,
- ),
-
- // required input
- 'integer_required_input' => array(
- 'required' => true,
- 'type' => VAR_TYPE_INT,
- 'default' => 100,
- ),
-
- // minimun input
- 'integer_min_input' => array(
- 'required' => true,
- 'type' => VAR_TYPE_INT,
- 'min' => 100,
- 'default' => 101,
- ),
-
- // maximum input
- 'integer_max_input' => array(
- 'required' => true,
- 'type' => VAR_TYPE_INT,
- 'max' => 100,
- 'default' => 102,
- ),
-
- // regexp input
- 'integer_regexp_input' => array(
- 'required' => true,
- 'type' => VAR_TYPE_INT,
- 'regexp' => '/^\d+$/',
- 'default' => 103,
- ),
-
- );
-
-}
-// }}}
-
-// {{{ Test_Default_ActionForm_WithPlugin_Integer
-/**
- * integer type filling default value Test ActionForm
- * with use_validator_plugin flag is on.
- *
- * @access private
- */
-class Test_Default_ActionForm_WithPlugin_Integer extends Test_Default_ActionForm_Integer
-{
- var $use_validator_plugin = true;
-}
-// }}}
-
-// {{{ Test_Default_ActionForm_Float
-/**
- * default float value type Test ActionForm
- *
- * @access private
- */
-class Test_Default_ActionForm_Float extends Ethna_ActionForm
-{
- var $form = array(
-
- //
- // float type.
- //
-
- // float type error
- 'float_type_error_input' => array(
- 'required' => false,
- 'type' => VAR_TYPE_FLOAT,
- 'default' => 99.1,
- ),
-
- // required input
- 'float_required_input' => array(
- 'required' => true,
- 'type' => VAR_TYPE_FLOAT,
- 'default' => 100.1,
- ),
-
- // minimun input
- 'float_min_input' => array(
- 'required' => true,
- 'type' => VAR_TYPE_FLOAT,
- 'min' => 100.1,
- 'default' => 101.1,
- ),
-
- // maximum input
- 'float_max_input' => array(
- 'required' => true,
- 'type' => VAR_TYPE_FLOAT,
- 'max' => 100.1,
- 'default' => 102.1,
- ),
-
- // regexp input
- 'float_regexp_input' => array(
- 'required' => true,
- 'type' => VAR_TYPE_FLOAT,
- 'regexp' => '/^\d+\.\d+$/',
- 'default' => 103.1,
- ),
-
- );
-
-}
-// }}}
-
-// {{{ Test_Default_ActionForm_WithPlugin_Float
-/**
- * float type filling default value Test ActionForm
- * with use_validator_plugin flag is on.
- *
- * @access private
- */
-class Test_Default_ActionForm_WithPlugin_Float extends Test_Default_ActionForm_Float
-{
- var $use_validator_plugin = true;
-}
-// }}}
-
-// {{{ Test_Default_ActionForm_String
-/**
- * default string value type Test ActionForm
- *
- * @access private
- */
-class Test_Default_ActionForm_String extends Ethna_ActionForm
-{
- var $form = array(
-
- //
- // string type.
- //
-
- // required input
- 'string_required_input' => array(
- 'required' => true,
- 'type' => VAR_TYPE_STRING,
- 'default' => 'a',
- ),
-
- // minimun input
- 'string_min_input' => array(
- 'required' => true,
- 'type' => VAR_TYPE_STRING,
- 'min' => 5,
- 'default' => 'b',
- ),
-
- // maximum input
- 'string_max_input' => array(
- 'required' => true,
- 'type' => VAR_TYPE_STRING,
- 'max' => 5,
- 'default' => 'c',
- ),
-
- // regexp input
- 'string_regexp_input' => array(
- 'required' => true,
- 'type' => VAR_TYPE_STRING,
- 'regexp' => '/^b+$/',
- 'default' => 'd',
- ),
- );
-
-}
-// }}}
-
-// {{{ Test_Default_ActionForm_WithPlugin_String
-/**
- * string type filling default value Test ActionForm
- * with use_validator_plugin flag is on.
- *
- * @access private
- */
-class Test_Default_ActionForm_WithPlugin_String extends Test_Default_ActionForm_String
-{
- var $use_validator_plugin = true;
-}
-// }}}
-
-// {{{ Test_Default_ActionForm_Datetime
-/**
- * default datetime value type Test ActionForm
- *
- * @access private
- */
-class Test_Default_ActionForm_Datetime extends Ethna_ActionForm
-{
- var $form = array(
-
- //
- // datetime type.
- //
-
- // required input
- 'datetime_required_input' => array(
- 'required' => true,
- 'type' => VAR_TYPE_DATETIME,
- 'default' => '2000-01-01',
- ),
-
- // minimun input
- 'datetime_min_input' => array(
- 'required' => true,
- 'type' => VAR_TYPE_DATETIME,
- 'min' => '2010-10-01',
- 'default' => '2000-01-02',
- ),
-
- // maximum input
- 'datetime_max_input' => array(
- 'required' => true,
- 'type' => VAR_TYPE_DATETIME,
- 'max' => '2000-01-05',
- 'default' => '2000-01-03',
- ),
-
- // regexp input
- 'datetime_regexp_input' => array(
- 'required' => true,
- 'type' => VAR_TYPE_DATETIME,
- 'regexp' => '/^\d{4}-\d{2}-\d{2}$/',
- 'default' => '2000-01-04',
- ),
- );
-
-}
-// }}}
-
-// {{{ Test_Default_ActionForm_Boolean
-/**
- * default boolean value type Test ActionForm
- *
- * @access private
- */
-class Test_Default_ActionForm_Boolean extends Ethna_ActionForm
-{
- var $form = array(
-
- //
- // boolean type.
- //
-
- // required input
- 'boolean_required_input' => array(
- 'required' => true,
- 'type' => VAR_TYPE_BOOLEAN,
- 'default' => true,
- ),
- );
-
-}
-// }}}
-
-// {{{ Test_Default_ActionForm_WithPlugin_Boolean
-/**
- * boolean type filling default value Test ActionForm
- * with use_validator_plugin flag is on.
- *
- * @access private
- */
-class Test_Default_ActionForm_WithPlugin_Boolean extends Test_Default_ActionForm_Boolean
-{
- var $use_validator_plugin = true;
-}
-// }}}
-
-
-// {{{ Test_Default_ActionForm_WithPlugin_Datetime
-/**
- * datetime type filling default value Test ActionForm
- * with use_validator_plugin flag is on.
- *
- * @access private
- */
-class Test_Default_ActionForm_WithPlugin_Datetime extends Test_Default_ActionForm_Datetime
-{
- var $use_validator_plugin = true;
-}
-// }}}
-
-
-// {{{ Test_Default_ActionForm_File
-/**
- * default file value type Test ActionForm
- *
- * @access private
- */
-class Test_Default_ActionForm_File extends Ethna_ActionForm
-{
- var $form = array(
- //
- // file input
- // this must be ignored!
- //
- 'file_input' => array(
- 'required' => true,
- 'type' => VAR_TYPE_FILE,
- 'default' => 'hoge', // this must be ignored.
- ),
- );
-
-}
-// }}}
-
-// {{{ Test_Default_ActionForm_WithPlugin_File
-/**
- * float type filling default value Test ActionForm
- * with use_validator_plugin flag is on.
- *
- * @access private
- */
-class Test_Default_ActionForm_WithPlugin_File extends Test_Default_ActionForm_Float
-{
- var $use_validator_plugin = true;
-}
-// }}}
-// }}}
-
// {{{ Ethna_ActionForm_Test
/**
* Test Case For Ethna_ActionForm
'string_required_input' => 'aaaa',
'string_min_input' => 'aaaaaaa',
'string_max_input' => 'bbb',
- 'string_min_input_multi' => 'ああああああ',
- 'string_max_input_multi' => 'あああ',
'string_regexp_input' => 'bbb',
- 'string_regexp_input_multi' => 'ううう',
);
- $this->_run('string', $normal_input, false);
+ $this->_run('string', $normal_input);
// error test value
$error_input = array(
'string_required_input' => null,
'string_min_input' => 'a',
'string_max_input' => 'bbbbbbbbbbbbbb',
- 'string_min_input_multi' => 'あ',
- 'string_max_input_multi' => 'ああああああああああああ',
'string_regexp_input' => 'aaa',
- 'string_regexp_input_multi' => 'ええええ',
);
$this->_run('string', $error_input, true);
}
// }}}
- // {{{ test_Validate_Integer_Default
- function test_Validate_Integer_Default()
- {
- // test value
- $input = array(
- 'integer_type_error_input' => 'aaaa',
- 'integer_required_input' => null,
- 'integer_min_input' => -10000,
- 'integer_max_input' => 10000,
- 'integer_regexp_input' => 'aaa',
- );
- $expected = array(
- 'integer_type_error_input' => 99,
- 'integer_required_input' => 100,
- 'integer_min_input' => 101,
- 'integer_max_input' => 102,
- 'integer_regexp_input' => 103,
- );
-
- $this->_run_default_value_test('integer', $input, $expected);
- }
- // }}}
-
- // {{{ test_Validate_Float_Default
- function test_Validate_Float_Default()
- {
- // test value
- $input = array(
- 'float_type_error_input' => 'aaaa',
- 'float_required_input' => null,
- 'float_min_input' => -10000,
- 'float_max_input' => 10000,
- 'float_regexp_input' => 'aaa',
- );
- $expected = array(
- 'float_type_error_input' => 99.1,
- 'float_required_input' => 100.1,
- 'float_min_input' => 101.1,
- 'float_max_input' => 102.1,
- 'float_regexp_input' => 103.1,
- );
-
- $this->_run_default_value_test('float', $input, $expected);
- }
- // }}}
-
- // {{{ test_Validate_String_Default
- function test_Validate_String_Default()
- {
- // test value
- $input = array(
- 'string_required_input' => null,
- 'string_min_input' => 'a',
- 'string_max_input' => 'bbbbbbbbbbbbbb',
- 'string_regexp_input' => 'aaa',
- );
- $expected = array(
- 'string_required_input' => 'a',
- 'string_min_input' => 'b',
- 'string_max_input' => 'c',
- 'string_regexp_input' => 'd',
- );
-
- $this->_run_default_value_test('string', $input, $expected);
- }
- // }}}
-
- // {{{ test_Validate_DateTime_Default
- function test_Validate_Datetime_Default()
- {
- // test value
- $input = array(
- 'datetime_required_input' => null,
- 'datetime_min_input' => '2007-01-01',
- 'datetime_max_input' => '2007-01-01',
- 'datetime_regexp_input' => '2007a-11-ga',
- );
- $expected = array(
- 'datetime_required_input' => '2000-01-01',
- 'datetime_min_input' => '2000-01-02',
- 'datetime_max_input' => '2000-01-03',
- 'datetime_regexp_input' => '2000-01-04',
- );
-
-
- $this->_run_default_value_test('datetime', $input, $expected);
- }
- // }}}
-
- // {{{ test_Validate_Boolean_Default
- function test_Validate_Boolean_Default()
- {
- // test value
- $input = array(
- 'boolean_required_input' => null,
- );
- $expected = array(
- 'boolean_required_input' => true,
- );
-
- $this->_run_default_value_test('boolean', $input, $expected);
- }
- // }}}
-
// {{{ private utility functions
function _run($type_name, $input, $test_error = false)
{
function _assert($input, $ae, $test_error = false)
{
-
foreach($input as $key => $value)
{
- $result = true;
if ($test_error) {
- $result = $this->assertTrue($ae->isError($key));
+ $this->assertTrue($ae->isError($key));
} else {
- $result = $this->assertFalse($ae->isError($key));
- }
-
- if (!$result) {
- $this->dump("Input Key: $key "
- . "Input : $value "
- . "error_test: " . var_export($test_error, true)
- );
+ $this->assertFalse($ae->isError($key));
}
}
}