]> git.mxchange.org Git - core.git/blob - framework/main/middleware/io/class_FileIoHandler.php
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\Traits\Streamer\File\Input\FileInputStreamerTrait;
12 use Org\Mxchange\CoreFramework\Traits\Streamer\File\Output\FileOutputStreamerTrait;
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 - 2020 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         // Load traits
42         use FileInputStreamerTrait;
43         use FileOutputStreamerTrait;
44
45         /**
46          * An instance of this class
47          */
48         private static $selfInstance = NULL;
49
50         /**
51          * Protected constructor
52          *
53          * @return      void
54          */
55         protected function __construct () {
56                 // Call parent constructor
57                 parent::__construct(__CLASS__);
58
59                 // Set own instance
60                 self::$selfInstance = $this;
61         }
62
63         /**
64          * Creates an instance of this class and prepares the IO system. This is
65          * being done by setting the default file IO class
66          *
67          * @return      $ioInstance             A prepared instance of FilIOHandler
68          */
69         public static final function createFileIoHandler () {
70                 // Get instance
71                 $ioHandler = new FileIoHandler();
72
73                 // Set the *real* file IO instances (both the same)
74                 $ioHandler->setInputStreamerInstance(ObjectFactory::createObjectByConfiguredName('file_input_class'));
75                 $ioHandler->setOutputStreamerInstance(ObjectFactory::createObjectByConfiguredName('file_output_class'));
76
77                 // Return instance
78                 return $ioHandler;
79         }
80
81         /**
82          * Getter for an instance of this class
83          *
84          * @return      $selfInstance   An instance of this class
85          */
86         public static final function getSelfInstance () {
87                 return self::$selfInstance;
88         }
89
90         /**
91          * Saves streamed (that are mostly serialized objects) data to files or
92          * external servers.
93          *
94          * @param       $infoInstance   An instance of a SplFileInfo class
95          * @param       $dataArray      Array containing the compressor's extension and streamed data
96          * @return      void
97          * @throws      UnsupportedOperationException   If this method is called
98          */
99         public function saveFile (SplFileInfo $infoInstance, array $dataArray) {
100                 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('infoInstance.pathname=' . $infoInstance->getPathname() . ',dataArray()=' . count($dataArray));
101                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
102         }
103
104         /**
105          * Saves a file with data by using the current output stream
106          *
107          * @param       $infoInstance           An instance of a SplFileInfo class
108          * @param       $dataStream                     File data stream
109          * @param       $objectInstance         An instance of a FrameworkInterface class (default: NULL)
110          * @return      void
111          */
112         public function saveStreamToFile (SplFileInfo $infoInstance, string $dataStream, FrameworkInterface $objectInstance = NULL) {
113                 // Default is this array
114                 $className = $this->__toString();
115
116                 // Is the object instance set?
117                 if ($objectInstance instanceof FrameworkInterface) {
118                         // Then use this
119                         $className = $objectInstance->__toString();
120                 } // END - if
121
122                 // Prepare output array
123                 $dataArray = array(
124                         0 => $className,
125                         1 => $dataStream
126                 );
127
128                 // Send the infoInstance and dataArray to the output handler
129                 $this->getOutputStreamerInstance()->saveFile($infoInstance, $dataArray);
130         }
131
132         /** Loads data from a file over the input handler
133          *
134          * @param       $infoInstance   An instance of a SplFileInfo class
135          * @return      $array  Array with the file contents
136          */
137         public function loadFileContents (SplFileInfo $infoInstance) {
138                 // Read from the input handler
139                 return $this->getInputStreamerInstance()->loadFileContents($infoInstance);
140         }
141
142         /**
143          * Determines seek position
144          *
145          * @return      $seekPosition   Current seek position
146          * @todo        0% done
147          */
148         public function determineSeekPosition () {
149                 $this->partialStub();
150         }
151
152         /**
153          * Seek to given offset (default) or other possibilities as fseek() gives.
154          *
155          * @param       $offset         Offset to seek to (or used as "base" for other seeks)
156          * @param       $whence         Added to offset (default: only use offset to seek to)
157          * @return      $status         Status of file seek: 0 = success, -1 = failed
158          */
159         public function seek (int $offset, int $whence = SEEK_SET) {
160                 $this->partialStub('offset=' . $offset . ',whence=' . $whence);
161         }
162
163         /**
164          * Size of file stack
165          *
166          * @return      $size   Size (in bytes) of file
167          */
168         public function size () {
169                 $this->partialStub();
170         }
171
172         /**
173          * "Getter" for seek position
174          *
175          * @return      $seekPosition   Current seek position
176          * @todo        0% done
177          */
178         public function getPosition () {
179                 $this->partialStub();
180         }
181
182 }