Added missing methods from Streamable interface.
[core.git] / inc / classes / middleware / io / class_FileIoHandler.php
1 <?php
2 /**
3  * This is a file IO handler. It handles reading from and writing to files.
4  * Missing paths in writing process will be automatically created.
5  *
6  * @author              Roland Haeder <webmaster@shipsimu.org>
7  * @version             0.0.0
8  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2013 Core Developer Team
9  * @license             GNU GPL 3.0 or any newer version
10  * @link                http://www.shipsimu.org
11  *
12  * This program is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation, either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program. If not, see <http://www.gnu.org/licenses/>.
24  */
25 class FileIoHandler extends BaseMiddleware implements IoHandler {
26         /**
27          * The *real* file input class we shall use for reading data
28          */
29         private $inputStream = NULL;
30
31         /**
32          * The *real* file output class we shall use for reading data
33          */
34         private $outputStream = NULL;
35
36         /**
37          * An instance of this class
38          */
39         private static $selfInstance = NULL;
40
41         /**
42          * Protected constructor
43          *
44          * @return      void
45          */
46         protected function __construct () {
47                 // Call parent constructor
48                 parent::__construct(__CLASS__);
49
50                 // Set own instance
51                 self::$selfInstance = $this;
52         }
53
54         /**
55          * Creates an instance of this class and prepares the IO system. This is
56          * being done by setting the default file IO class
57          *
58          * @return      $ioInstance             A prepared instance of FilIOHandler
59          */
60         public static final function createFileIoHandler () {
61                 // Get instance
62                 $ioHandler = new FileIoHandler();
63
64                 // Set the *real* file IO instances (both the same)
65                 $ioHandler->setInputStream(ObjectFactory::createObjectByConfiguredName('file_input_class'));
66                 $ioHandler->setOutputStream(ObjectFactory::createObjectByConfiguredName('file_output_class'));
67
68                 // Return instance
69                 return $ioHandler;
70         }
71
72         /**
73          * Getter for an instance of this class
74          *
75          * @return      $selfInstance   An instance of this class
76          */
77         public static final function getSelfInstance () {
78                 return self::$selfInstance;
79         }
80
81         /**
82          * Setter for the *real* file input instance
83          *
84          * @param       $inputStream    The *real* file-input class
85          * @return      void
86          */
87         public final function setInputStream (FileInputStreamer $inputStream) {
88                 $this->inputStream = $inputStream;
89         }
90
91         /**
92          * Getter for the *real* file input instance
93          *
94          * @return      $inputStream    The *real* file-input class
95          */
96         public final function getInputStream () {
97                 return $this->inputStream;
98         }
99
100         /**
101          * Setter for the *real* file output instance
102          *
103          * @param       $outputStream   The *real* file-output class
104          * @return      void
105          */
106         public final function setOutputStream (FileOutputStreamer $outputStream) {
107                 $this->outputStream = $outputStream;
108         }
109
110         /**
111          * Getter for the *real* file output instance
112          *
113          * @return      $outputStream   The *real* file-output class
114          */
115         public final function getOutputStream () {
116                 return $this->outputStream;
117         }
118         /**
119          * Saves streamed (that are mostly serialized objects) data to files or
120          * external servers.
121          *
122          * @param       $fileName       The local file's name including full path
123          * @param       $dataArray      Array containing the compressor's extension and streamed data
124          * @return      void
125          * @throws      UnsupportedOperationException   If this method is called
126          */
127         public function saveFile ($fileName, array $dataArray) {
128                 self::createDebugInstance(__CLASS__)->debugOutput('fileName=' . $fileName . ',dataArray()=' . count($dataArray));
129                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
130         }
131
132         /**
133          * Saves a file with data by using the current output stream
134          *
135          * @param       $fileName                       Name of the file
136          * @param       $dataStream                     File data stream
137          * @param       $objectInstance         An instance of a FrameworkInterface class (default: NULL)
138          * @return      void
139          */
140         public function saveStreamToFile ($fileName, $dataStream, FrameworkInterface $objectInstance = NULL) {
141                 // Default is this array
142                 $className = $this->__toString();
143
144                 // Is the object instance set?
145                 if ($objectInstance instanceof FrameworkInterface) {
146                         // Then use this
147                         $className = $objectInstance->__toString();
148                 } // END - if
149
150                 // Prepare output array
151                 $dataArray = array(
152                         0 => $className,
153                         1 => $dataStream
154                 );
155
156                 // Send the fileName and dataArray to the output handler
157                 $this->getOutputStream()->saveFile($fileName, $dataArray);
158         }
159
160         /** Loads data from a file over the input handler
161          *
162          * @param       $fqfn   Given full-qualified file name (FQFN) to load
163          * @return      $array  Array with the file contents
164          */
165         public function loadFileContents ($fqfn) {
166                 // Read from the input handler
167                 return $this->getInputStream()->loadFileContents($fqfn);
168         }
169
170         /**
171          * "Getter" for seek position
172          *
173          * @return      $seekPosition   Current seek position
174          * @todo        0% done
175          */
176         public function getPosition () {
177                 $this->partialStub();
178         }
179
180         /**
181          * Seek to given offset (default) or other possibilities as fseek() gives.
182          *
183          * @param       $offset         Offset to seek to (or used as "base" for other seeks)
184          * @param       $whence         Added to offset (default: only use offset to seek to)
185          * @return      $status         Status of file seek: 0 = success, -1 = failed
186          */
187         public function seek ($offset, $whence = SEEK_SET) {
188                 $this->partialStub('offset=' . $offset . ',whence=' . $whence);
189         }
190
191         /**
192          * Size of file stack
193          *
194          * @return      $size   Size (in bytes) of file
195          */
196         public function size () {
197                 $this->partialStub();
198         }
199 }
200
201 // [EOF]
202 ?>