From f1c3ac4395e94c35cfe064c23cf3477f992de703 Mon Sep 17 00:00:00 2001 From: Simon Wisselink Date: Mon, 23 Jan 2023 11:45:29 +0100 Subject: [PATCH] Fixed all inheritance issues --- src/Compile/Tag/ExtendsTag.php | 4 +++- src/Compile/Tag/IncludeTag.php | 10 ++++---- src/Runtime/InheritanceRuntime.php | 15 ++++++------ src/Template.php | 23 +++++++++++++++++-- .../BockExtend/CompileBlockExtendsTest.php | 4 ---- 5 files changed, 38 insertions(+), 18 deletions(-) diff --git a/src/Compile/Tag/ExtendsTag.php b/src/Compile/Tag/ExtendsTag.php index 26dd7d15..f49efaac 100644 --- a/src/Compile/Tag/ExtendsTag.php +++ b/src/Compile/Tag/ExtendsTag.php @@ -103,7 +103,9 @@ class ExtendsTag extends Inheritance { $inlineUids = ''; if (isset($template) && $compiler->getSmarty()->merge_compiled_includes) { $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]; } } diff --git a/src/Compile/Tag/IncludeTag.php b/src/Compile/Tag/IncludeTag.php index 99971c36..06750534 100644 --- a/src/Compile/Tag/IncludeTag.php +++ b/src/Compile/Tag/IncludeTag.php @@ -222,9 +222,10 @@ class IncludeTag extends Base { if (isset($_assign)) { $_output .= "ob_start();\n"; } - $_output .= "\$_smarty_tpl->_subTemplateRender({$fullResourceName}, {$_cache_id}, \$_smarty_tpl->compile_id, - {$_caching}, {$_cache_lifetime}, {$_vars}, '{$compiler->getParentCompiler()->mergedSubTemplatesData[$uid][$t_hash]['uid']}', - '{$compiler->getParentCompiler()->mergedSubTemplatesData[$uid][$t_hash]['func']}', (int) {$_scope});\n"; + $_output .= "\$_smarty_tpl->renderSubTemplate({$fullResourceName}, {$_cache_id}, \$_smarty_tpl->compile_id, " . + "{$_caching}, {$_cache_lifetime}, {$_vars}, " . + "'{$compiler->getParentCompiler()->mergedSubTemplatesData[$uid][$t_hash]['uid']}', " . + "'{$compiler->getParentCompiler()->mergedSubTemplatesData[$uid][$t_hash]['func']}', (int) {$_scope});\n"; if (isset($_assign)) { $_output .= "\$_smarty_tpl->assign({$_assign}, ob_get_clean(), false, {$_scope});\n"; } @@ -239,7 +240,8 @@ class IncludeTag extends Base { if (isset($_assign)) { $_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)) { $_output .= "\$_smarty_tpl->assign({$_assign}, ob_get_clean(), false, {$_scope});\n"; } diff --git a/src/Runtime/InheritanceRuntime.php b/src/Runtime/InheritanceRuntime.php index 1d4f0218..c1070a85 100644 --- a/src/Runtime/InheritanceRuntime.php +++ b/src/Runtime/InheritanceRuntime.php @@ -24,42 +24,42 @@ class InheritanceRuntime { * * @var int */ - public $state = 0; + private $state = 0; /** * Array of root child {block} objects * * @var \Smarty\Runtime\Block[] */ - public $childRoot = []; + private $childRoot = []; /** * inheritance template nesting level * * @var int */ - public $inheritanceLevel = 0; + private $inheritanceLevel = 0; /** * inheritance template index * * @var int */ - public $tplIndex = -1; + private $tplIndex = -1; /** * Array of template source objects * * @var Source[] */ - public $sources = []; + private $sources = []; /** * Stack of source objects while executing block code * * @var Source[] */ - public $sourceStack = []; + private $sourceStack = []; /** * Initialize inheritance @@ -71,6 +71,7 @@ class InheritanceRuntime { public function init(Template $tpl, $initChild, $blockNames = []) { // 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)) { + $tpl->setInheritance(clone $tpl->getSmarty()->getRuntime('Inheritance')); $tpl->getInheritance()->init($tpl, $initChild, $blockNames); return; } @@ -112,7 +113,7 @@ class InheritanceRuntime { $this->state = 2; } if (isset($template)) { - $tpl->_subTemplateRender( + $tpl->renderSubTemplate( $template, $tpl->cache_id, $tpl->compile_id, diff --git a/src/Template.php b/src/Template.php index 85ef2be3..db2edd18 100644 --- a/src/Template.php +++ b/src/Template.php @@ -105,6 +105,10 @@ class Template extends TemplateBase { */ private $right_delimiter = null; + /** + * @var InheritanceRuntime|null + */ + private $inheritance; /** * Create template data object @@ -242,7 +246,7 @@ class Template extends TemplateBase { * * @throws Exception */ - public function _subTemplateRender( + public function renderSubTemplate( $template_name, $cache_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->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) { $tpl->setDefaultScope($scope); @@ -512,7 +517,21 @@ class Template extends TemplateBase { * @throws Exception */ 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; } /** diff --git a/tests/UnitTests/TemplateSource/TagTests/BockExtend/CompileBlockExtendsTest.php b/tests/UnitTests/TemplateSource/TagTests/BockExtend/CompileBlockExtendsTest.php index 373a1985..a10211c3 100644 --- a/tests/UnitTests/TemplateSource/TagTests/BockExtend/CompileBlockExtendsTest.php +++ b/tests/UnitTests/TemplateSource/TagTests/BockExtend/CompileBlockExtendsTest.php @@ -10,10 +10,6 @@ use Smarty\Template; /** * class for block extends compiler tests - * - * - * @preserveGlobalState disabled - * */ class CompileBlockExtendsTest extends PHPUnit_Smarty {