Code sync from ship-simu code (all class config entries must end with _class!)
[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->generateUniqueId();
55
56                 // Set own instance
57                 self::$thisInstance = $this;
58         }
59
60         /**
61          * Creates an instance of this class and prepares the IO system. This is
62          * being done by setting the default file IO class
63          *
64          * @return      $ioInstance     A prepared instance of FilIOHandler
65          */
66         public final static function createFileIoHandler () {
67                 // Get instance
68                 $ioHandler = new FileIoHandler();
69
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'));
73
74                 // Return instance
75                 return $ioHandler;
76         }
77
78         /**
79          * Getter for an instance of this class
80          *
81          * @return      $thisInstance   An instance of this class
82          */
83         public final static function getInstance () {
84                 return self::$thisInstance;
85         }
86
87         /**
88          * Setter for the *real* file input instance
89          *
90          * @param               $inputStream    The *real* file-input class
91          */
92         public final function setInputStream (FileInputStreamer $inputStream) {
93                 $this->inputStream = $inputStream;
94         }
95
96         /**
97          * Getter for the *real* file input instance
98          *
99          * @return      $inputStream    The *real* file-input class
100          */
101         public final function getInputStream () {
102                 return $this->inputStream;
103         }
104
105         /**
106          * Setter for the *real* file output instance
107          *
108          * @param               $outputStream   The *real* file-output class
109          */
110         public final function setOutputStream (FileOutputStreamer $outputStream) {
111                 $this->outputStream = $outputStream;
112         }
113
114         /**
115          * Getter for the *real* file output instance
116          *
117          * @return      $outputStream   The *real* file-output class
118          */
119         public final function getOutputStream () {
120                 return $this->outputStream;
121         }
122
123         /**
124          * Saves a file with data by using the current output stream
125          *
126          * @see FileOutputStreamer
127          */
128         public function saveFile ($fileName, $dataArray) {
129                 // Get output stream
130                 $outInstance = $this->getOutputStream();
131
132                 // Is it a valid stream?
133                 if (is_null($outInstance)) {
134                         // No class returned
135                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
136                 } elseif (!is_object($outInstance)) {
137                         // Not an object! ;-(
138                         throw new NoObjectException($outInstance, self::EXCEPTION_IS_NO_OBJECT);
139                 } elseif (!method_exists($outInstance, 'saveFile')) {
140                         // Nope, so throw exception
141                         throw new MissingMethodException(array($outInstance, 'saveFile'), self::EXCEPTION_MISSING_METHOD);
142                 }
143
144                 // Send the fileName and dataArray to the output handler
145                 $outInstance->saveFile($fileName, $dataArray);
146         }
147
148         /** Loads data from a file over the input handler
149          *
150          * @see FileInputStreamer
151          */
152         public function loadFileContents ($fqfn) {
153                 // Initialize the array
154                 $array = array();
155
156                 // Get output stream
157                 $inInstance = $this->getInputStream();
158
159                 // Is it a valid stream?
160                 if (is_null($inInstance)) {
161                         // No class returned
162                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
163                 } elseif (!is_object($inInstance)) {
164                         // Not an object! ;-(
165                         throw new NoObjectException($inInstance, self::EXCEPTION_IS_NO_OBJECT);
166                 } elseif (!method_exists($inInstance, 'loadFileContents')) {
167                         // Nope, so throw exception
168                         throw new MissingMethodException(array($inInstance, 'loadFileContents'), self::EXCEPTION_MISSING_METHOD);
169                 }
170
171                 // Read from the input handler
172                 return $inInstance->loadFileContents($fqfn);
173         }
174 }
175
176 // [EOF]
177 ?>