mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-05-04 17:44:38 +09:00
Fix #2203 'this' in lang directive will refer to current module
This commit is contained in:
parent
3233619eac
commit
e72336c90c
7 changed files with 69 additions and 6 deletions
|
|
@ -18,6 +18,7 @@ class Template
|
||||||
*/
|
*/
|
||||||
public $config;
|
public $config;
|
||||||
public $source_type;
|
public $source_type;
|
||||||
|
public $source_name;
|
||||||
public $parent;
|
public $parent;
|
||||||
public $vars;
|
public $vars;
|
||||||
|
|
||||||
|
|
@ -184,7 +185,11 @@ class Template
|
||||||
$this->config->version = 2;
|
$this->config->version = 2;
|
||||||
$this->config->autoescape = true;
|
$this->config->autoescape = true;
|
||||||
}
|
}
|
||||||
$this->source_type = preg_match('!^((?:m\.)?[a-z]+)/!', $this->relative_dirname, $match) ? $match[1] : null;
|
if (preg_match('!^(addons|common|(?:m\.)?layouts|modules|plugins|themes|widgets|widgetstyles)/(\w+)!', $this->relative_dirname, $match))
|
||||||
|
{
|
||||||
|
$this->source_type = $match[1];
|
||||||
|
$this->source_name = $match[2];
|
||||||
|
}
|
||||||
$this->path = $this->absolute_dirname;
|
$this->path = $this->absolute_dirname;
|
||||||
$this->web_path = \RX_BASEURL . $this->relative_dirname;
|
$this->web_path = \RX_BASEURL . $this->relative_dirname;
|
||||||
$this->setCachePath();
|
$this->setCachePath();
|
||||||
|
|
@ -928,4 +933,26 @@ class Template
|
||||||
}
|
}
|
||||||
return count($args) ? in_array((string)$validator_id, $args, true) : true;
|
return count($args) ? in_array((string)$validator_id, $args, true) : true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lang shortcut for v2.
|
||||||
|
*
|
||||||
|
* @param ...$args
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
protected function _v2_lang(...$args): string
|
||||||
|
{
|
||||||
|
if (!isset($GLOBALS['lang']) || !$GLOBALS['lang'] instanceof Lang)
|
||||||
|
{
|
||||||
|
$GLOBALS['lang'] = Lang::getInstance(\Context::getLangType());
|
||||||
|
$GLOBALS['lang']->loadDirectory(\RX_BASEDIR . 'common/lang', 'common');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isset($args[0]) && !strncmp($args[0], 'this.', 5))
|
||||||
|
{
|
||||||
|
$args[0] = $this->source_name . '.' . substr($args[0], 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $GLOBALS['lang']->get(...$args);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -720,7 +720,7 @@ class TemplateParser_v2
|
||||||
'json_encode(%s, self::$_json_options) : ' .
|
'json_encode(%s, self::$_json_options) : ' .
|
||||||
'htmlspecialchars(json_encode(%s, self::$_json_options), \ENT_QUOTES, \'UTF-8\', false); ?>', $args, $args);
|
'htmlspecialchars(json_encode(%s, self::$_json_options), \ENT_QUOTES, \'UTF-8\', false); ?>', $args, $args);
|
||||||
case 'lang':
|
case 'lang':
|
||||||
return sprintf('<?php echo $this->config->context === \'JS\' ? escape_js(lang(%s)) : lang(%s); ?>', $args, $args);
|
return sprintf('<?php echo $this->config->context === \'JS\' ? escape_js($this->_v2_lang(%s)) : $this->_v2_lang(%s); ?>', $args, $args);
|
||||||
case 'dump':
|
case 'dump':
|
||||||
return sprintf('<?php ob_start(); var_dump(%s); \$__dump = ob_get_clean(); echo rtrim(\$__dump); ?>', $args);
|
return sprintf('<?php ob_start(); var_dump(%s); \$__dump = ob_get_clean(); echo rtrim(\$__dump); ?>', $args);
|
||||||
case 'dd':
|
case 'dd':
|
||||||
|
|
|
||||||
|
|
@ -84,7 +84,7 @@
|
||||||
|
|
||||||
<!-- ETC -->
|
<!-- ETC -->
|
||||||
@desktop
|
@desktop
|
||||||
<div id="rhymix_waiting" class="wfsr" data-message="@lang('msg_call_server')"></div>
|
<div id="rhymix_waiting" class="wfsr" data-message="@lang('common.msg_call_server')"></div>
|
||||||
@enddesktop
|
@enddesktop
|
||||||
<div id="rhymix_alert"></div>
|
<div id="rhymix_alert"></div>
|
||||||
<div id="rhymix_debug_panel"></div>
|
<div id="rhymix_debug_panel"></div>
|
||||||
|
|
|
||||||
3
tests/_data/template/v2lang.executed1.html
Normal file
3
tests/_data/template/v2lang.executed1.html
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
|
||||||
|
<p>comment.foobarfoobar</p>
|
||||||
|
<p>document.foobarbazbaz</p>
|
||||||
3
tests/_data/template/v2lang.executed2.html
Normal file
3
tests/_data/template/v2lang.executed2.html
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
|
||||||
|
<p>comment.foobarfoobar</p>
|
||||||
|
<p>member.foobarbazbaz</p>
|
||||||
4
tests/_data/template/v2lang.html
Normal file
4
tests/_data/template/v2lang.html
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
@version(2)
|
||||||
|
|
||||||
|
<p>@lang('comment.foobarfoobar')</p>
|
||||||
|
<p>@lang('this.foobarbazbaz')</p>
|
||||||
|
|
@ -936,17 +936,17 @@ class TemplateParserV2Test extends \Codeception\Test\Unit
|
||||||
|
|
||||||
// Lang code with variable as name
|
// Lang code with variable as name
|
||||||
$source = '@lang($var->name)';
|
$source = '@lang($var->name)';
|
||||||
$target = '<?php echo $this->config->context === \'JS\' ? escape_js(lang($__Context->var->name)) : lang($__Context->var->name); ?>';
|
$target = '<?php echo $this->config->context === \'JS\' ? escape_js($this->_v2_lang($__Context->var->name)) : $this->_v2_lang($__Context->var->name); ?>';
|
||||||
$this->assertEquals($target, $this->_parse($source));
|
$this->assertEquals($target, $this->_parse($source));
|
||||||
|
|
||||||
// Lang code with literal name and variable
|
// Lang code with literal name and variable
|
||||||
$source = "@lang('board.cmd_list_items', \$var)";
|
$source = "@lang('board.cmd_list_items', \$var)";
|
||||||
$target = "<?php echo \$this->config->context === 'JS' ? escape_js(lang('board.cmd_list_items', \$__Context->var)) : lang('board.cmd_list_items', \$__Context->var); ?>";
|
$target = "<?php echo \$this->config->context === 'JS' ? escape_js(\$this->_v2_lang('board.cmd_list_items', \$__Context->var)) : \$this->_v2_lang('board.cmd_list_items', \$__Context->var); ?>";
|
||||||
$this->assertEquals($target, $this->_parse($source));
|
$this->assertEquals($target, $this->_parse($source));
|
||||||
|
|
||||||
// Lang code with class alias
|
// Lang code with class alias
|
||||||
$source = "@use('Rhymix\Framework\Lang', 'Lang')\n" . '<p>@lang(Lang::getLang())</p>';
|
$source = "@use('Rhymix\Framework\Lang', 'Lang')\n" . '<p>@lang(Lang::getLang())</p>';
|
||||||
$target = "\n" . '<p><?php echo $this->config->context === \'JS\' ? escape_js(lang(Rhymix\Framework\Lang::getLang())) : lang(Rhymix\Framework\Lang::getLang()); ?></p>';
|
$target = "\n" . '<p><?php echo $this->config->context === \'JS\' ? escape_js($this->_v2_lang(Rhymix\Framework\Lang::getLang())) : $this->_v2_lang(Rhymix\Framework\Lang::getLang()); ?></p>';
|
||||||
$this->assertEquals($target, $this->_parse($source));
|
$this->assertEquals($target, $this->_parse($source));
|
||||||
|
|
||||||
// Dump one variable
|
// Dump one variable
|
||||||
|
|
@ -1149,6 +1149,32 @@ class TemplateParserV2Test extends \Codeception\Test\Unit
|
||||||
$list = \Context::getCssFile();
|
$list = \Context::getCssFile();
|
||||||
$this->assertStringContainsString('/rhymix/tests/_data/template/css/style.scss', array_first($list)['file']);
|
$this->assertStringContainsString('/rhymix/tests/_data/template/css/style.scss', array_first($list)['file']);
|
||||||
|
|
||||||
|
// Lang
|
||||||
|
$tmpl = new \Rhymix\Framework\Template('./tests/_data/template', 'v2lang.html');
|
||||||
|
$tmpl->source_type = 'modules';
|
||||||
|
$tmpl->source_name = 'document';
|
||||||
|
$tmpl->disableCache();
|
||||||
|
|
||||||
|
$executed_output = $tmpl->compile();
|
||||||
|
//Rhymix\Framework\Storage::write(\RX_BASEDIR . 'tests/_data/template/v2lang.executed1.html', $executed_output);
|
||||||
|
$expected = file_get_contents(\RX_BASEDIR . 'tests/_data/template/v2lang.executed1.html');
|
||||||
|
$this->assertEquals(
|
||||||
|
$this->_normalizeWhitespace($expected),
|
||||||
|
$this->_normalizeWhitespace($executed_output)
|
||||||
|
);
|
||||||
|
|
||||||
|
$tmpl->source_type = 'modules';
|
||||||
|
$tmpl->source_name = 'member';
|
||||||
|
$tmpl->disableCache();
|
||||||
|
|
||||||
|
$executed_output = $tmpl->compile();
|
||||||
|
//Rhymix\Framework\Storage::write(\RX_BASEDIR . 'tests/_data/template/v2lang.executed2.html', $executed_output);
|
||||||
|
$expected = file_get_contents(\RX_BASEDIR . 'tests/_data/template/v2lang.executed2.html');
|
||||||
|
$this->assertEquals(
|
||||||
|
$this->_normalizeWhitespace($expected),
|
||||||
|
$this->_normalizeWhitespace($executed_output)
|
||||||
|
);
|
||||||
|
|
||||||
// Loop variable
|
// Loop variable
|
||||||
$tmpl = new \Rhymix\Framework\Template('./tests/_data/template', 'v2loops.html');
|
$tmpl = new \Rhymix\Framework\Template('./tests/_data/template', 'v2loops.html');
|
||||||
$tmpl->disableCache();
|
$tmpl->disableCache();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue