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@ship-simu.org>
8 * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software
9 * @license GNU GPL 3.0 or any newer version
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class FileIOHandler extends BaseMiddleware {
26 * The *real* file input class we shall use for reading data
28 private $inputStream = null;
31 * The *real* file output class we shall use for reading data
33 private $outputStream = null;
36 * An instance of this class
38 private static $thisInstance = null;
45 private function __construct () {
46 // Call parent constructor
47 parent::constructor(__CLASS__);
50 $this->setPartDescr("Datei-Ein-/Ausgabe-Handler");
52 // Create an unique ID
53 $this->createUniqueID();
56 $this->removeNumberFormaters();
57 $this->removeSystemArray();
60 self::$thisInstance = $this;
64 * Creates an instance of this class and prepares the IO system. This is
65 * being done by setting the default file IO class
67 * @return $ioInstance A prepared instance of FilIOHandler
69 public final static function createFileIOHandler () {
71 $ioHandler = new FileIOHandler();
73 // Set the *real* file IO instances (both the same)
74 $ioHandler->setInputStream(FileIOStream::createFileIOStream());
75 $ioHandler->setOutputStream(FileIOStream::createFileIOStream());
82 * Getter for an instance of this class
84 * @return $thisInstance An instance of this class
86 public final static function getInstance () {
87 return self::$thisInstance;
91 * Setter for the *real* file input instance
93 * @param $inputStream The *real* file-input class
95 public final function setInputStream (FileInputStreamer $inputStream) {
96 $this->inputStream = $inputStream;
100 * Getter for the *real* file input instance
102 * @return $inputStream The *real* file-input class
104 public final function getInputStream () {
105 return $this->inputStream;
109 * Setter for the *real* file output instance
111 * @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;
127 * Saves a file with data by using the current output stream
129 * @see FileOutputStreamer
131 public function saveFile ($fileName, $dataArray) {
133 $outInstance = $this->getOutputStream();
135 // Is it a valid stream?
136 if (is_null($outInstance)) {
138 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
139 } elseif (!is_object($outInstance)) {
140 // Not an object! ;-(
141 throw new NoObjectException($outInstance, self::EXCEPTION_IS_NO_OBJECT);
142 } elseif (!method_exists($outInstance, 'saveFile')) {
143 // Nope, so throw exception
144 throw new MissingMethodException(array($outInstance, 'saveFile'), self::EXCEPTION_MISSING_METHOD);
147 // Send the fileName and dataArray to the output handler
148 $outInstance->saveFile($fileName, $dataArray);
151 /** Loads data from a file over the input handler
153 * @see FileInputStreamer
155 public function loadFileContents ($fqfn) {
156 // Initialize the array
160 $inInstance = $this->getInputStream();
162 // Is it a valid stream?
163 if (is_null($inInstance)) {
165 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
166 } elseif (!is_object($inInstance)) {
167 // Not an object! ;-(
168 throw new NoObjectException($inInstance, self::EXCEPTION_IS_NO_OBJECT);
169 } elseif (!method_exists($inInstance, 'loadFileContents')) {
170 // Nope, so throw exception
171 throw new MissingMethodException(array($inInstance, 'loadFileContents'), self::EXCEPTION_MISSING_METHOD);
174 // Read from the input handler
175 return $inInstance->loadFileContents($fqfn);