1<?php
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.16 2005/06/02 12:24:43 vincentlascaux Exp $
30 * @link       http://pear.php.net/package/File_Archive
31 */
32
33require_once "File/Archive/Writer/Archive.php";
34require_once "File/Archive/Writer/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 */
40class 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 $buffer = '';
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        parent::File_Archive_Writer_Archive($filename, $t, $stat, $autoClose);
76    }
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->buffer);
136            }
137            if (PEAR::isError($error)) {
138                return $error;
139            }
140
141            $this->currentFilename = null;
142            $this->currentDataFile = null;
143            $this->buffer = '';
144        }
145    }
146    /**
147     * @see File_Archive_Writer::writeData()
148     */
149    function writeData($data)
150    {
151        if ($this->currentDataFile !== null) {
152            $this->buffer .= file_get_contents($this->currentDataFile);
153            $this->currentDataFile = null;
154        }
155        $this->buffer .= $data;
156    }
157    /**
158     * @see File_Archive_Writer::writeFile()
159     */
160    function writeFile($filename)
161    {
162        if ($this->currentDataFile === null && empty($this->buffer)) {
163            $this->currentDataFile = $filename;
164        } else {
165            if ($this->currentDataFile !== null) {
166                $this->buffer .= file_get_contents($this->currentDataFile);
167                $this->currentDataFile = null;
168            }
169            $this->buffer .= file_get_contents($filename);
170        }
171    }
172
173//MUST REWRITE FUNCTIONS
174    /**
175     * The subclass must treat the data $data
176     * $data is the entire data of the filename $filename
177     * $stat is the stat of the file
178     *
179     * @access protected
180     */
181    function appendFileData($filename, $stat, &$data) { }
182
183//SHOULD REWRITE FUNCTIONS
184    /**
185     * The subclass may rewrite the sendHeader function if it needs to execute
186     * code before the first file
187     *
188     * @access protected
189     */
190    function sendHeader() { }
191    /**
192     * The subclass may rewrite the sendFooter function if it needs to execute
193     * code before closing the archive
194     *
195     * @access protected
196     */
197    function sendFooter() { }
198    /**
199     * The subclass may rewrite this class if it knows an efficient way to treat
200     * a physical file.
201     *
202     * @access protected
203     */
204    function appendFile($filename, $dataFilename)
205    {
206        return $this->appendFileData(
207                            $filename,
208                            stat($dataFilename),
209                            file_get_contents($dataFilename));
210    }
211}
212
213?>