PHP Classes

Buffer tree: Manage ordered collections of data items

Recommend this page to a friend!
  Info   View files Example   View files View files (7)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-01-09 (4 days ago) RSS 2.0 feedNot enough user ratingsTotal: 157 This week: 1All time: 8,980 This week: 348Up
Version License PHP version Categories
buffer-tree 1.17GNU Lesser Genera...5.2PHP 5, Data types
Description 

Author

This package can manage ordered collections of data items.

It provides a class can that can append, replace and find arbitrary values in a buffer.

Another class can perform operations to manage a tree of buffers like entering a new buffer, activate a previous buffer, write data to the current buffer, or find a buffer by name.

Picture of Roger Baklund
  Performance   Level  
Name: Roger Baklund <contact>
Classes: 7 packages by
Country: Norway Norway
Innovation award
Innovation award
Nominee: 4x

Example

<?php

include '../buffer_manager.class.php';

class
HTML_Tags extends buffer_manager {
 
# a typical usage pattern:
 
function WriteTo($name,$data) {
   
$this->enter($name);
   
$this->out($data);
   
$this->leave();
  }
 
# generic named HTML tag, content is a separate buffer
 
function NamedTag($name,$tag=false,$attrs='',$data=NULL) {
    if(!
$tag) $tag = $name;
    if(
$attrs) $attrs = " $attrs";
   
$this->out("<$tag$attrs>");
   
$this->enter($name);
    if(!
is_null($data))
     
$this->out($data);
   
$this->leave();
   
$this->out("</$tag>");
  }
 
# a named image template, src attribute can be written later
 
function NamedImage($name,$src='',$alt='',$extra='') {
   
$this->out('<img src="');
   
$this->enter($name);
    if(
$src) $this->out($src);
   
$this->leave();
    if(
$extra) $extra = " $extra";
   
$this->out('" alt="'.$alt.'"'.$extra.' />');
  }

}

$bm = new HTML_Tags('document');

$doc_node = $bm->current(); # used for debug dump below

$bm->out('<html><head>');

$bm->enter('head'); # create buffer 'head'
$bm->NamedTag('title');
$bm->NamedTag('style','','type="text/css"');
$bm->leave(); # leave 'head'

$bm->out('</head><body>');

$bm->enter('body'); # create buffer 'body'

$NameOfPage = 'Hello world';
$bm->out("<h1>$NameOfPage</h1>"); # writing in 'body'...
$bm->WriteTo('title',$NameOfPage); # ...and to the title in head

$bm->NamedImage('ProfilePic',
 
'http://files.phpclasses.org/picture/user/',
 
'PHP Classes profile picture',
 
'width="90" height="117" style="float:left;padding:.5em"');

$bm->NamedTag('Source','div','class="Source"','<p>Source:</p>');
$bm->WriteTo('style', 'div.Source {
  width:50%;
  border:solid 1px black;
  float:right;
  padding:.5em;
}'
);

$bm->out('<p>This is a small example of how you can use the '.
        
'buffer manager to navigate in a tree of buffers.</p>');
$bm->out('<p>Check the source to the right.</p>');
$bm->out('<p>The HTML_Tags class extends the buffer_manager class, '.
        
'adding a few methods relevant to HTML. ');
$bm->out('<p>Note that these are just examples of how to '.
        
'use buffers as templates for HTML, use your imagination!</p>');

$bm->out('<h2>Debug dump:</h2><p>Overview of buffer structure for debug purpouses.</p>');
$bm->NamedTag('DebugDump','div');

$bm->WriteTo('style', 'p {font-family:Verdana, sans-serif;}');

$bm->WriteTo('Source',highlight_file(__FILE__,true));

$bm->leave(); # leave 'body'
$bm->out('</body></html>');

# final adjustments before output
$bm->WriteTo('ProfilePic','930196.jpg');
$bm->WriteTo('DebugDump',$doc_node->dump());

echo (string)
$bm->current();

?>


Details

Buffer tree

Two PHP classes for creating and manipulating a hierarchy of output buffers.

The class buffer represents a single buffer, it has a name, a list of content and an optional separator. The list of content consists of other buffers and data. The following methods are defined:

append($data)   -- append data (including buffer or array) to this buffer
clear()         -- clear this buffer
replace($data)  -- replace this buffer with new data
find($name)     -- find a named buffer within this buffer
dump()          -- make visual output of this buffer (for debugging)
__toString()    -- output buffer content as a string

The second class buffer_manager has methods for managing a stack of buffers.

current()       -- return current buffer instance
enter($name)    -- activate named buffer, if it does not exist: create and append to current buffer
leave()         -- leave current buffer, activate previous active buffer
out($data)      -- write data (string, buffer or array) to current active buffer
undo()          -- remove current (newly created) buffer
find($name)     -- find named buffer within root buffer

There are two demo files provided for each class, showing examples of how these classes can be used.

  • buffer_demo_1: Creating and manipulating a table of integers, using different separators
  • buffer_demo_2: Creating HTML output, using buffers as templates
  • buffer_manager_demo_1: Using the buffer manager for better control of HTML output
  • buffer_manager_demo_2: More advanced examples of HTML template usage

Note that the first example is very different from the others. The buffer class is very generic, it does not need to contain strings as values. However the __toString() method will automatically cast each content member to a string. This means you can use any scalars and even objects as data items, as long as they can be cast to strings, i.e. they have a __toString() method.


  Files folder image Files  
File Role Description
Files folder imagedemo (4 files)
Plain text file buffer.class.php Class Defines the buffer class
Plain text file buffer_manager.class.php Class Defines the buffer_manager class
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  demo  
File Role Description
  Accessible without login Plain text file buffer_demo_1.php Example Example script
  Accessible without login Plain text file buffer_demo_2.php Example Example script
  Accessible without login Plain text file buffer_manager_demo_1.php Example Example script
  Accessible without login Plain text file buffer_manager_demo_2.php Example Example script

 Version Control Unique User Downloads Download Rankings  
 100%
Total:157
This week:1
All time:8,980
This week:348Up