Throw warning if <block>, cond, loop is used in template v2

This commit is contained in:
Kijin Sung 2023-10-20 21:17:38 +09:00
parent a284a1b072
commit 129a0403e4
2 changed files with 63 additions and 0 deletions

View file

@ -149,6 +149,7 @@ class TemplateParser_v2
$content = $this->_convertInlineDirectives($content);
$content = $this->_convertMiscDirectives($content);
$content = $this->_convertEchoStatements($content);
$content = $this->_addDeprecationMessages($content);
// Postprocessing.
$content = $this->_postprocess($content);
@ -917,6 +918,31 @@ class TemplateParser_v2
}
}
/**
* Add an error message if any supported v1 syntax is found.
*
* @param string $content
* @return string
*/
protected function _addDeprecationMessages(string $content): string
{
// <block>
$content = preg_replace_callback('#<block(?=\s)#', function($match) {
return $match[0] . '<?php trigger_error("block element is not supported in template v2", \E_USER_WARNING); ?>';
}, $content);
// cond, loop
$content = preg_replace_callback('#(?<=\s)(cond|loop)="([^"]+)"#', function($match) {
if ($match[1] === 'loop' && ctype_alnum($match[2]))
{
return $match[0];
}
return '<?php trigger_error("' . $match[1] . ' attribute is not supported in template v2", \E_USER_WARNING); ?>';
}, $content);
return $content;
}
/**
* Postprocessing.
*

View file

@ -1057,6 +1057,43 @@ class TemplateParserV2Test extends \Codeception\Test\Unit
$this->assertEquals($target, $this->_parse($source));
}
public function testDeprecationMessages()
{
// <block> element
$source = '<block class="foobar">';
$target = '<block<?php trigger_error("block element is not supported in template v2", \E_USER_WARNING); ?> class="foobar">';
$this->assertEquals($target, $this->_parse($source));
// cond
$source = '<div cond="$foo->isBar()"></div>';
$target = '<div <?php trigger_error("cond attribute is not supported in template v2", \E_USER_WARNING); ?>></div>';;
$this->assertEquals($target, $this->_parse($source));
// cond is OK in includes
$source = '<include src="foo.html" cond="$bar" />';
$target = '<?php if(!empty($bar)): ?><?php $__tpl = new \Rhymix\Framework\Template($this->relative_dirname, "foo.html", "html"); echo $__tpl->compile(); ?><?php endif; ?>';;
$this->assertEquals($target, $this->_parse($source));
// loop
$source = '<tr loop="$foo => $bar"></tr>';
$target = '<tr <?php trigger_error("loop attribute is not supported in template v2", \E_USER_WARNING); ?>></tr>';;
$this->assertEquals($target, $this->_parse($source));
// loop is OK in multimedia elements
$source = '<video autoplay loop="loop"></video>';
$target = '<video autoplay loop="loop"></video>';
$this->assertEquals($target, $this->_parse($source));
// Comprehensive example
$source = '<block cond="$foo" loop="$arr => $k, $v"></block>';
$target = implode('', [
'<block<?php trigger_error("block element is not supported in template v2", \E_USER_WARNING); ?> ',
'<?php trigger_error("cond attribute is not supported in template v2", \E_USER_WARNING); ?> ',
'<?php trigger_error("loop attribute is not supported in template v2", \E_USER_WARNING); ?>></block>',
]);
$this->assertEquals($target, $this->_parse($source));
}
public function testCompile()
{
// General example