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
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->generateUniqueId();
57 self::$thisInstance = $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 final static 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 $thisInstance An instance of this class
83 public final static function getInstance () {
84 return self::$thisInstance;
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;
126 * Saves a file with data by using the current output stream
128 * @param $fileName Name of the file
129 * @param $dataArray Array with file contents
131 * @see FileOutputStreamer
133 public function saveFile ($fileName, $dataArray) {
135 $outInstance = $this->getOutputStream();
137 // Send the fileName and dataArray to the output handler
138 $outInstance->saveFile($fileName, $dataArray);
141 /** Loads data from a file over the input handler
143 * @return $array Array with the file contents
144 * @see FileInputStreamer
146 public function loadFileContents ($fqfn) {
148 $inInstance = $this->getInputStream();
150 // Read from the input handler
151 return $inInstance->loadFileContents($fqfn);