Source for file Buffer.class.php

Documentation is available at Buffer.class.php

  1. <?php
  2. /**
  3.  * Gumbo Library Framework
  4.  *
  5.  * LICENSE
  6.  * This library is being released under the terms of the New BSD License.  A
  7.  * copy of the license is packaged with the software (LICENSE.txt).  If no
  8.  * copy is found, a copy of the license template can be found at:
  9.  * http://www.opensource.org/licenses/bsd-license.php
  10.  * 
  11.  * @category Gumbo
  12.  * @package Buffer
  13.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  14.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  15.  * @author Michael Luster <mluster79@yahoo.com>
  16.  * @link http://sourceforge.net/projects/phpgumbo
  17.  * @version 0.0.1
  18.  */
  19.  
  20. /**
  21.  * Buffer Class
  22.  * 
  23.  * This class is simply meant to provide an OOP interface for
  24.  * creating a PHP buffer.  It starts on construction, and ends
  25.  * when the object is destroyed.  The methods will perform
  26.  * basic operations on the buffer.
  27.  * 
  28.  * This class does not perform advanced functionality on output
  29.  * buffers.
  30.  * 
  31.  * To use the buffer, simply create a new instance of the object
  32.  * when the buffer should start.  Send any content to the browser,
  33.  * the buffer will catch.  Once the buffer is finished, simply
  34.  * destroy the object:
  35.  * <pre>
  36.  * $buffer = new Gumbo_Buffer ();
  37.  * // insert output code
  38.  * // ...
  39.  * $buffer = null; or unset ($buffer);
  40.  * </pre>
  41.  *
  42.  * @category Gumbo
  43.  * @package Buffer
  44.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  45.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  46.  * @author Michael Luster <mluster79@yahoo.com>
  47.  * @link http://sourceforge.net/projects/phpgumbo
  48.  * @desc Buffer Class
  49.  * @version 0.0.1
  50.  */
  51.  
  52. gumbo_load ("Interface_Buffer");
  53.  
  54. class Gumbo_Buffer implements Gumbo_Interface_Buffer {
  55.     
  56.     /**
  57.      * Constructor - starts the buffer
  58.      * @postcondition ob_start ()
  59.      */
  60.     public function __construct ({
  61.         ob_start ();
  62.     }
  63.     
  64.     /**
  65.      * Destructor - ends the buffer
  66.      * $buffer = null; or unset ($buffer);
  67.      * @postcondition ob_end_clean ()
  68.      */
  69.     public function __destruct ({
  70.         ob_end_clean ();
  71.     }
  72.     
  73.     
  74.     
  75.     /** ACTION METHODS **/
  76.     /**
  77.      * Cleans the output buffer: ob_clean ()
  78.      */
  79.     public function clean ({
  80.         ob_clean ();
  81.     }
  82.     
  83.     /**
  84.      * Flushes the contents of the buffer to the browser: ob_flush ()
  85.      */
  86.     public function flush ({
  87.         ob_flush ();
  88.     }
  89.     
  90.     
  91.     
  92.     /** ACCESSOR METHODS **/
  93.     /**
  94.      * Returns the contents of the buffer: ob_get_contents ()
  95.      * @param bool $clean cleans the buffer after getting the contents
  96.      * @return string 
  97.      */
  98.     public function contents ($clean=false{
  99.         try {
  100.             // verify precondition
  101.             if (!is_bool ($clean)) {
  102.                 throw new Gumbo_Exception ("Invalid Argument 'clean:bool' => {$clean}:gettype ($clean));
  103.             }
  104.         catch (Gumbo_Exception $e{
  105.             $e->setFunction (__METHOD__);
  106.             gumbo_trigger ($e);
  107.             $clean false;
  108.         }
  109.         
  110.         $txt ob_get_contents ();
  111.         if ($clean$this->clean ()}
  112.         
  113.         return $txt;
  114.     }
  115.     
  116.     /**
  117.      * Returns the size of the buffer: ob_get_length ()
  118.      * @return int 
  119.      */
  120.     public function size ({
  121.         return ob_get_length ();
  122.     }
  123.     
  124. }
  125.  
  126. ?>