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