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