<?php
 
/* --------------------------------------------------------------------------
 
 * XIRE - eXtendable Information Rendering Engine
 
 * --------------------------------------------------------------------------
 
 * LICENSE
 
 * Copyright (C) 2006  David Duong
 
 *
 
 * This library is free software; you can redistribute it and/or
 
 * modify it under the terms of the GNU Lesser General Public
 
 * License as published by the Free Software Foundation; either
 
 * version 2.1 of the License, or (at your option) any later version.
 
 *
 
 * This library is distributed in the hope that it will be useful,
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
 * Lesser General Public License for more details.
 
 *
 
 * You should have received a copy of the GNU Lesser General Public
 
 * License along with this library; if not, write to the Free Software
 
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
 * -------------------------------------------------------------------------- */
 
define('XIRL_NAMESPACE', 'http://ns.forizon.com/XIRL');
 
include 'Process.class.php';
 
 
/**
 
 * A template
 
 */
 
class XIRE_Template extends ArrayObject {
 
    private $document;
 
    private $variables;
 
    private $filename;
 
    private $loaded;
 
 
    public function __construct ($filename) {
 
        $this->document = new DomDocument();
 
        $this->document->preserveWhiteSpace = false;
 
        $this->variables = array();
 
        $this->filename = (string)$filename;
 
        $this->loaded = false;
 
        parent::__construct(&$this->variables);
 
    }
 
 
    public function __get ($name) {
 
        // Read-only members hack
 
        if ($name === 'document') {
 
            return $this->document;
 
        } elseif ($name === 'filename') {
 
            return $this->filename;
 
        }
 
    }
 
 
    public function load () {
 
        if (@!$this->document->load($this->filename)) {
 
            throw new XIRE_InvalidFileException(
 
              "Template file could not be loaded: $this->filename");
 
        }
 
        $this->loaded = true;
 
    }
 
 
    /**
 
     * Load template from file
 
     *
 
     * @param string $filename the path to the template file
 
     * @throws XIRE_InvalidFileException if the given template could not be loaded
 
     */
 
    public function process () {
 
        $this->loaded or $this->load();
 
        // Instantiate a processor and process document element
 
        $process = $this->createProcess();
 
        $process->enqueue($this->document->documentElement);
 
        $process->process();
 
    }
 
 
    /**
 
     * Creates a XIRE_Process that belongs to the template
 
     */
 
    public function createProcess () {
 
        return new XIRE_Process($this);
 
    }
 
 
    /**
 
     * @return bool whether a template variable is set
 
     */
 
    public function has ($name) {
 
        return isset($this->variables[$name]);
 
    }
 
 
    /**
 
     * Set a template variable to a value
 
     */
 
    public function set ($name, $value) {
 
        $this->variables[$name] = $value;
 
    }
 
 
    /**
 
     * @return the value of a template variable
 
     */
 
    public function get ($name, $default = null) {
 
        if (isset($this->variables[$name])) {
 
            return $this->variables[$name];
 
        } elseif (func_num_args() > 1) {
 
            return $default;
 
        }
 
        throw new XIRE_MissingVariableException("Missing template variable: $name");
 
    }
 
 
    /**
 
     * Reference to a variable with a template variable
 
     */
 
    public function reference ($name, &$variable) {
 
        $this->variables[$name] =& $variable;
 
    }
 
 
    /**
 
     * @return a reference to a template variable
 
     */
 
    public function &get_reference ($name) {
 
        return $this->variables[$name];
 
    }
 
 
    /**
 
     * @return string the XML representation of the DOM
 
     */
 
    public function getXML () {
 
        return $this->document->saveXML();
 
    }
 
 
    public function save ($filename) {
 
        return $this->document->save($filename);
 
    }
 
}
 
 
class XIRE_Exception extends Exception{}
 
class XIRE_InvalidFileException extends XIRE_Exception{}
 
class XIRE_MissingVariableException extends XIRE_Exception
 
{
 
    private $name;
 
 
    public function __construct($name) {
 
        $this->name = $name;
 
        parent::__construct("Template is missing required variable: $name");
 
    }
 
 
    /**
 
     * @return the name of the missing required attribute
 
     */
 
    public function getName () {
 
        return $this->name;
 
    }
 
}
 
?>
 
 |