3 namespace CoreFramework\Handler\Filesystem;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Generic\FrameworkInterface;
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.
13 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
32 class FileIoHandler extends BaseMiddleware implements IoHandler {
34 * The *real* file input class we shall use for reading data
36 private $inputStream = NULL;
39 * The *real* file output class we shall use for reading data
41 private $outputStream = NULL;
44 * An instance of this class
46 private static $selfInstance = NULL;
49 * Protected constructor
53 protected function __construct () {
54 // Call parent constructor
55 parent::__construct(__CLASS__);
58 self::$selfInstance = $this;
62 * Creates an instance of this class and prepares the IO system. This is
63 * being done by setting the default file IO class
65 * @return $ioInstance A prepared instance of FilIOHandler
67 public static final function createFileIoHandler () {
69 $ioHandler = new FileIoHandler();
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'));
80 * Getter for an instance of this class
82 * @return $selfInstance An instance of this class
84 public static final function getSelfInstance () {
85 return self::$selfInstance;
89 * Setter for the *real* file input instance
91 * @param $inputStream The *real* file-input class
94 public final function setInputStream (FileInputStreamer $inputStream) {
95 $this->inputStream = $inputStream;
99 * Getter for the *real* file input instance
101 * @return $inputStream The *real* file-input class
103 public final function getInputStream () {
104 return $this->inputStream;
108 * Setter for the *real* file output instance
110 * @param $outputStream The *real* file-output class
113 public final function setOutputStream (FileOutputStreamer $outputStream) {
114 $this->outputStream = $outputStream;
118 * Getter for the *real* file output instance
120 * @return $outputStream The *real* file-output class
122 public final function getOutputStream () {
123 return $this->outputStream;
126 * Saves streamed (that are mostly serialized objects) data to files or
129 * @param $fileName The local file's name including full path
130 * @param $dataArray Array containing the compressor's extension and streamed data
132 * @throws UnsupportedOperationException If this method is called
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);
140 * Saves a file with data by using the current output stream
142 * @param $fileName Name of the file
143 * @param $dataStream File data stream
144 * @param $objectInstance An instance of a FrameworkInterface class (default: NULL)
147 public function saveStreamToFile ($fileName, $dataStream, FrameworkInterface $objectInstance = NULL) {
148 // Default is this array
149 $className = $this->__toString();
151 // Is the object instance set?
152 if ($objectInstance instanceof FrameworkInterface) {
154 $className = $objectInstance->__toString();
157 // Prepare output array
163 // Send the fileName and dataArray to the output handler
164 $this->getOutputStream()->saveFile($fileName, $dataArray);
167 /** Loads data from a file over the input handler
169 * @param $fqfn Given full-qualified file name (FQFN) to load
170 * @return $array Array with the file contents
172 public function loadFileContents ($fqfn) {
173 // Read from the input handler
174 return $this->getInputStream()->loadFileContents($fqfn);
178 * Determines seek position
180 * @return $seekPosition Current seek position
183 public function determineSeekPosition () {
184 $this->partialStub();
188 * Seek to given offset (default) or other possibilities as fseek() gives.
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
194 public function seek ($offset, $whence = SEEK_SET) {
195 $this->partialStub('offset=' . $offset . ',whence=' . $whence);
201 * @return $size Size (in bytes) of file
203 public function size () {
204 $this->partialStub();