PHP Classes

File: lib/plugins/If.class.php

Recommend this page to a friend!
  Classes of David N. Q. Duong   XIRE   lib/plugins/If.class.php   Download  
File: lib/plugins/If.class.php
Role: Class source
Content type: text/plain
Description: if plugin
Class: XIRE
Template engine extensible with plug-in classes
Author: By
Last change: minor changes
Date: 18 years ago
Size: 2,831 bytes
 

Contents

Class file image Download
<?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
 * -------------------------------------------------------------------------- */
/**
 * Provides branching functionality
 */
class XIRE_Plugin_If extends XIRE_Plugin {
    private
$branched;
   
// Only allow DOMElement
    // @todo support DOMAttr
   
public function __construct (DOMElement $node, XIRE_Process $sender) {
       
parent::__construct($node, $sender);
       
$this->branched = false;
    }

    public function
execute () {
       
$current = $this->node;
       
// Seek other else-if or else elements for a suitable branch
       
do {
           
$next = $current->nextSibling;
            if (!
$this->branched) {
                if (
$current->localName === 'else') {
                   
$this->branch($current);
                } elseif (
$current->childNodes->length > 0) {
                   
// @TODO improve this
                   
$this->getVariable_cascade($current->getAttribute('variable')) and $this->branch($current);
                }
            }
           
$current->parentNode->removeChild($current); // Remove the current node
           
$current = $next;
        } while(
$current instanceof DOMNode
         
&& $current->namespaceURI === XIRL_NAMESPACE
         
&& ($current->localName === 'else-if'
         
|| $current->localName === 'else'));
    }

    public function
branch (DOMNode $branch) {
       
// Proccess the child nodes
       
$process = $this->template->createProcess();
        foreach (
$branch->childNodes as $node) {
           
$process->enqueue($node);
        }
       
// Process the nodes
       
$process->process();
        while (
$branch->hasChildNodes()) {
           
$branch->parentNode->insertBefore($branch->firstChild, $branch);
        }
       
$this->branched = true;
    }
}

?>