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