3 namespace CoreFramework\Handler\Filesystem;
5 // Load framework stuff
6 use CoreFramework\Generic\FrameworkInterface;
9 * This is a file IO handler. It handles reading from and writing to files.
10 * Missing paths in writing process will be automatically created.
12 * @author Roland Haeder <webmaster@shipsimu.org>
14 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
15 * @license GNU GPL 3.0 or any newer version
16 * @link http://www.shipsimu.org
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 class FileIoHandler extends BaseMiddleware implements IoHandler {
33 * The *real* file input class we shall use for reading data
35 private $inputStream = NULL;
38 * The *real* file output class we shall use for reading data
40 private $outputStream = NULL;
43 * An instance of this class
45 private static $selfInstance = NULL;
48 * Protected constructor
52 protected function __construct () {
53 // Call parent constructor
54 parent::__construct(__CLASS__);
57 self::$selfInstance = $this;
61 * Creates an instance of this class and prepares the IO system. This is
62 * being done by setting the default file IO class
64 * @return $ioInstance A prepared instance of FilIOHandler
66 public static final function createFileIoHandler () {
68 $ioHandler = new FileIoHandler();
70 // Set the *real* file IO instances (both the same)
71 $ioHandler->setInputStream(ObjectFactory::createObjectByConfiguredName('file_input_class'));
72 $ioHandler->setOutputStream(ObjectFactory::createObjectByConfiguredName('file_output_class'));
79 * Getter for an instance of this class
81 * @return $selfInstance An instance of this class
83 public static final function getSelfInstance () {
84 return self::$selfInstance;
88 * Setter for the *real* file input instance
90 * @param $inputStream The *real* file-input class
93 public final function setInputStream (FileInputStreamer $inputStream) {
94 $this->inputStream = $inputStream;
98 * Getter for the *real* file input instance
100 * @return $inputStream The *real* file-input class
102 public final function getInputStream () {
103 return $this->inputStream;
107 * Setter for the *real* file output instance
109 * @param $outputStream The *real* file-output class
112 public final function setOutputStream (FileOutputStreamer $outputStream) {
113 $this->outputStream = $outputStream;
117 * Getter for the *real* file output instance
119 * @return $outputStream The *real* file-output class
121 public final function getOutputStream () {
122 return $this->outputStream;
125 * Saves streamed (that are mostly serialized objects) data to files or
128 * @param $fileName The local file's name including full path
129 * @param $dataArray Array containing the compressor's extension and streamed data
131 * @throws UnsupportedOperationException If this method is called
133 public function saveFile ($fileName, array $dataArray) {
134 self::createDebugInstance(__CLASS__)->debugOutput('fileName=' . $fileName . ',dataArray()=' . count($dataArray));
135 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
139 * Saves a file with data by using the current output stream
141 * @param $fileName Name of the file
142 * @param $dataStream File data stream
143 * @param $objectInstance An instance of a FrameworkInterface class (default: NULL)
146 public function saveStreamToFile ($fileName, $dataStream, FrameworkInterface $objectInstance = NULL) {
147 // Default is this array
148 $className = $this->__toString();
150 // Is the object instance set?
151 if ($objectInstance instanceof FrameworkInterface) {
153 $className = $objectInstance->__toString();
156 // Prepare output array
162 // Send the fileName and dataArray to the output handler
163 $this->getOutputStream()->saveFile($fileName, $dataArray);
166 /** Loads data from a file over the input handler
168 * @param $fqfn Given full-qualified file name (FQFN) to load
169 * @return $array Array with the file contents
171 public function loadFileContents ($fqfn) {
172 // Read from the input handler
173 return $this->getInputStream()->loadFileContents($fqfn);
177 * Determines seek position
179 * @return $seekPosition Current seek position
182 public function determineSeekPosition () {
183 $this->partialStub();
187 * Seek to given offset (default) or other possibilities as fseek() gives.
189 * @param $offset Offset to seek to (or used as "base" for other seeks)
190 * @param $whence Added to offset (default: only use offset to seek to)
191 * @return $status Status of file seek: 0 = success, -1 = failed
193 public function seek ($offset, $whence = SEEK_SET) {
194 $this->partialStub('offset=' . $offset . ',whence=' . $whence);
200 * @return $size Size (in bytes) of file
202 public function size () {
203 $this->partialStub();