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.10 2005/04/08 10:18:06 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
  44.      *         waiting for the file to be complete
  45.      * @access private
  46.      */
  47.     var $memoryWriter = null;
  48.     /**
  49.      * @var    string Name of the file which data are coming
  50.      * @access private
  51.      */
  52.     var $currentFilename = null;
  53.     /**
  54.      * @var    array Stats of the file which data are coming
  55.      * @access private
  56.      */
  57.     var $currentStat = null;
  58.     /**
  59.      * @var    string URL of the file being treated if it is a physical file
  60.      * @access private
  61.      */
  62.     var $currentDataFile = null;
  63.     /**
  64.      * @var    int Number of times newFile function has been called
  65.      * @access protected
  66.      */
  67.     var $nbFiles = 0;
  68.  
  69.     /**
  70.      * @see File_Archive_Writer::File_Archive_Writer()
  71.      */
  72.     function File_Archive_Writer_MemoryArchive
  73.                 ($filename, &$t, $stat = array(), $autoClose = true)
  74.     {
  75.         $this->memoryWriter = new File_Archive_Writer_Memory($tmp = null);
  76.         parent::File_Archive_Writer_Archive($filename, $t, $stat, $autoClose);
  77.     }
  78.     /**
  79.      * @see File_Archive_Writer::newFile()
  80.      */
  81.     function newFile($filename, $stat = array(),
  82.                      $mime = "application/octet-stream")
  83.     {
  84.         if ($this->nbFiles == 0) {
  85.             $error = $this->sendHeader();
  86.             if (PEAR::isError($error)) {
  87.                 return $error;
  88.             }
  89.         } else {
  90.             $error = $this->flush();
  91.             if (PEAR::isError($error)) {
  92.                 return $error;
  93.             }
  94.         }
  95.  
  96.         $this->nbFiles++;
  97.  
  98.         $this->currentFilename = $filename;
  99.         $this->currentStat = $stat;
  100.  
  101.         return true;
  102.     }
  103.     /**
  104.      * @see File_Archive_Writer::close()
  105.      */
  106.     function close()
  107.     {
  108.         $error = $this->flush();
  109.         if (PEAR::isError($error)) {
  110.             return $error;
  111.         }
  112.         $error = $this->sendFooter();
  113.         if (PEAR::isError($error)) {
  114.             return $error;
  115.         }
  116.  
  117.         return parent::close();
  118.     }
  119.     /**
  120.      * Indicate that all the data have been read from the current file
  121.      * and send it to appendFileData
  122.      * Send the current data to the appendFileData function
  123.      *
  124.      * @access private
  125.      */
  126.     function flush()
  127.     {
  128.         if ($this->currentFilename !== null) {
  129.             if ($this->currentDataFile !== null) {
  130.                 $error = $this->appendFile($this->currentFilename,
  131.                                   $this->currentDataFile);
  132.             } else {
  133.                 $error = $this->appendFileData($this->currentFilename,
  134.                                  $this->currentStat,
  135.                                  $this->memoryWriter->getData());
  136.             }
  137.             if (PEAR::isError($error)) {
  138.                 return $error;
  139.             }
  140.  
  141.             $this->currentFilename = null;
  142.             $this->currentDataFile = null;
  143.             $this->memoryWriter->clear();
  144.         }
  145.     }
  146.     /**
  147.      * @see File_Archive_Writer::writeData()
  148.      */
  149.     function writeData($data) { return $this->memoryWriter->writeData($data); }
  150.     /**
  151.      * @see File_Archive_Writer::writeFile()
  152.      */
  153.     function writeFile($filename)
  154.     {
  155.         if ($this->currentDataFile == null && $this->memoryWriter->isEmpty()) {
  156.             $this->currentDataFile = $filename;
  157.         } else {
  158.             return $this->memoryWriter->writeFile($filename);
  159.         }
  160.     }
  161.  
  162. //MUST REWRITE FUNCTIONS
  163.     /**
  164.      * The subclass must treat the data $data
  165.      * $data is the entire data of the filename $filename
  166.      * $stat is the stat of the file
  167.      *
  168.      * @access protected
  169.      */
  170.     function appendFileData($filename, $stat, $data) { }
  171.  
  172. //SHOULD REWRITE FUNCTIONS
  173.     /**
  174.      * The subclass may rewrite the sendHeader function if it needs to execute
  175.      * code before the first file
  176.      *
  177.      * @access protected
  178.      */
  179.     function sendHeader() { }
  180.     /**
  181.      * The subclass may rewrite the sendFooter function if it needs to execute
  182.      * code before closing the archive
  183.      *
  184.      * @access protected
  185.      */
  186.     function sendFooter() { }
  187.     /**
  188.      * The subclass may rewrite this class if it knows an efficient way to treat
  189.      * a physical file.
  190.      *
  191.      * @access protected
  192.      */
  193.     function appendFile($filename, $dataFilename)
  194.     {
  195.         return $this->appendFileData(
  196.                             $filename,
  197.                             stat($dataFilename),
  198.                             file_get_contents($dataFilename));
  199.     }
  200. }
  201.  
  202. ?>

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