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

namespace Redstart\Templating\PHP;

use Redstart\Templating\Compiler\Compiler;
use Redstart\Templating\Template;

/**
 * PHP Compiler
 *
 * @package Redstart    
 * @subpackage Redstart_Templating_Php
 *
 * @author Hyyan Abo Fakher
 * @since Redstart 1.0
 * @version 1.0
 */
class PHPCompiler extends Compiler {

    protected $currentTemplate = null;
    protected $decoratedTemplate = null;
    protected $external = false;
    protected $decorateRequest = false;
    protected $blocks = null;
    protected $blockPlugin = null;

    /**
     * Constrcut new php compiler
     * @param \Redstart\Templating\Template $template
     */
    public function __construct(Template $template) {
        parent::__construct($template);
        $this->blocks = new PHPBlocks($this);
    }

    /**
     * Get decorated template if any
     * @return string|null null if there is not any decorated template 
     */
    public function getDecoratedTemplate() {
        return $this->decoratedTemplate;
    }

    /**
     * Check if the last fetched template file registered decorate request
     * @return boolean true if the last fecthed template registered an decorate
     * request and false otherwie
     */
    public function wasDecorateRequest() {
        return $this->decorateRequest;
    }

    /**
     * Get the current template file
     * @return string
     */
    public function getCurrentTemplate() {
        return $this->currentTemplate;
    }

    /**
     * Compile the php template 
     * @param string $file file name
     * @return string the full path to the compiled php template
     */
    protected function doCompile($file) {
        $this->currentTemplate = $file;
        return $file;
    }

    /**
     * Check if the compiler supports the given file type
     * @param string $file file name
     * @return boolean true if supports false otherwise
     */
    public function isSupported($file) {
        if (false !== $pos = strrpos($file, '.')) {
            if (strtolower(substr($file, $pos + 1)) === "phtml") {
                return true;
            }
        }
        return false;
    }

    /**
     * Called by the template after the template is included in the
     * <tt>fetch</tt> scope
     * @param string $content the content of the included template
     * @return string the new content
     */
    public function fetched($content) {
        $this->decorateRequest = false;
        if (!is_null($this->decoratedTemplate)) {
            $file = $this->decoratedTemplate;
            $external = $this->external;
            $this->decoratedTemplate = null;
            $this->external = false;
            $this->decorateRequest = true;
            return $this->getTemplate()->fetch(
                            $file, $external
            );
        }
        return $content;
    }

    /**
     * Extend another template file
     * @param string $file
     * @param boolean $external
     * @throws \LogicException if more than one decorate operation were made in the
     * same scope or if the template file is trying to decorates itself
     */
    public function decorate($file, $external = false) {
        if ($this->decoratedTemplate !== null) {
            throw new \LogicException(
            'More Than One Decorate Operation Can Not Be Made In The Same Scope'
            );
        }
        if ($this->getCurrentTemplate() === $file) {
            throw new \LogicException(
            sprintf('Self Extend In ("%s")', $file)
            );
        }
        $this->decoratedTemplate = $file;
        $this->external = $external;
    }

    /**
     * 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() {
        $this->blocks->super();
    }

    /**
     * Start new php block
     * @param string $name block name
     * @throws \LogicException if the block name is already started or declared
     */
    public function block($name) {
        $this->blocks->block($name);
    }

    /**
     * End last open block
     * @throws \LogicException if there is no open block
     */
    public function endBlock() {
        $this->blocks->endBlock();
    }

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

    /** Install the php compiler */
    public function install() {
        if (!$this->blockPlugin) {
            $this->blockPlugin = new BlockPlugin($this);
        }
        $this->getTemplate()->addPlugin($this->blockPlugin);
    }

    /**  Uninstall the php compiler */
    public function uninstall() {
        $this->getTemplate()->removePlugin($this->blockPlugin->getName());
    }

    /**
     * Reset the compiler params after the template is fetched 
     * @throws \RuntimeException if there are blocks supercall left
     */
    public function finalize() {
        if (($name = $this->blocks->isLeftSuperCall())) {
            throw new \RuntimeException(
            sprintf('(%s) Has No Parent', $name)
            );
        }
        $this->decorateRequest = false;
        $this->decoratedTemplate = null;
        $this->external = false;
        $this->currentTemplate = null;
        $this->blocks->clear();
    }

}
Return current item: Redstart Templating