Location: PHPKode > scripts > Redstart Templating > Redstart-Templating-master/Redstart/Templating/PHP/PHPBlocks.php
<?php

namespace Redstart\Templating\PHP;

/**
 * PHP Blocks
 *
 * @package Redstart    
 * @subpackage Redstart_Templating_Php
 *
 * @author Hyyan Abo Fakher
 * @since Redstart 1.0
 * @version 1.0
 */
class PHPBlocks {

    public $blocks = array();
    protected $openBlocks = array();
    public $supercall = array();
    protected $superkey = null;
    protected $compiler = null;

    /**
     * Constrcut New PHPBlock
     * @param \Redstart\Templating\PHP\PHPCompiler $compiler
     */
    public function __construct(PHPCompiler $compiler) {
        $this->compiler = $compiler;
    }

    /**
     * Start new php block
     * @param string $name block name
     * @throws \LogicException if the block name is already started or declared
     */
    public function block($name) {
        if (in_array($name, $this->openBlocks)) {
            throw new \LogicException(
            sprintf('Block Name "%s" Is Already Started', $name)
            );
        }
        if (
                $this->hasBlock($name) &&
                (
                $this->compiler->getDecoratedTemplate() === null &&
                $this->compiler->wasDecorateRequest() === false
                )
        ) {
            throw new \LogicException(
            sprintf('Can Not Redeclare ("%s") Block In The ("%s")'
                    , $name, $this->compiler->getCurrentTemplate())
            );
        }
        $this->openBlocks[] = $name;
        ob_start();
        ob_implicit_flush(0);
    }

    /**
     * End last open block
     * @throws \LogicException if there is no open block
     */
    public function endBlock() {
        if (!$this->openBlocks) {
            throw new \LogicException('No Open Block ');
        }
        $name = array_pop($this->openBlocks);
        $content = trim(ob_get_clean());
        if ($this->compiler->wasDecorateRequest()) {
            if ($this->hasBlock($name)) {
                if (isset($this->supercall[$name])) {
                    unset($this->supercall[$name]);
                }

                $content = trim(str_replace(
                                $this->superkey
                                , $content
                                , trim($this->getBlockContent($name))
                ));
            }
        }
        $this->setBlockContent($name, $content);
        echo trim($this->getBlockContent($name));
    }

    /**
     * For Debug Not For Public Use
     * @return string|false false if there are no super call left or string conatins
     *                            the block's name which registered the super call
     */
    public function isLeftSuperCall() {
        if (!empty($this->supercall)) {
            $arr=array_keys($this->supercall);
            return array_shift($arr);
        }
        return false;
    }

    /**
     * Call the parent block of the current open block
     * @throws \LogicException if there is no open slot or if the slot has 
     * no parent
     */
    public function super() {
        if (!$this->openBlocks) {
            throw new \LogicException('Calling Super When No Block Is Open');
        }
        $name = $this->openBlocks[count($this->openBlocks) - 1];


        if (!$this->compiler->getDecoratedTemplate()) {
            throw new \LogicException(
            sprintf('Block "%s" Has No Parent', $name)
            );
        }

        if (!$this->superkey) {
            $this->superkey = uniqid('Super', true);
        }

        $this->supercall[$name] = $this->compiler->getDecoratedTemplate();
        echo $this->superkey;
    }

    /**
     * Check if block exist
     * @param string $name block name
     * @return boolean true if block exists false otherwise
     */
    public function hasBlock($name) {
        return isset($this->blocks[$name]);
    }

    /**
     * Set block content
     * @param string $name block name
     * @param string $content block content
     */
    protected function setBlockContent($name, $content) {
        $this->blocks[$name] = $content;
    }

    /**
     * Get block content
     * @param string $name 
     * @return string 
     */
    protected function getBlockContent($name) {
        return isset($this->blocks[$name]) ? $this->blocks[$name] : '';
    }

    /**
     * Clear all registered blocks
     */
    public function clear() {
        $this->openBlocks = array();
        $this->blocks = array();
        $this->supercall = array();
        $this->superkey = null;
    }

}

Return current item: Redstart Templating