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