3 * This is a file IO handler. It handles reading from and writing to files.
4 * Missing paths in writing process will be automatically created.
6 * @author Roland Haeder <webmaster@mxchange.org>
8 * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software
9 * @license GNU GPL 3.0 or any newer version
10 * @link http://www.ship-simu.org
12 * This program is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 class FileIOHandler extends BaseMiddleware {
27 * The *real* file input class we shall use for reading data
29 private $inputStream = null;
32 * The *real* file output class we shall use for reading data
34 private $outputStream = null;
37 * An instance of this class
39 private static $thisInstance = null;
42 * Protected constructor
46 protected function __construct () {
47 // Call parent constructor
48 parent::__construct(__CLASS__);
51 $this->setObjectDescription("Datei-Ein-/Ausgabe-Handler");
53 // Create an unique ID
54 $this->createUniqueID();
57 $this->removeNumberFormaters();
58 $this->removeSystemArray();
61 self::$thisInstance = $this;
65 * Creates an instance of this class and prepares the IO system. This is
66 * being done by setting the default file IO class
68 * @return $ioInstance A prepared instance of FilIOHandler
70 public final static function createFileIOHandler () {
72 $ioHandler = new FileIOHandler();
74 // Set the *real* file IO instances (both the same)
75 $ioHandler->setInputStream(FileIOStream::createFileIOStream());
76 $ioHandler->setOutputStream(FileIOStream::createFileIOStream());
83 * Getter for an instance of this class
85 * @return $thisInstance An instance of this class
87 public final static function getInstance () {
88 return self::$thisInstance;
92 * Setter for the *real* file input instance
94 * @param $inputStream The *real* file-input class
96 public final function setInputStream (FileInputStreamer $inputStream) {
97 $this->inputStream = $inputStream;
101 * Getter for the *real* file input instance
103 * @return $inputStream The *real* file-input class
105 public final function getInputStream () {
106 return $this->inputStream;
110 * Setter for the *real* file output instance
112 * @param $outputStream The *real* file-output class
114 public final function setOutputStream (FileOutputStreamer $outputStream) {
115 $this->outputStream = $outputStream;
119 * Getter for the *real* file output instance
121 * @return $outputStream The *real* file-output class
123 public final function getOutputStream () {
124 return $this->outputStream;
128 * Saves a file with data by using the current output stream
130 * @see FileOutputStreamer
132 public function saveFile ($fileName, $dataArray) {
134 $outInstance = $this->getOutputStream();
136 // Is it a valid stream?
137 if (is_null($outInstance)) {
139 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
140 } elseif (!is_object($outInstance)) {
141 // Not an object! ;-(
142 throw new NoObjectException($outInstance, self::EXCEPTION_IS_NO_OBJECT);
143 } elseif (!method_exists($outInstance, 'saveFile')) {
144 // Nope, so throw exception
145 throw new MissingMethodException(array($outInstance, 'saveFile'), self::EXCEPTION_MISSING_METHOD);
148 // Send the fileName and dataArray to the output handler
149 $outInstance->saveFile($fileName, $dataArray);
152 /** Loads data from a file over the input handler
154 * @see FileInputStreamer
156 public function loadFileContents ($fqfn) {
157 // Initialize the array
161 $inInstance = $this->getInputStream();
163 // Is it a valid stream?
164 if (is_null($inInstance)) {
166 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
167 } elseif (!is_object($inInstance)) {
168 // Not an object! ;-(
169 throw new NoObjectException($inInstance, self::EXCEPTION_IS_NO_OBJECT);
170 } elseif (!method_exists($inInstance, 'loadFileContents')) {
171 // Nope, so throw exception
172 throw new MissingMethodException(array($inInstance, 'loadFileContents'), self::EXCEPTION_MISSING_METHOD);
175 // Read from the input handler
176 return $inInstance->loadFileContents($fqfn);