3 namespace CoreFramework\Handler\Filesystem;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Generic\FrameworkInterface;
8 use CoreFramework\Generic\UnsupportedOperationException;
9 use CoreFramework\Handler\Stream\IoHandler;
10 use CoreFramework\Middleware\BaseMiddleware;
11 use CoreFramework\Stream\Filesystem\FileInputStreamer;
12 use CoreFramework\Stream\Filesystem\FileOutputStreamer;
15 * This is a file IO handler. It handles reading from and writing to files.
16 * Missing paths in writing process will be automatically created.
18 * @author Roland Haeder <webmaster@shipsimu.org>
20 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
21 * @license GNU GPL 3.0 or any newer version
22 * @link http://www.shipsimu.org
24 * This program is free software: you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation, either version 3 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program. If not, see <http://www.gnu.org/licenses/>.
37 class FileIoHandler extends BaseMiddleware implements IoHandler {
39 * The *real* file input class we shall use for reading data
41 private $inputStream = NULL;
44 * The *real* file output class we shall use for reading data
46 private $outputStream = NULL;
49 * An instance of this class
51 private static $selfInstance = NULL;
54 * Protected constructor
58 protected function __construct () {
59 // Call parent constructor
60 parent::__construct(__CLASS__);
63 self::$selfInstance = $this;
67 * Creates an instance of this class and prepares the IO system. This is
68 * being done by setting the default file IO class
70 * @return $ioInstance A prepared instance of FilIOHandler
72 public static final function createFileIoHandler () {
74 $ioHandler = new FileIoHandler();
76 // Set the *real* file IO instances (both the same)
77 $ioHandler->setInputStream(ObjectFactory::createObjectByConfiguredName('file_input_class'));
78 $ioHandler->setOutputStream(ObjectFactory::createObjectByConfiguredName('file_output_class'));
85 * Getter for an instance of this class
87 * @return $selfInstance An instance of this class
89 public static final function getSelfInstance () {
90 return self::$selfInstance;
94 * Setter for the *real* file input instance
96 * @param $inputStream The *real* file-input class
99 public final function setInputStream (FileInputStreamer $inputStream) {
100 $this->inputStream = $inputStream;
104 * Getter for the *real* file input instance
106 * @return $inputStream The *real* file-input class
108 public final function getInputStream () {
109 return $this->inputStream;
113 * Setter for the *real* file output instance
115 * @param $outputStream The *real* file-output class
118 public final function setOutputStream (FileOutputStreamer $outputStream) {
119 $this->outputStream = $outputStream;
123 * Getter for the *real* file output instance
125 * @return $outputStream The *real* file-output class
127 public final function getOutputStream () {
128 return $this->outputStream;
131 * Saves streamed (that are mostly serialized objects) data to files or
134 * @param $fileName The local file's name including full path
135 * @param $dataArray Array containing the compressor's extension and streamed data
137 * @throws UnsupportedOperationException If this method is called
139 public function saveFile ($fileName, array $dataArray) {
140 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('fileName=' . $fileName . ',dataArray()=' . count($dataArray));
141 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
145 * Saves a file with data by using the current output stream
147 * @param $fileName Name of the file
148 * @param $dataStream File data stream
149 * @param $objectInstance An instance of a FrameworkInterface class (default: NULL)
152 public function saveStreamToFile ($fileName, $dataStream, FrameworkInterface $objectInstance = NULL) {
153 // Default is this array
154 $className = $this->__toString();
156 // Is the object instance set?
157 if ($objectInstance instanceof FrameworkInterface) {
159 $className = $objectInstance->__toString();
162 // Prepare output array
168 // Send the fileName and dataArray to the output handler
169 $this->getOutputStream()->saveFile($fileName, $dataArray);
172 /** Loads data from a file over the input handler
174 * @param $fqfn Given full-qualified file name (FQFN) to load
175 * @return $array Array with the file contents
177 public function loadFileContents ($fqfn) {
178 // Read from the input handler
179 return $this->getInputStream()->loadFileContents($fqfn);
183 * Determines seek position
185 * @return $seekPosition Current seek position
188 public function determineSeekPosition () {
189 $this->partialStub();
193 * Seek to given offset (default) or other possibilities as fseek() gives.
195 * @param $offset Offset to seek to (or used as "base" for other seeks)
196 * @param $whence Added to offset (default: only use offset to seek to)
197 * @return $status Status of file seek: 0 = success, -1 = failed
199 public function seek ($offset, $whence = SEEK_SET) {
200 $this->partialStub('offset=' . $offset . ',whence=' . $whence);
206 * @return $size Size (in bytes) of file
208 public function size () {
209 $this->partialStub();