- improve inheritance code

This commit is contained in:
uwetews
2015-12-27 04:02:21 +01:00
parent 7e7591f6c0
commit dc6da4384b
5 changed files with 52 additions and 47 deletions

View File

@@ -1,4 +1,7 @@
 ===== 3.1.30-dev ===== (xx.xx.xx)
27.12.2015
- improve inheritance code
25.12.2015
- compile {block} tag code and its processing into classes
- optimization replace hhvm extension by inline code

View File

@@ -118,7 +118,7 @@ class Smarty extends Smarty_Internal_TemplateBase
/**
* smarty version
*/
const SMARTY_VERSION = '3.1.30-dev/9';
const SMARTY_VERSION = '3.1.30-dev/10';
/**
* define variable scopes

View File

@@ -67,54 +67,61 @@ class Smarty_Internal_Block
/**
* Smarty_Internal_Block constructor.
* - if outer level {block} of child template ($state == 1) save it as child root block
* - otherwise process inheritance and render
*
* @param \Smarty_Internal_Template $_smarty_tpl
*
* @param \Smarty_Internal_Template $tpl
* @param int|null $tplIndex index of outer level {block} if nested
*/
public function __construct(Smarty_Internal_Template $_smarty_tpl)
public function __construct(Smarty_Internal_Template $tpl, $tplIndex = null)
{
$this->tplIndex = $_smarty_tpl->ext->_inheritance->tplIndex;
if (isset($_smarty_tpl->ext->_inheritance->blockParameter[ $this->name ])) {
$this->child = $_smarty_tpl->ext->_inheritance->blockParameter[ $this->name ];
$this->tplIndex = $tplIndex ? $tplIndex : $tpl->ext->_inheritance->tplIndex;
if (isset($tpl->ext->_inheritance->childRoot[ $this->name ])) {
$this->child = $tpl->ext->_inheritance->childRoot[ $this->name ];
}
if ($_smarty_tpl->ext->_inheritance->state == 1) {
$_smarty_tpl->ext->_inheritance->blockParameter[ $this->name ] = $this;
if ($tpl->ext->_inheritance->state == 1) {
$tpl->ext->_inheritance->childRoot[ $this->name ] = $this;
return;
}
$this->process($_smarty_tpl);
// make sure we got child block of child template of current block
while ($this->child && $this->tplIndex <= $this->child->tplIndex) {
$this->child = $this->child->child;
}
$this->process($tpl);
}
/**
* Goto child block or render this
*
* @param \Smarty_Internal_Template $_smarty_tpl
* @param \Smarty_Internal_Template $tpl
* @param \Smarty_Internal_Block|null $parent
*/
public function process(Smarty_Internal_Template $_smarty_tpl, Smarty_Internal_Block $parent = null) {
public function process(Smarty_Internal_Template $tpl, Smarty_Internal_Block $parent = null)
{
if ($this->hide && !isset($this->child)) {
return;
return;
}
if (isset($this->child) && $this->child->hide && !isset($this->child->child)) {
$this->child = null;
}
$this->parent = $parent;
if ($this->append && !$this->prepend && isset($parent)) {
$this->callParent($_smarty_tpl);
$this->callParent($tpl);
}
if ($this->callsChild || !isset($this->child) || ($this->child->hide && !isset($this->child->child))) {
$this->callBlock($_smarty_tpl);
$this->callBlock($tpl);
} else {
$this->child->process($_smarty_tpl, $this);
$this->child->process($tpl, $this);
}
if ($this->prepend && isset($parent)) {
$this->callParent($_smarty_tpl);
$this->callParent($tpl);
if ($this->append) {
if ($this->callsChild || !isset($this->child) || ($this->child->hide && !isset($this->child->child))) {
$this->callBlock($_smarty_tpl);
$this->callBlock($tpl);
} else {
$this->child->process($_smarty_tpl, $this);
$this->child->process($tpl, $this);
}
}
}
}
$this->parent = null;
}
@@ -122,34 +129,37 @@ class Smarty_Internal_Block
/**
* Compiled block code overloaded by {block} class
*
* @param \Smarty_Internal_Template $_smarty_tpl
* @param \Smarty_Internal_Template $tpl
*/
public function callBlock(Smarty_Internal_Template $_smarty_tpl) {
public function callBlock(Smarty_Internal_Template $tpl)
{
}
/**
* Render child on {$smarty.block.child}
*
* @param \Smarty_Internal_Template $_smarty_tpl
* @param \Smarty_Internal_Template $tpl
*/
public function callChild (Smarty_Internal_Template $_smarty_tpl) {
public function callChild(Smarty_Internal_Template $tpl)
{
if (isset($this->child)) {
$this->child->process($_smarty_tpl, $this);
$this->child->process($tpl, $this);
}
}
/**
* Render parent on {$smarty.block.parent} or {block append/prepend} *
*
* @param \Smarty_Internal_Template $_smarty_tpl
* @param \Smarty_Internal_Template $tpl
*
* @throws \SmartyException
*/
public function callParent (Smarty_Internal_Template $_smarty_tpl) {
public function callParent(Smarty_Internal_Template $tpl)
{
if (isset($this->parent)) {
$this->parent->callBlock($_smarty_tpl, $this->parent->parent);
$this->parent->callBlock($tpl);
} else {
throw new SmartyException("inheritance: illegal {\$smarty.block.parent} or {block append/prepend} used in parent template '{$_smarty_tpl->ext->_inheritance->compiledFilePath[$this->tplIndex]}' block '{$this->name}'");
throw new SmartyException("inheritance: illegal {\$smarty.block.parent} or {block append/prepend} used in parent template '{$tpl->ext->_inheritance->templateResource[$this->tplIndex]}' block '{$this->name}'");
}
}
}

View File

@@ -235,7 +235,7 @@ class Smarty_Internal_Compile_Blockclose extends Smarty_Internal_Compile_Shared_
if ($compiler->_cache[ 'blockNesting' ] == 1) {
$output .= "new {$_className}(\$_smarty_tpl);\n";
} else {
$output .= "new {$_className}(\$_smarty_tpl);\n";
$output .= "new {$_className}(\$_smarty_tpl, \$this->tplIndex);\n";
}
$output .= "?>\n";
$compiler->_cache[ 'blockNesting' ] --;

View File

@@ -24,11 +24,11 @@ class Smarty_Internal_Runtime_Inheritance
public $state = 0;
/**
* Array of block parameter of known {block} tags
* Array of root child {block} objects
*
* @var array
* @var Smarty_Internal_Block[]
*/
public $blockParameter = array();
public $childRoot = array();
/**
* inheritance template nesting level
@@ -45,20 +45,12 @@ class Smarty_Internal_Runtime_Inheritance
public $tplIndex = - 1;
/**
* Array of compiled template file path
* Array of source template names
* - key template index
* only used when caching is enabled
*
* @var []string
* @var string[]
*/
public $compiledFilePath = array();
/**
* Current {block} nesting level
*
* @var int
*/
public $blockNesting = 0;
public $templateResource = array();
/**
* Initialize inheritance
@@ -70,8 +62,8 @@ class Smarty_Internal_Runtime_Inheritance
*/
public function init(Smarty_Internal_Template $tpl, $initChild, $blockNames = array())
{
// if template was from an inner block or template is a parent template create new inheritance root
if ($initChild && ($this->blockNesting || $this->state == 3)) {
// if called while executing parent template it must be a sub-template with new inheritance root
if ($initChild && $this->state == 3) {
$tpl->ext->_inheritance = new Smarty_Internal_Runtime_Inheritance();
$tpl->ext->_inheritance->init($tpl, $initChild, $blockNames);
return;
@@ -88,7 +80,7 @@ class Smarty_Internal_Runtime_Inheritance
// in parent state {include} will not increment template index
if ($this->state != 3) {
$this->tplIndex ++;
$this->compiledFilePath[ $this->tplIndex ] = $tpl->template_resource;
$this->templateResource[ $this->tplIndex ] = $tpl->template_resource;
}
// if state was waiting for parent change state to parent
if ($this->state == 2) {