Continued:
[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\Object\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\Middleware\Debug\DebugMiddleware;
12 use Org\Mxchange\CoreFramework\Traits\Streamer\File\Input\FileInputStreamerTrait;
13 use Org\Mxchange\CoreFramework\Traits\Streamer\File\Output\FileOutputStreamerTrait;
14
15 // Import SPL stuff
16 use \InvalidArgumentException;
17 use \SplFileInfo;
18
19 /**
20  * This is a file IO handler. It handles reading from and writing to files.
21  * Missing paths in writing process will be automatically created.
22  *
23  * @author              Roland Haeder <webmaster@shipsimu.org>
24  * @version             0.0.0
25  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
26  * @license             GNU GPL 3.0 or any newer version
27  * @link                http://www.shipsimu.org
28  *
29  * This program is free software: you can redistribute it and/or modify
30  * it under the terms of the GNU General Public License as published by
31  * the Free Software Foundation, either version 3 of the License, or
32  * (at your option) any later version.
33  *
34  * This program is distributed in the hope that it will be useful,
35  * but WITHOUT ANY WARRANTY; without even the implied warranty of
36  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37  * GNU General Public License for more details.
38  *
39  * You should have received a copy of the GNU General Public License
40  * along with this program. If not, see <http://www.gnu.org/licenses/>.
41  */
42 class FileIoHandler extends BaseMiddleware implements IoHandler {
43         // Load traits
44         use FileInputStreamerTrait;
45         use FileOutputStreamerTrait;
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         private function __construct () {
58                 // Call parent constructor
59                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('FILE-IO-HANDLER: CONSTRUCTED!');
60                 parent::__construct(__CLASS__);
61
62                 // Set own instance
63                 self::$selfInstance = $this;
64
65                 // Trace message
66                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('FILE-IO-HANDLER: EXIT!');
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      $ioHandlerInstance      A prepared instance of FilIoHandler
74          */
75         public static final function createFileIoHandler () {
76                 // Get instance
77                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('FILE-IO-HANDLER: CALLED!');
78                 $ioHandlerInstance = new FileIoHandler();
79
80                 // Set the *real* file IO instances (both the same)
81                 $ioHandlerInstance->setInputStreamerInstance(ObjectFactory::createObjectByConfiguredName('file_input_class'));
82                 $ioHandlerInstance->setOutputStreamerInstance(ObjectFactory::createObjectByConfiguredName('file_output_class'));
83
84                 // Return instance
85                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FILE-IO-HANDLER: ioHandlerInstance=%s - EXIT!', $ioHandlerInstance->__toString()));
86                 return $ioHandlerInstance;
87         }
88
89         /**
90          * Getter for an instance of this class
91          *
92          * @return      $selfInstance   An instance of this class
93          */
94         public static final function getSelfInstance () {
95                 // Trace message
96                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FILE-IO-HANDLER: Returning self::selfInstance[]=%s - EXIT!', gettype(self::$selfInstance)));
97                 return self::$selfInstance;
98         }
99
100         /**
101          * Saves streamed (that are mostly serialized objects) data to files or
102          * external servers.
103          *
104          * @param       $infoInstance   An instance of a SplFileInfo class
105          * @param       $dataArray      Array containing the compressor's extension and streamed data
106          * @return      void
107          * @throws      UnsupportedOperationException   If this method is called
108          */
109         public function saveFile (SplFileInfo $infoInstance, array $dataArray) {
110                 // Trace message for logging parameters
111                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FILE-IO-HANDLER: infoInstance.pathname=%s,dataArray()=%d - CALLED!', $infoInstance->getPathname(), count($dataArray)));
112                 throw new UnsupportedOperationException([$this, __FUNCTION__], FrameworkInterface::EXCEPTION_UNSPPORTED_OPERATION);
113         }
114
115         /**
116          * Saves a file with data by using the current output stream
117          *
118          * @param       $infoInstance           An instance of a SplFileInfo class
119          * @param       $dataStream                     File data stream
120          * @param       $objectInstance         An instance of a FrameworkInterface class (default: NULL)
121          * @return      void
122          * @throws      InvalidArgumentException        If a parameter has an invalid value
123          */
124         public function saveStreamToFile (SplFileInfo $infoInstance, string $dataStream, FrameworkInterface $objectInstance = NULL) {
125                 // Check parameters
126                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FILE-IO-HANDLER: infoInstance=%s,dataStream()=%d,objectInstance[]=%s - CALLED!', $infoInstance->__toString(), strlen($dataStream), gettype($objectInstance)));
127                 if (empty($dataStream)) {
128                         // Throw IAE
129                         throw new InvalidArgumentException('Parameter "dataStream" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
130                 }
131
132                 // Default is this array
133                 $className = $this->__toString();
134
135                 // Is the object instance set?
136                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('FILE-IO-HANDLER: className=%s - BEFORE!', $className));
137                 if ($objectInstance instanceof FrameworkInterface) {
138                         // Then use this
139                         $className = $objectInstance->__toString();
140                 }
141
142                 // Send the infoInstance and data array to the output handler
143                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('FILE-IO-HANDLER: className=%s - AFTER!', $className));
144                 $this->getOutputStreamerInstance()->saveFile($infoInstance, [
145                         0 => $className,
146                         1 => $dataStream
147                 ]);
148
149                 // Trace message
150                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('FILE-IO-HANDLER: EXIT!');
151         }
152
153         /** Loads data from a file over the input handler
154          *
155          * @param       $infoInstance   An instance of a SplFileInfo class
156          * @return      $array  Array with the file contents
157          */
158         public function loadFileContents (SplFileInfo $infoInstance) {
159                 // Read from the input handler
160                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FILE-IO-HANDLER: infoInstance=%s - CALLED!', $infoInstance->__toString()));
161                 return $this->getInputStreamerInstance()->loadFileContents($infoInstance);
162         }
163
164         /**
165          * Determines seek position
166          *
167          * @return      $seekPosition   Current seek position
168          * @todo        0% done
169          */
170         public function determineSeekPosition () {
171                 // Trace message
172                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('FILE-IO-HANDLER: CALLED!');
173                 DebugMiddleware::getSelfInstance()->partialStub();
174
175                 // Trace message
176                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('FILE-IO-HANDLER: EXIT!');
177         }
178
179         /**
180          * Seek to given offset (default) or other possibilities as fseek() gives.
181          *
182          * @param       $offset         Offset to seek to (or used as "base" for other seeks)
183          * @param       $whence         Added to offset (default: only use offset to seek to)
184          * @return      void
185          * @throws      InvalidArgumentException        If a parameter has an invalid value
186          */
187         public function seek (int $offset, int $whence = SEEK_SET) {
188                 // Check parameter
189                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FILE-IO-HANDLER: offset=%d,whence=%d - CALLED!', $offset, $whence));
190                 if ($offset < 0) {
191                         // Throw IAE
192                         throw new InvalidArgumentException('Parameter "offset" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
193                 }
194
195                 // @TODO Unfinished work
196                 DebugMiddleware::getSelfInstance()->partialStub('offset=' . $offset . ',whence=' . $whence);
197
198                 // Trace message
199                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('FILE-IO-HANDLER: EXIT!');
200         }
201
202         /**
203          * Size of file stack
204          *
205          * @return      $size   Size (in bytes) of file
206          * @todo        0% done
207          */
208         public function size () {
209                 // @TODO: Unfinished method:
210                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('FILE-IO-HANDLER: CALLED!');
211                 DebugMiddleware::getSelfInstance()->partialStub();
212
213                 // Trace message
214                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('FILE-IO-HANDLER: EXIT!');
215         }
216
217         /**
218          * "Getter" for seek position
219          *
220          * @return      $seekPosition   Current seek position
221          * @todo        0% done
222          */
223         public function getPosition () {
224                 // @TODO: Unfinished method:
225                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('FILE-IO-HANDLER: CALLED!');
226                 DebugMiddleware::getSelfInstance()->partialStub();
227
228                 // Trace message
229                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('FILE-IO-HANDLER: EXIT!');
230         }
231
232 }