3 namespace Org\Mxchange\CoreFramework\Handler\Filesystem;
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;
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.
21 * @author Roland Haeder <webmaster@shipsimu.org>
23 <<<<<<< HEAD:framework/main/middleware/io/class_FileIoHandler.php
24 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
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
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.
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.
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/>.
44 class FileIoHandler extends BaseMiddleware implements IoHandler {
46 * The *real* file input class we shall use for reading data
48 private $inputStream = NULL;
51 * The *real* file output class we shall use for reading data
53 private $outputStream = NULL;
56 * An instance of this class
58 private static $selfInstance = NULL;
61 * Protected constructor
65 protected function __construct () {
66 // Call parent constructor
67 parent::__construct(__CLASS__);
70 self::$selfInstance = $this;
74 * Creates an instance of this class and prepares the IO system. This is
75 * being done by setting the default file IO class
77 * @return $ioInstance A prepared instance of FilIOHandler
79 public static final function createFileIoHandler () {
81 $ioHandler = new FileIoHandler();
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'));
92 * Getter for an instance of this class
94 * @return $selfInstance An instance of this class
96 public static final function getSelfInstance () {
97 return self::$selfInstance;
101 * Setter for the *real* file input instance
103 * @param $inputStream The *real* file-input class
106 public final function setInputStream (FileInputStreamer $inputStream) {
107 $this->inputStream = $inputStream;
111 * Getter for the *real* file input instance
113 * @return $inputStream The *real* file-input class
115 public final function getInputStream () {
116 return $this->inputStream;
120 * Setter for the *real* file output instance
122 * @param $outputStream The *real* file-output class
125 public final function setOutputStream (FileOutputStreamer $outputStream) {
126 $this->outputStream = $outputStream;
130 * Getter for the *real* file output instance
132 * @return $outputStream The *real* file-output class
134 public final function getOutputStream () {
135 return $this->outputStream;
138 * Saves streamed (that are mostly serialized objects) data to files or
141 * @param $infoInstance An instance of a SplFileInfo class
142 * @param $dataArray Array containing the compressor's extension and streamed data
144 * @throws UnsupportedOperationException If this method is called
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);
152 * Saves a file with data by using the current output stream
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)
159 public function saveStreamToFile (SplFileInfo $infoInstance, $dataStream, FrameworkInterface $objectInstance = NULL) {
160 // Default is this array
161 $className = $this->__toString();
163 // Is the object instance set?
164 if ($objectInstance instanceof FrameworkInterface) {
166 $className = $objectInstance->__toString();
169 // Prepare output array
175 // Send the infoInstance and dataArray to the output handler
176 $this->getOutputStream()->saveFile($infoInstance, $dataArray);
179 /** Loads data from a file over the input handler
181 * @param $infoInstance An instance of a SplFileInfo class
182 * @return $array Array with the file contents
184 public function loadFileContents (SplFileInfo $infoInstance) {
185 // Read from the input handler
186 return $this->getInputStream()->loadFileContents($infoInstance);
190 * Determines seek position
192 * @return $seekPosition Current seek position
195 public function determineSeekPosition () {
196 $this->partialStub();
200 * Seek to given offset (default) or other possibilities as fseek() gives.
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
206 public function seek ($offset, $whence = SEEK_SET) {
207 $this->partialStub('offset=' . $offset . ',whence=' . $whence);
213 * @return $size Size (in bytes) of file
215 public function size () {
216 $this->partialStub();
220 * "Getter" for seek position
222 * @return $seekPosition Current seek position
225 public function getPosition () {
226 $this->partialStub();
230 * Seek to given offset (default) or other possibilities as fseek() gives.
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
236 public function seek ($offset, $whence = SEEK_SET) {
237 $this->partialStub('offset=' . $offset . ',whence=' . $whence);
243 * @return $size Size (in bytes) of file
245 public function size () {
246 $this->partialStub();