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