File_Archive
[ class tree: File_Archive ] [ index: File_Archive ] [ all elements ]

Source for file MemoryArchive.php

Documentation is available at MemoryArchive.php

  1. <?
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3.  
  4. /**
  5.  * Base class for all the archiveWriters that can only work on complete files
  6.  * (the write data function may be called with small chunks of data)
  7.  *
  8.  * PHP versions 4 and 5
  9.  *
  10.  * This library is free software; you can redistribute it and/or
  11.  * modify it under the terms of the GNU Lesser General Public
  12.  * License as published by the Free Software Foundation; either
  13.  * version 2.1 of the License, or (at your option) any later version.
  14.  *
  15.  * This library is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18.  * Lesser General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU Lesser General Public
  21.  * License along with this library; if not, write to the Free Software
  22.  * Foundation, Inc., 59 Temple Place, Suite 330,Boston,MA 02111-1307 USA
  23.  *
  24.  * @category   File Formats
  25.  * @package    File_Archive
  26.  * @author     Vincent Lascaux <vincentlascaux@php.net>
  27.  * @copyright  1997-2005 The PHP Group
  28.  * @license    http://www.gnu.org/copyleft/lesser.html  LGPL
  29.  * @version    CVS: $Id: MemoryArchive.php,v 1.4 2005/02/18 21:00:49 vincentlascaux Exp $
  30.  * @link       http://pear.php.net/package/File_Archive
  31.  */
  32.  
  33. require_once "Archive.php";
  34. require_once "Memory.php";
  35.  
  36. /**
  37.  * Base class for all the archiveWriters that can only work on complete files
  38.  * (the write data function may be called with small chunks of data)
  39.  */
  40. class File_Archive_Writer_MemoryArchive extends File_Archive_Writer_Archive
  41. {
  42.     /**
  43.      * @var File_Archive_Writer_Memory A buffer where the data will be put waiting for the file to be complete
  44.      * @access private
  45.      */
  46.     var $memoryWriter = null;
  47.     /**
  48.      * @var String Name of the file which data are coming
  49.      * @access private
  50.      */
  51.     var $currentFilename = null;
  52.     /**
  53.      * @var Array Stats of the file which data are coming
  54.      * @access private
  55.      */
  56.     var $currentStat = null;
  57.     /**
  58.      * @var String URL of the file being treated if it is a physical file
  59.      * @access private
  60.      */
  61.     var $currentDataFile = null;
  62.     /**
  63.      * @var Int Number of times newFile function has been called
  64.      * @access protected
  65.      */
  66.     var $nbFiles = 0;
  67.  
  68.     /**
  69.      * See the constructor of File_Archive_Writer for more informations
  70.      */
  71.     function File_Archive_Writer_MemoryArchive($filename, &$t, $stat=array(), $autoClose = true)
  72.     {
  73.         $this->memoryWriter = new File_Archive_Writer_Memory();
  74.         parent::File_Archive_Writer_Archive($filename, $t, $stat, $autoClose);
  75.     }
  76.     /**
  77.      * @see File_Archive_Writer::newFile
  78.      */
  79.     function newFile($filename, $stat, $mime = "application/octet-stream")
  80.     {
  81.         if($this->nbFiles == 0) {
  82.             $this->sendHeader();
  83.         } else {
  84.             $this->flush();
  85.         }
  86.  
  87.         $this->nbFiles++;
  88.  
  89.         $this->currentFilename = $filename;
  90.         $this->currentStat = $stat;
  91.  
  92.         return true;
  93.     }
  94.     /**
  95.      * @see File_Archive_Writer::close
  96.      */
  97.     function close()
  98.     {
  99.         $this->flush();
  100.         $this->sendFooter();
  101.  
  102.         parent::close();
  103.     }
  104.     /**
  105.      * Indicate that all the data have been read from the current file
  106.      * and send it to appendFileData
  107.      * Send the current data to the appendFileData function
  108.      *
  109.      * @access private
  110.      */
  111.     function flush()
  112.     {
  113.         if($this->currentFilename !== null) {
  114.             if($this->currentDataFile !== null)
  115.                 $this->appendFile($this->currentFilename,
  116.                                   $this->currentDataFile);
  117.             else
  118.                 $this->appendFileData($this->currentFilename,
  119.                                  $this->currentStat,
  120.                                  $this->memoryWriter->getData());
  121.  
  122.             $this->currentFilename = null;
  123.             $this->currentDataFile = null;
  124.             $this->memoryWriter->clear();
  125.         }
  126.     }
  127.     /**
  128.      * @see File_Archive_Writer::writeData
  129.      */
  130.     function writeData($data) { $this->memoryWriter->writeData($data); }
  131.     /**
  132.      * @see File_Archive_Writer::writeFile
  133.      */
  134.     function writeFile($filename)
  135.     {
  136.         if($this->currentDataFile == null && $this->memoryWriter->isEmpty()) {
  137.             $this->currentDataFile = $filename;
  138.         } else {
  139.             $this->memoryWriter->writeFile($filename);
  140.         }
  141.     }
  142.  
  143. //MUST REWRITE FUNCTIONS
  144.     /**
  145.      * The subclass must treat the data $data
  146.      * $data is the entire data of the filename $filename
  147.      * $stat is the stat of the file
  148.      *
  149.      * @access protected
  150.      */
  151.     function appendFileData($filename, $stat, $data) { }
  152.  
  153. //SHOULD REWRITE FUNCTIONS
  154.     /**
  155.      * The subclass may rewrite the sendHeader function if it needs to execute code
  156.      * before the first file
  157.      *
  158.      * @access protected
  159.      */
  160.     function sendHeader() { }
  161.     /**
  162.      * The subclass may rewrite the sendFooter function if it needs to execute code
  163.      * before closing the archive
  164.      *
  165.      * @access protected
  166.      */
  167.     function sendFooter() { }
  168.     /**
  169.      * The subclass may rewrite this class if it knows an efficient way to treat a physical file
  170.      * This function is equivalent to $this->appendFileData($filename, stat($dataFilename), file_get_contents($dataFilename));
  171.      * but may be more efficient
  172.      *
  173.      * @access protected
  174.      */
  175.     function appendFile($filename, $dataFilename)
  176.     {
  177.         $this->appendFileData($filename, stat($dataFilename), file_get_contents($dataFilename));
  178.     }
  179. }
  180.  
  181. ?>

Documentation generated on Mon, 11 Mar 2019 14:24:01 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.