Fixed all inheritance issues

This commit is contained in:
Simon Wisselink
2023-01-23 11:45:29 +01:00
parent 7c02bb9a63
commit f1c3ac4395
5 changed files with 38 additions and 18 deletions

View File

@@ -103,7 +103,9 @@ class ExtendsTag extends Inheritance {
$inlineUids = ''; $inlineUids = '';
if (isset($template) && $compiler->getSmarty()->merge_compiled_includes) { if (isset($template) && $compiler->getSmarty()->merge_compiled_includes) {
$code = $compiler->compileTag('include', [$template, ['scope' => 'parent']]); $code = $compiler->compileTag('include', [$template, ['scope' => 'parent']]);
if (preg_match('/([,][\s]*[\'][a-z0-9]+[\'][,][\s]*[\']content.*[\'])[)]/', $code, $match)) {
// @TODO this relies on the generated code to have a certain format and is sure to break someday
if (preg_match('/(,\s*\'[a-z0-9]+\',\s*\'content.*\')/', $code, $match)) {
$inlineUids = $match[1]; $inlineUids = $match[1];
} }
} }

View File

@@ -222,9 +222,10 @@ class IncludeTag extends Base {
if (isset($_assign)) { if (isset($_assign)) {
$_output .= "ob_start();\n"; $_output .= "ob_start();\n";
} }
$_output .= "\$_smarty_tpl->_subTemplateRender({$fullResourceName}, {$_cache_id}, \$_smarty_tpl->compile_id, $_output .= "\$_smarty_tpl->renderSubTemplate({$fullResourceName}, {$_cache_id}, \$_smarty_tpl->compile_id, " .
{$_caching}, {$_cache_lifetime}, {$_vars}, '{$compiler->getParentCompiler()->mergedSubTemplatesData[$uid][$t_hash]['uid']}', "{$_caching}, {$_cache_lifetime}, {$_vars}, " .
'{$compiler->getParentCompiler()->mergedSubTemplatesData[$uid][$t_hash]['func']}', (int) {$_scope});\n"; "'{$compiler->getParentCompiler()->mergedSubTemplatesData[$uid][$t_hash]['uid']}', " .
"'{$compiler->getParentCompiler()->mergedSubTemplatesData[$uid][$t_hash]['func']}', (int) {$_scope});\n";
if (isset($_assign)) { if (isset($_assign)) {
$_output .= "\$_smarty_tpl->assign({$_assign}, ob_get_clean(), false, {$_scope});\n"; $_output .= "\$_smarty_tpl->assign({$_assign}, ob_get_clean(), false, {$_scope});\n";
} }
@@ -239,7 +240,8 @@ class IncludeTag extends Base {
if (isset($_assign)) { if (isset($_assign)) {
$_output .= "ob_start();\n"; $_output .= "ob_start();\n";
} }
$_output .= "\$_smarty_tpl->_subTemplateRender({$fullResourceName}, $_cache_id, \$_smarty_tpl->compile_id, $_caching, $_cache_lifetime, $_vars, null, null, (int) {$_scope});\n"; $_output .= "\$_smarty_tpl->renderSubTemplate({$fullResourceName}, $_cache_id, \$_smarty_tpl->compile_id, " .
"$_caching, $_cache_lifetime, $_vars, null, null, (int) {$_scope});\n";
if (isset($_assign)) { if (isset($_assign)) {
$_output .= "\$_smarty_tpl->assign({$_assign}, ob_get_clean(), false, {$_scope});\n"; $_output .= "\$_smarty_tpl->assign({$_assign}, ob_get_clean(), false, {$_scope});\n";
} }

View File

@@ -24,42 +24,42 @@ class InheritanceRuntime {
* *
* @var int * @var int
*/ */
public $state = 0; private $state = 0;
/** /**
* Array of root child {block} objects * Array of root child {block} objects
* *
* @var \Smarty\Runtime\Block[] * @var \Smarty\Runtime\Block[]
*/ */
public $childRoot = []; private $childRoot = [];
/** /**
* inheritance template nesting level * inheritance template nesting level
* *
* @var int * @var int
*/ */
public $inheritanceLevel = 0; private $inheritanceLevel = 0;
/** /**
* inheritance template index * inheritance template index
* *
* @var int * @var int
*/ */
public $tplIndex = -1; private $tplIndex = -1;
/** /**
* Array of template source objects * Array of template source objects
* *
* @var Source[] * @var Source[]
*/ */
public $sources = []; private $sources = [];
/** /**
* Stack of source objects while executing block code * Stack of source objects while executing block code
* *
* @var Source[] * @var Source[]
*/ */
public $sourceStack = []; private $sourceStack = [];
/** /**
* Initialize inheritance * Initialize inheritance
@@ -71,6 +71,7 @@ class InheritanceRuntime {
public function init(Template $tpl, $initChild, $blockNames = []) { public function init(Template $tpl, $initChild, $blockNames = []) {
// if called while executing parent template it must be a sub-template with new inheritance root // if called while executing parent template it must be a sub-template with new inheritance root
if ($initChild && $this->state === 3 && (strpos($tpl->template_resource, 'extendsall') === false)) { if ($initChild && $this->state === 3 && (strpos($tpl->template_resource, 'extendsall') === false)) {
$tpl->setInheritance(clone $tpl->getSmarty()->getRuntime('Inheritance'));
$tpl->getInheritance()->init($tpl, $initChild, $blockNames); $tpl->getInheritance()->init($tpl, $initChild, $blockNames);
return; return;
} }
@@ -112,7 +113,7 @@ class InheritanceRuntime {
$this->state = 2; $this->state = 2;
} }
if (isset($template)) { if (isset($template)) {
$tpl->_subTemplateRender( $tpl->renderSubTemplate(
$template, $template,
$tpl->cache_id, $tpl->cache_id,
$tpl->compile_id, $tpl->compile_id,

View File

@@ -105,6 +105,10 @@ class Template extends TemplateBase {
*/ */
private $right_delimiter = null; private $right_delimiter = null;
/**
* @var InheritanceRuntime|null
*/
private $inheritance;
/** /**
* Create template data object * Create template data object
@@ -242,7 +246,7 @@ class Template extends TemplateBase {
* *
* @throws Exception * @throws Exception
*/ */
public function _subTemplateRender( public function renderSubTemplate(
$template_name, $template_name,
$cache_id, $cache_id,
$compile_id, $compile_id,
@@ -258,6 +262,7 @@ class Template extends TemplateBase {
$tpl = $this->getSmarty()->createTemplate($template_name, $cache_id, $compile_id, $this, $caching, $cache_lifetime, $baseFilePath); $tpl = $this->getSmarty()->createTemplate($template_name, $cache_id, $compile_id, $this, $caching, $cache_lifetime, $baseFilePath);
$tpl->setCached($this->getCached()); // re-use the same Cache object across subtemplates to gather hashes and file dependencies. $tpl->setCached($this->getCached()); // re-use the same Cache object across subtemplates to gather hashes and file dependencies.
$tpl->setInheritance($this->getInheritance()); // re-use the same Inheritance object inside the inheritance tree
if ($scope) { if ($scope) {
$tpl->setDefaultScope($scope); $tpl->setDefaultScope($scope);
@@ -512,7 +517,21 @@ class Template extends TemplateBase {
* @throws Exception * @throws Exception
*/ */
public function getInheritance(): InheritanceRuntime { public function getInheritance(): InheritanceRuntime {
return $this->getSmarty()->getRuntime('Inheritance'); if (is_null($this->inheritance)) {
$this->inheritance = clone $this->getSmarty()->getRuntime('Inheritance');
}
return $this->inheritance;
}
/**
* Sets a new InheritanceRuntime object.
*
* @param InheritanceRuntime $inheritanceRuntime
*
* @return void
*/
public function setInheritance(InheritanceRuntime $inheritanceRuntime) {
$this->inheritance = $inheritanceRuntime;
} }
/** /**

View File

@@ -10,10 +10,6 @@ use Smarty\Template;
/** /**
* class for block extends compiler tests * class for block extends compiler tests
*
*
* @preserveGlobalState disabled
*
*/ */
class CompileBlockExtendsTest extends PHPUnit_Smarty class CompileBlockExtendsTest extends PHPUnit_Smarty
{ {