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