(no commit message)
[shipsimu.git] / inc / classes / middleware / io / class_FileIOHandler.php
1 <?php
2 /**
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.
5  *
6  * @author              Roland Haeder <webmaster@ship-simu.org>
7  * @version             0.0
8  * @copyright   Copyright(c) 2007, 2008 Roland Haeder, this is free software
9  * @license             GNU GPL 3.0 or any newer version
10  *
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.
15  *
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.
20  *
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/>.
23  */
24 class FileIOHandler extends BaseMiddleware {
25         /**
26          * The *real* file input class we shall use for reading data
27          */
28         private $inputStream = null;
29
30         /**
31          * The *real* file output class we shall use for reading data
32          */
33         private $outputStream = null;
34
35         /**
36          * An instance of this class
37          */
38         private static $thisInstance = null;
39
40         /**
41          * Private constructor
42          *
43          * @return      void
44          */
45         private function __construct () {
46                 // Call parent constructor
47                 parent::constructor(__CLASS__);
48
49                 // Set description
50                 $this->setPartDescr("Datei-Ein-/Ausgabe-Handler");
51
52                 // Create an unique ID
53                 $this->createUniqueID();
54
55                 // Clean up a little
56                 $this->removeNumberFormaters();
57                 $this->removeSystemArray();
58
59                 // Set own instance
60                 self::$thisInstance = $this;
61         }
62
63         /**
64          * Creates an instance of this class and prepares the IO system. This is
65          * being done by setting the default file IO class
66          *
67          * @return      $ioInstance     A prepared instance of FilIOHandler
68          */
69         public final static function createFileIOHandler () {
70                 // Get instance
71                 $ioHandler = new FileIOHandler();
72
73                 // Set the *real* file IO instances (both the same)
74                 $ioHandler->setInputStream(FileIOStream::createFileIOStream());
75                 $ioHandler->setOutputStream(FileIOStream::createFileIOStream());
76
77                 // Return instance
78                 return $ioHandler;
79         }
80
81         /**
82          * Getter for an instance of this class
83          *
84          * @return      $thisInstance   An instance of this class
85          */
86         public final static function getInstance () {
87                 return self::$thisInstance;
88         }
89
90         /**
91          * Setter for the *real* file input instance
92          *
93          * @param               $inputStream    The *real* file-input class
94          */
95         public final function setInputStream (FileInputStreamer $inputStream) {
96                 $this->inputStream = $inputStream;
97         }
98
99         /**
100          * Getter for the *real* file input instance
101          *
102          * @return      $inputStream    The *real* file-input class
103          */
104         public final function getInputStream () {
105                 return $this->inputStream;
106         }
107
108         /**
109          * Setter for the *real* file output instance
110          *
111          * @param               $outputStream   The *real* file-output class
112          */
113         public final function setOutputStream (FileOutputStreamer $outputStream) {
114                 $this->outputStream = $outputStream;
115         }
116
117         /**
118          * Getter for the *real* file output instance
119          *
120          * @return      $outputStream   The *real* file-output class
121          */
122         public final function getOutputStream () {
123                 return $this->outputStream;
124         }
125
126         /**
127          * Saves a file with data by using the current output stream
128          *
129          * @see FileOutputStreamer
130          */
131         public function saveFile ($fileName, $dataArray) {
132                 // Get output stream
133                 $outInstance = $this->getOutputStream();
134
135                 // Is it a valid stream?
136                 if (is_null($outInstance)) {
137                         // No class returned
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);
145                 }
146
147                 // Send the fileName and dataArray to the output handler
148                 $outInstance->saveFile($fileName, $dataArray);
149         }
150
151         /** Loads data from a file over the input handler
152          *
153          * @see FileInputStreamer
154          */
155         public function loadFileContents ($fqfn) {
156                 // Initialize the array
157                 $array = array();
158
159                 // Get output stream
160                 $inInstance = $this->getInputStream();
161
162                 // Is it a valid stream?
163                 if (is_null($inInstance)) {
164                         // No class returned
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);
172                 }
173
174                 // Read from the input handler
175                 return $inInstance->loadFileContents($fqfn);
176         }
177 }
178
179 // [EOF]
180 ?>