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