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