Source for file Tree.php
Documentation is available at Tree.php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2002 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Alan Knowles <alan@akbkhome.com> |
// +----------------------------------------------------------------------+
// $Id: Tree.php 334846 2014-09-12 04:50:56Z alan_k $
// The Html Tree Component of Flexy
// Designed to be used on it's own
// - it builds a big tokens[] array :
// - filters "Text with {placeholders}" into sprintf formated strings.
// - matches closers to openers eg. token[4]->close = &token[5];
// - it aliases the tokens into token[0] as children as a tree
// - locates the base of the file. (flexy:startchildren.
class HTML_Template_Flexy_Tree {
* 'ignore' => dont change {xxxX} into placeholders?
* 'filename' => filename of file being parsed. (for error messages.)
* 'ignore_html' => return <html> elements as strings.
* 'ignore_php' => DELETE/DESTROY any php code in the original template.
'ignore' => false , // was flexyIgnore
* Array of all tokens (eg. nodes / text / tags etc. )
* children = array( &tag[1] );
* [1] => Token_Text::'some test'
* under normal situations, the tree is built into node[0], the remaining nodes are referenced by alias.
* if caching is used (the nodes > 0 will not exist, and everything will just be a child of node 0.
* Run a Tokenizer and Store its results and return the tree.
* It should build a DOM Tree of the HTML
* @param string $data data to parse.
* @param array $options see options array.
* @return base token (really a dummy token, which contains the tree)
function construct ($data,$options=array ())
if (isset ($GLOBALS[__CLASS__ ]['cache'][$md5])) {
return $GLOBALS[__CLASS__ ]['cache'][$md5];
$t = new HTML_Template_Flexy_Tree;
$t->options = $t->options + $options;
require_once 'HTML/Template/Flexy/Token.php';
$t->tokens = array (new HTML_Template_Flexy_Token );
$r = $t->tokenize ($data);
//new Gtk_VarDump($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][0]);
$GLOBALS[__CLASS__ ]['cache'][$md5] = $t->returnStart ();
return $GLOBALS[__CLASS__ ]['cache'][$md5];
* The core tokenizing part - runs the tokenizer on the data,
* and stores the results in $this->tokens[]
* @param string Data to tokenize
* @return none | PEAR::Error
* @see see also methods.....
function tokenize ($data) {
require_once 'HTML/Template/Flexy/Tokenizer.php';
$tokenizer = &HTML_Template_Flexy_Tokenizer ::construct ($data,$this->options);
// initialize state - this trys to make sure that
// you dont do to many elses etc.
//echo "RUNNING TOKENIZER";
// step one just tokenize it.
while ($t = $tokenizer->yylex ()) {
return HTML_Template_Flexy ::staticRaiseError (
"HTML_Template_Flexy_Tree::Syntax error in File: %s (Line %s)\n".
"Context:\n\n%s\n\n >>>>>> %s\n",
$this->options['filename'], $tokenizer->yyline ,
if ($t->token == 'Php') {
$this->tokens[$i] = $tokenizer->value;
$this->tokens[$i]->id = $i;
//print_r($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]);
* Match the opening and closing tags eg. </B> is the closer of <B>
* aliases the ->close to the tokens[{closeid}] element
$total = count($this->tokens);
// connect open and close tags.
// this is done by having a stack for each of the tag types..
// then removing it when it finds the closing one
// <a href=""><img src=""></a>
// ends up with a stack for <a>'s and a stack for <img>'s
for($i=1; $i< $total; $i++ ) {
//echo "Checking TAG $i\n";
if (!isset ($res[$i]->tag )) {
if ($tag{0 } != '/') { // it's not a close tag..
if (!isset ($stack[$tag])) {
$npos = $stack[$tag]['pos'] = 0;
$npos = ++ $stack[$tag]['pos'];
$stack[$tag][$npos] = $i;
//echo "GOT END TAG: {$res[$i]->tag}\n";
if (!isset ($stack[$tag]['pos'])) {
$npos = $stack[$tag]['pos'];
if (!isset ($stack[$tag][$npos])) {
// alias closer to opener..
$this->tokens[$stack[$tag][$npos]]->close = &$this->tokens[$i];
// take it off the stack so no one else uses it!!!
unset ($stack[$tag][$npos]);
if ($stack[$tag]['pos'] < 0 ) {
// too many closes - just ignore it..
// create a dummy close for the end
$this->tokens[$i] = new HTML_Template_Flexy_Token;
$this->tokens[$i]->id = $total;
$this->tokens[0 ]->close = &$this->tokens[$i];
// now is it possible to connect children...
// now we need to GLOBALIZE!! -
* Build the child array for each element.
* does not move tokens, just aliases the child nodes into the token array.
* @param int id of node to add children to.
function buildChildren ($id)
$base = &$this->tokens[$id];
$base->children = array ();
for ($i= $start; $i< $end; $i++ ) {
//echo "{$base->id}:{$base->tag} ADDING {$i}{$_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]->tag}<BR>";
//if ($base->id == 1176) {
// echo "<PRE>";print_r($_HTML_TEMPLATE_FLEXY_TOKEN['tokens'][$i]);
$base->children [] = &$this->tokens[$i];
if (isset ($this->tokens[$i]->close )) {
// if the close id is greater than my id - ignore it! -
if ($this->tokens[$i]->close ->id > $end) {
$this->buildChildren ($i);
$i = $this->tokens[$i]->close ->id;
* Locates Flexy:startchildren etc. if it is used.
* and returns the base of the tree. (eg. otherwise token[0].
* @return HTML_Template_Flexy_Token (base of tree.)
case isset ($this->tokens[$i]->ucAttributes ['FLEXYSTART']):
case isset ($this->tokens[$i]->ucAttributes ['FLEXY:START']):
$this->tokens[$i]->removeAttribute ('FLEXY:START');
$this->tokens[$i]->removeAttribute ('FLEXYSTART');
return $this->tokens[$i];
case isset ($this->tokens[$i]->ucAttributes ['FLEXYSTARTCHILDREN']):
case isset ($this->tokens[$i]->ucAttributes ['FLEXY:STARTCHILDREN']):
$this->tokens[0 ]->children = $this->tokens[$i]->children;
Documentation generated on Mon, 11 Mar 2019 16:00:04 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|