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