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

namespace Redstart\Templating;

use Redstart\Templating\Path\PathListInterface;
use Redstart\Templating\Path\PathList;
use Redstart\Templating\Compiler\CompilerInterface;
use Redstart\Templating\PHP\PHPCompiler;
use Redstart\Templating\Placeholder\PlaceholderInterface;
use Redstart\Templating\Placeholder\ObjectPlaceholder;
use Redstart\Templating\Plugin\PluginListInterface;
use Redstart\Templating\Plugin\PluginList;
use Redstart\Templating\Plugin\PluginInterface;
use Redstart\Templating\Core\Plugin\Html;
use Redstart\Templating\Escape\EscapeComponent;

/**
 * Template
 *
 * @package Redstart    
 * @subpackage Redstart_Templating
 *
 * @author Hyyan Abo Fakher
 * @since Redstart 1.0
 * @version 1.0
 */
class Template extends EscapeComponent implements \ArrayAccess {

    private $pathlist;
    private $pluginlist;
    private $placeholder;
    private $compiler;
    private $extract;

    /**
     * Constrcut New Template
     */
    public function __construct() {
        parent::__construct();
        $this->setExtract(false)
                ->addEscape('htmlentities')
                ->setPlaceholder(new ObjectPlaceholder())
                ->setPluginList(new PluginList())
                ->setPathList(new PathList())
                ->setCompiler(new PHPCompiler($this))
                ->setPlugin(array(new Html($this)));
    }

    /**
     * Turns extraction of variables on/off
     * @param boolean $flag True to turn on extraction, false to turn off.
     * @return \Redstart\Templating\Template
     */
    public function setExtract($flag) {
        $this->extract = (boolean) $flag;
        return $this;
    }

    /**
     * Check if extraction of variables is on
     * @return boolean 
     */
    public function isExtract() {
        return $this->extract;
    }

    /**
     * Set path list
     * @param \Redstart\Templating\Path\PathListInterface $list
     * @return \Redstart\Templating\Template
     */
    public function setPathList(PathListInterface $list) {
        $this->pathlist = $list;
        return $this;
    }

    /**
     * Get path list
     * @return \Redstart\Templating\Path\PathListInterface Description
     */
    public function getPathList() {
        return $this->pathlist;
    }

    /**
     * Add Path 
     * @param string $path
     * @return \Redstart\Templating\Template
     */
    public function addPath($path) {
        $this->getPathList()->addPath($path);
        return $this;
    }

    /**
     * Add array of paths
     * @param array $paths
     * @return \Redstart\Templating\Template
     */
    public function setPath(array $paths) {
        $this->getPathList()->setPath($paths);
        return $this;
    }

    /**
     * Find file's full path
     * @param string $file file path relative to the source path
     * @return string|boolean false if file can't be found 
     */
    public function find($file) {
        return $this->getPathList()->find($file);
    }

    /**
     * Set plugin list 
     * @param \Redstart\Templating\Plugin\PluginListInterface $list
     * @return \Redstart\Templating\Template
     */
    public function setPluginList(PluginListInterface $list) {
        $this->pluginlist = $list;
        return $this;
    }

    /**
     * Get plugin list
     * @return \Redstart\Templating\Plugin\PluginListInterface 
     */
    public function getPluginList() {
        return $this->pluginlist;
    }

    /**
     * Add plugin to the plugin list
     * 
     * @param \Redstart\Templating\Plugin\PluginInterface $plugin
     * 
     * @return \Redstart\Templating\Template
     * 
     * @throws \LogicException
     */
    public function addPlugin(PluginInterface $plugin) {
        $this->getPluginList()->addPlugin($plugin);
        return $this;
    }

    /**
     * Set array of plugins
     * 
     * @see Template::addPlugin()
     * 
     * @param array $plugins
     * @return \Redstart\Templating\Template
     */
    public function setPlugin(array $plugins) {
        $this->getPluginList()->setPlugin($plugins);
        return $this;
    }

    /**
     * Remove plugin from the plugin list
     * @param string $name plugin name
     * @return \Redstart\Templating\Template
     */
    public function removePlugin($name) {
        $this->getPluginList()->remove($name);
        return $this;
    }

    /**
     * Get plugin 
     * @param string $name
     * @return \Redstart\Templating\Plugin\PluginInterface 
     */
    public function plugin($name) {
        return $this->getPluginList()->get($name);
    }

    /**
     * Call Plugin
     * @param string $name
     * @param string $arguments never used
     * @throws \RuntimeException if the plugin is not registered
     */
    public function __call($name, $arguments) {
        if (!$this->getPluginList()->has($name)) {
            throw new \RuntimeException(
            sprintf('(%s) Is Not Registered Plugin', $name)
            );
        };
        return $this->plugin($name);
    }

    /**
     * Check if plugin exists in the plugin list
     * @param type $name
     * @return type
     */
    public function offsetExists($name) {
        return $this->getPluginList()->has($name);
    }

    /**
     * Get plugin 
     * 
     * alias for <tt>plugin()</tt> method
     * 
     * @see Template::plugin()
     * 
     * @param string $name
     * @return \Redstart\Templating\Plugin\PluginInterface 
     */
    public function offsetGet($name) {
        return $this->plugin($name);
    }

    /**
     * Add plugin to the plugin list
     * 
     * alias for <tt>addPlugin()</tt> method
     * 
     * @see Template::addPlugin()
     * 
     * @param string $key always null
     * @param \Redstart\Templating\Plugin\PluginInterface|array $plugin plugin
     * object or array of pugins objects
     * 
     * @throws \LogicException
     */
    public function offsetSet($key, $plugin) {
        if (is_array($plugin)) {
            $this->setPlugin($plugin);
        } else {
            $this->addPlugin($plugin);
        }
    }

    /**
     * Remove plugin from the plugin list
     * @param string $name plugin name
     */
    public function offsetUnset($name) {
        $this->removePlugin($name);
    }

    /**
     * Set placeholder
     * @param \Redstart\Templating\Placeholder\PlaceholderInterface $holder
     * @return \Redstart\Templating\Template
     */
    public function setPlaceholder(PlaceholderInterface $holder) {
        $this->placeholder = $holder;
        return $this;
    }

    /**
     * Get placeholder
     * @return type
     */
    public function getPlaceholder() {
        return $this->placeholder;
    }

    /**
     * Assign new param to the template's placeholder
     *
     * @param string $name
     * @param string $value
     * @return \Redstart\Templating\Template
     */
    public function assign($name, $value) {
        $this->getPlaceholder()->set($name, $value);
        return $this;
    }

    /**
     * Get param from the place holder
     * @param string $name
     * @return mixed
     */
    public function __get($name) {
        return $this->getPlaceholder()->get($name);
    }

    /**
     * Add param to the place holder
     * 
     * @see Engine::assign()
     * 
     * @param string $name
     * @param string $value
     */
    public function __set($name, $value) {
        $this->assign($name, $value);
    }

    /**
     * Remove param from the place holder
     * @param type $name
     */
    public function __unset($name) {
        $this->getPlaceholder()->remove($name);
    }

    /**
     * Check if param is in the place holder
     * @param string $name
     * @return boolean
     */
    public function __isset($name) {
        return $this->getPlaceholder()->has($name);
    }

    /**
     * Set compiler
     * @param \Redstart\Templating\Compiler\CompilerInterface $complier
     * @return \Redstart\Templating\Template
     */
    public function setCompiler(CompilerInterface $complier) {
        if ($this->compiler) {
            $this->compiler->uninstall();
        }
        $complier->install();
        $this->compiler = $complier;
        return $this;
    }

    /**
     * Get compoler
     * @return \Redstart\Templating\Compiler\CompilerInterface
     */
    public function getCompiler() {
        return $this->compiler;
    }

    /**
     * Load template file
     *
     * @param string $file 
     * 
     * @return string The full path to the <b>compiled</b> template script.
     *
     * @throws \RuntimeException if template file can't be loacted
     * @throws \Exception
     */
    public function load($file) {
        if (!($path = $this->find($file))) {
            throw new \RuntimeException(
            sprintf('Template File (%s) Can Not Be Located', $file)
            );
        }
        return $this->getCompiler()->compile($path);
    }

    /**
     * Load external template file ( path list will not be used) 
     *
     * @param string $file file's path
     * 
     * @return string The full path to the <b>compiled</b> template script.
     *
     * @throws \RuntimeException if template file can't be loacted
     * @throws \Exception
     */
    public function eload($file) {
        if (file_exists($file)) {
            return $this->getCompiler()->compile($file);
        }
        throw new \RuntimeException(
        sprintf('Template File (%s) Can Not Be Located', $file)
        );
    }

    /**
     * Executes a template script and returns the results as a string. 
     * 
     * @param string $file 
     * @param boolean $external if false the <tt>load</tt> method will be used
     *                          if true  the <tt>eload</tt> method will be used
     *                          where default is false
     * @return string The output of the the template script.
     * 
     * @throws \RuntimeException if template file can't be loacted
     * @throws \Exception
     */
    public function fetch($file, $external = false) {
        ob_start();

        if ($this->isExtract()) {
            extract($this->getPlaceholder()->all(), EXTR_SKIP);
        }

        if ($external === false) {
            include $this->load($file);
        } else {
            include $this->eload($file);
        }

        $content = $this->getCompiler()->fetched(ob_get_clean());

        $this->getCompiler()->finalize();

        return $content;
    }

    /**
     * Display template
     * 
     * @return \Redstart\Templating\Template
     * @throws \RuntimeException if template file can't be loacted
     * @throws \Exception
     */
    public function display($file) {
        echo $this->fetch($file);
        return $this;
    }

}
Return current item: Redstart Templating