Source for file Buffer.php

Documentation is available at Buffer.php

  1. <?php
  2. // vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 fdm=marker encoding=utf8 :
  3. /**
  4.  * Pxxo - build self-supported and interoperable Web graphical components
  5.  * 
  6.  * Copyright (c) 2008, Nicolas Thouvenin
  7.  *
  8.  * All rights reserved.
  9.  *
  10.  * Redistribution and use in source and binary forms, with or without
  11.  * modification, are permitted provided that the following conditions are met:
  12.  *
  13.  *     * Redistributions of source code must retain the above copyright
  14.  *       notice, this list of conditions and the following disclaimer.
  15.  *     * Redistributions in binary form must reproduce the above copyright
  16.  *       notice, this list of conditions and the following disclaimer in the
  17.  *       documentation and/or other materials provided with the distribution.
  18.  *     * Neither the name of the author nor the names of its contributors may be
  19.  *       used to endorse or promote products derived from this software without
  20.  *       specific prior written permission.
  21.  *
  22.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
  23.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  24.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  25.  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
  26.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  27.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  29.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32.  
  33.  * @package    Pxxo
  34.  * @copyright  Copyright (c) 2008 Nicolas Thouvenin
  35.  * @license    http://opensource.org/licenses/bsd-license.php
  36.  * @version    $Id$
  37.  */
  38. require_once 'Pxxo.php';
  39. /**
  40.  * Classe permet de gérer l'accès à des flux de données
  41.  * Un flux est par exemple une chaine de caractère, un fichier,
  42.  * mais cela pourait être une base de données, une zone mémoire, peu importe...
  43.  *
  44.  * @package    Pxxo
  45.  * @copyright  Copyright (c) 2008 Nicolas Thouvenin
  46.  * @license    http://opensource.org/licenses/bsd-license.php
  47.  */
  48. class Pxxo_Buffer extends Pxxo
  49. {
  50.     /**
  51.      * @var        string identifiant unique du Buffer
  52.      */
  53.     protected $id = '';
  54.     /**
  55.      * @var        string localisation du flux
  56.      */
  57.     public $uri = '';
  58.     /**
  59.      * @var        string 
  60.      */
  61.     protected $content = '';
  62.     /**
  63.      * @var        array list de callback à appliquer en sortie du flux
  64.      */
  65.     protected $_func = array();
  66.     /**
  67.      * @var     string type du contenu du flux
  68.      */
  69.     protected $type = 'txt';
  70.     /**
  71.      * @var        string  support pour le flux (url/file/buffer/...)
  72.      */
  73.      public $scheme = 'buffer';
  74.  
  75.      /**
  76.      * @var integer importance du Buffer
  77.      */
  78.      protected $weight = 0;
  79.     /**
  80.      * Constructeur PHP5
  81.      *
  82.      * @param    string localisation du flux
  83.      */
  84.     function __construct($a null)
  85.     {
  86.         parent::__construct();
  87.         $this->set($a);
  88.     }
  89.     /**
  90.      * Création d'un objet pour un type de flux spécifique
  91.      *
  92.      * @param   string type du fichier
  93.      * @return    object 
  94.      * @static
  95.      */
  96.     public static function factory($type$uri)
  97.     {
  98.         $type strtolower($type);
  99.         switch ($type{
  100.         case 'php':
  101.             include_once('Pxxo/Buffer/PHP.php');
  102.             $o new Pxxo_Buffer_PHP($uri);
  103.             break;
  104.         case 'html':
  105.             include_once('Pxxo/Buffer/HTML.php');
  106.             $o new Pxxo_Buffer_HTML($uri);
  107.             break;
  108.         case 'css':
  109.         case 'style':
  110.             include_once('Pxxo/Buffer/Resource/CSS.php');
  111.             $o new Pxxo_Buffer_Resource_CSS($uri);
  112.             break;
  113.         case 'js':
  114.         case 'javascript':
  115.             include_once('Pxxo/Buffer/Resource/Javascript.php');
  116.             $o new Pxxo_Buffer_Resource_Javascript($uri);
  117.             break;
  118.         case 'title':
  119.             include_once('Pxxo/Buffer/Header/Title.php');
  120.             $o new Pxxo_Buffer_Header_Title($uri);
  121.             break;
  122.         case 'meta':
  123.             include_once('Pxxo/Buffer/Header/Meta.php');
  124.             $o new Pxxo_Buffer_Header_Meta($uri);
  125.             break;
  126.         case 'header':
  127.             include_once('Pxxo/Buffer/Header.php');
  128.             $o new Pxxo_Buffer_Header($uri);
  129.             break;
  130.         case 'png':
  131.         case 'gif':
  132.         case 'jpg':
  133.         case 'svg':
  134.             include_once('Pxxo/Buffer/Resource.php');
  135.             $o new Pxxo_Buffer_Resource($uri);
  136.             $o->type $type;
  137.             break;
  138.         case 'tpl':
  139.         default:
  140.             $o new Pxxo_Buffer($uri);
  141.         }
  142.         return $o;
  143.     }
  144.     /**
  145.      * On récupère le contenu du flux
  146.      *
  147.      * @return    string le contenu du flux
  148.      */
  149.     public function get(
  150.     {
  151.         if (!is_null($this->contentand $this->content === '' and $this->scheme == 'file'{
  152.             $this->content = file_get_contents($this->uri);
  153.         }
  154.         $this->filter();
  155.         return $this->content;
  156.     }
  157.     /**
  158.      * On rempli soit même le flux
  159.      *
  160.      * @param   mixed 
  161.      */
  162.     public function set($p
  163.     {
  164.         if (!is_null($p)) {
  165.             if (strpos($p"\n"=== false and strpos($p'<'=== false and strlen($p 256and is_file($p)) {
  166.                 // Reference vers un contenu
  167.                 $this->uri = $p;
  168.                 $this->scheme = 'file';
  169.                 $this->setID(md5(''.filemtime($this->uri).$this->uri));
  170.             }
  171.             else {
  172.                 // Le contenu direct
  173.                 $this->uri = md5(''.$p)// Localization fictive, est-ce vraiment utile ????
  174.                 $this->content = $p;
  175.                 $this->scheme = 'buffer';
  176.                 $this->setID($this->uri);
  177.             }
  178.         }
  179.     }
  180.     /**
  181.      * Merge un buffer avec le buffer courant
  182.      *
  183.      * @param   Pxxo_Buffer 
  184.      * @return  Pxxo_Buffer 
  185.      */
  186.     public function merge($o
  187.     {
  188.         $this->set($this->get().$o->get());
  189.         return $this;
  190.     }
  191.     /**
  192.      * Ajoute une fonction callback en sortie du flux
  193.      *
  194.      * @param    string nom d'une function ou d'une méthode de l'objet Buffer (protected)
  195.      * @return   boolean le filtre est-il actif
  196.      */
  197.     public function addFilter($s
  198.     {
  199.         if (is_string($sand method_exists($this$s)) {
  200.             $this->_func[array('method'$s);
  201.             return true;
  202.         }
  203.         elseif (is_string($sand function_exists($s)) {
  204.             $this->_func[$s;
  205.             return true;
  206.         }
  207.         else return false;
  208.     }
  209.     /**
  210.      * Application des Filtres
  211.      *
  212.      */
  213.     public function filter(
  214.     {
  215.         foreach($this->_func as $func{
  216.             if (is_array($funcand $func[0=== 'method'{
  217.                 $this->content = call_user_func(array($this$func[1])$this->content);
  218.             }
  219.             else {
  220.                 $this->content = call_user_func($func$this->content);
  221.             }
  222.         }
  223.     }
  224.     /**
  225.      * Fixe l'identifiant unique du Buffer
  226.      *
  227.      * @param string 
  228.      */
  229.     public function setID($s
  230.     {
  231.         if (isset($s[0]and $s[0== '-'$s[0'z';
  232.         $this->id = $s;
  233.         $this->addCacheID($this->id);
  234.     }
  235.     /**
  236.      * Renvoit un identifiant unique du Buffer
  237.      *
  238.      * @return    string 
  239.      */
  240.     public function getID(
  241.     {
  242.         return $this->id;
  243.     }
  244.     /**
  245.      * Renvoi le type du Buffer
  246.      *
  247.      * @return    string 
  248.      */
  249.     public function getType(
  250.     {
  251.         return $this->type;
  252.     }
  253.     /**
  254.      * Renvoi le type étendu du Buffer
  255.      *
  256.      * @return    string 
  257.      */
  258.     public function getExtendedType(
  259.     {
  260.         return $this->type;
  261.     }
  262.  
  263.     /**
  264.      * Teste le type du Buffer
  265.      *
  266.      * @return    boolean 
  267.      */
  268.     public function isType($type
  269.     {
  270.         return $this->type === $type true false;
  271.     }
  272.  
  273.     /**
  274.      * Donne un poid au Buffer
  275.      *
  276.      * @param integer 
  277.      */
  278.     public function setWeight($w)
  279.     {
  280.         $this->weight = $w;
  281.     }
  282.  
  283.     /**
  284.     * returne le poid duBuffer
  285.      *
  286.      * @return integer 
  287.      */
  288.     public function getWeight()
  289.     {
  290.         return $this->weight;
  291.     }
  292.  
  293. }

Documentation generated on Thu, 13 Mar 2008 22:03:10 +0100 by phpDocumentor 1.4.1