/**
* Stacks blocks into each other.
*
* The first block found is always the container,
* all other blocks either to append, replace or prepend
* to/the first block.
*
* @param Node $node the block node to handle
*
* @return $this
*/
protected function handleBlock(Node $node)
{
if (!$node->name || $node->mode === 'ignore') {
//Will be handled through compileBlock when the loop encounters it
return $this;
}
//Find all other blocks with that name
foreach ($this->blocks as $block) {
if ($block === $node || $block->name !== $node->name) {
continue;
}
$mode = $block->mode;
//detach from parent
$block->parent->remove($block);
switch ($mode) {
default:
/** @noinspection PhpMissingBreakStatementInspection */
/** @noinspection PhpMissingBreakStatementInspection */
case 'replace':
$node->children = [];
//WANTED FALLTHROUGH!
//WANTED FALLTHROUGH!
case 'append':
//Append to master block
foreach ($block->children as $child) {
$block->remove($child);
$node->append($child);
}
break;
case 'prepend':
$last = null;
foreach ($block->children as $child) {
$block->remove($child);
if (!$last) {
$node->prepend($child);
$last = $child;
continue;
}
$node->insertAfter($last, $child);
$last = $child;
}
break;
}
$block->mode = 'ignore';
}
return $this;
}