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