Source for file Resource.php

Documentation is available at Resource.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: Resource.php,v 1.5 2008/03/05 15:17:49 thouveni Exp $
  37.  */
  38. require_once 'Pxxo/Buffer.php';
  39.  
  40. /**
  41.  * Classe permet de gérer l'accès à des flux de données comme des ressources WEB
  42.  *
  43.  * Un flux de type ressource renvoit une url vers un fichier contenant le flux.
  44.  *
  45.  * @package    Pxxo
  46.  * @copyright  Copyright (c) 2008 Nicolas Thouvenin
  47.  * @license    http://opensource.org/licenses/bsd-license.php
  48.  */
  49. {
  50.     /**
  51.      * @var     string type du contenu du flux
  52.      */
  53.     protected $type = 'rsc';
  54.     /**
  55.      * @var        string url d'accès à la ressource
  56.      */
  57.     public $url = './';
  58.     /**
  59.      * @var        string chemin physique correspondant à l'url
  60.      */
  61.     public $path = '.';
  62.     /**
  63.      * @var    array chemin physique et chemin logique de la resource
  64.      */
  65.     public $file = array();
  66.     /**
  67.      * @var        string  file or inline : sous fomre de fichier ou de lignes de code inserées
  68.      */
  69.     protected $disposition = 'file';
  70.     /**
  71.      * Constructeur PHP5
  72.      *
  73.      * @param    string localisation du flux
  74.      */
  75.     function __construct($p)
  76.     {
  77.         parent::__construct($p);
  78.     }
  79.     /**
  80.      * On récupere le contenu de la ressource
  81.      *
  82.      * @return    mixed     Description
  83.      */
  84.     public function getContent(
  85.     {
  86.         return parent::get();
  87.     }
  88.     /**
  89.      * On stocke le contenu du flux dans un fichier accessible via $this->url
  90.      *
  91.      * @return    string l'url d'accès au contenu du flux
  92.      */
  93.     public function get(
  94.     {
  95.         // Calcul du nom et de l'emplacement du fichier
  96.         $id $this->getID();
  97.         if (empty($id)) return null;
  98.         $filename $id.'.'.$this->type;
  99.         $filepath $this->path.DIRECTORY_SEPARATOR;
  100.         $file $filepath.$filename;
  101.         $url $this->url.'/'.str_replace(DIRECTORY_SEPARATOR'/'$filename);
  102.  
  103.  
  104.         if ($this->isCaching(|| $this->testCacheLevel(P_C_RESOURCE|| ($this->isCaching(and $this->testCacheLevel(P_C_RESOURCEand !file_exists($file))) {
  105.             $this->traceDebug("Buffer/RESOURCE\t[executed]\tlocalization(URI:".$this->uri.",\tRSC:".$url.')');
  106.  
  107.             // Création du Répertoire
  108.             if (!is_dir($filepath)) {
  109.                 if (!mkdir($filepath)) {
  110.                     trigger_error('Buffer_Resource failed  : unable to create '.$filepathE_USER_ERROR);
  111.                 }
  112.                 chmod($filepath,0777);
  113.             }
  114.             // Création du fichier
  115.             if (!$handle fopen($file'w'))
  116.             trigger_error('Buffer_Resource failed : unable to create '.$fileE_USER_ERROR);
  117.  
  118.             if (fwrite($handleparent::get()) === FALSE)
  119.             trigger_error('Buffer_Resource failed : unable to write in'.$fileE_USER_ERROR);
  120.  
  121.             fclose($handle);
  122.             if (chmod($file,0777=== FALSE)
  123.             trigger_error('Buffer_Resource failed : unable to change permission for '.$fileE_USER_ERROR);
  124.         }
  125.         else $this->traceDebug("Buffer/RESOURCE\t[cached]\tlocalization(URI:".$this->uri.",\tRSC:".$url.')');
  126.         $this->file = array($file$url);
  127.         return $url;
  128.     }
  129.     /**
  130.      * Merge un buffer avec le buffer courant
  131.      *
  132.      * @param   Pxxo_Buffer 
  133.      * @param boolean ajout ou non un commentaire
  134.      * @return  Pxxo_Buffer 
  135.      */
  136.     public function merge($o$c true
  137.     {
  138.         if ($c === true)
  139.             $com $this->comment(
  140.                 ' ID('.$o->getID().')'.                
  141.                 ' URI('.$o->uri.')'.
  142.                 ' SCHEME('.$o->scheme.')'.
  143.                 ' WEIGHT('.$o->weight.')'.
  144.                 ' TYPE('.$o->getExtendedType().')');
  145.         else $com '';
  146.  
  147.         if (method_exists($o,'getContent')) {
  148.             $this->set($this->getContent().$com.$o->getContent());
  149.             return $this;
  150.         }
  151.     }
  152.     /**
  153.      * Filtre permettant de minimiser une flux
  154.      *
  155.      * @param   string chaine de caractère
  156.      * @return    string chaine de caractère compressées
  157.      * @abstract
  158.      */
  159.     public function compress($s
  160.     {
  161.         return $s;
  162.     }
  163.     /**
  164.      * Fixe comme on utilisera le code Javascript
  165.      *
  166.      * @param    string 
  167.      */
  168.     public function setDisposition($s
  169.     {
  170.         if ($s == 'inline'$this->weight -= 50;
  171.         $this->disposition = $s;
  172.     }
  173.     /**
  174.      * Retourne le mode d'utilisation du code Javascript
  175.      *
  176.      * @return    string 
  177.      */
  178.     public function getDisposition(
  179.     {
  180.         return $this->disposition;
  181.     }
  182.     /**
  183.      * Retrourne une chaine en HTML permettant de charger la ressource
  184.      *
  185.      * @return    string chaine de caractère en HTML
  186.      * @abstract
  187.      */
  188.     public function getHTML(
  189.     {
  190.         // ...
  191.     }
  192.     /**
  193.      * Renvoi le type étendu du Buffer
  194.      *
  195.      * @return    string 
  196.      */
  197.     public function getExtendedType(
  198.     {
  199.         return $this->type.':'.$this->disposition;
  200.     }
  201.     /**
  202.      * fabrique un commentaire à partir de la chaine donnée en paramètre
  203.      *
  204.      * @param string 
  205.      * @return string 
  206.      */
  207.     public function comment($s)
  208.     {
  209.         return '/* '.$s." */\n";
  210.     }    
  211. }

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