Base controller stub added
[shipsimu.git] / inc / classes / main / io / class_FrameworkFileOutputPointer.php
1 <?php
2 /**
3  * A class for writing files
4  *
5  * @author              Roland Haeder <webmaster@mxchange.org>
6  * @version             0.3.0
7  * @copyright   Copyright(c) 2007, 2008 Roland Haeder, this is free software
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.mxchange.org
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 FrameworkFileOutputPointer extends BaseFrameworkSystem {
25         /**
26          * The current file we are working in
27          */
28         private $fileName = "";
29
30         /**
31          * The file pointer
32          */
33         private $filePointer = null;
34
35         /**
36          * Private constructor
37          */
38         private final function __construct () {
39                 // Call parent constructor
40                 parent::constructor(__CLASS__);
41
42                 // Set part description
43                 $this->setObjectDescription("Dateiausgabe-Handler");
44
45                 // Create unique ID
46                 $this->createUniqueID();
47
48                 // Clean-up a little
49                 $this->removeNumberFormaters();
50         }
51
52         /**
53          * Destructor for cleaning purposes, etc
54          */
55         public final function __destruct() {
56                 // Is there a resource pointer? Then we have to close the file here!
57                 if (is_resource($this->getPointer())) {
58                         // Try to close a file
59                         $this->closeFile();
60                 }
61
62                 // Call the parent destructor
63                 parent::__destruct();
64         }
65
66         /**
67          * Create a file pointer based on the given file. The file will also
68          * be verified here.
69          *
70          * @param               $fileName                               The file name we shall pass
71          *                                                              to fopen()
72          * @param               $mode                           The output mode ('w', 'a' are valid)
73          * @throws      FileIsEmptyException    If the provided file name is empty.
74          * @throws      FilePointerNotOpened    If fopen() returns not a file
75          *                                                              resource
76          * @return      void
77          */
78         public final static function createFrameworkFileOutputPointer ($fileName, $mode) {
79                 // Some pre-sanity checks...
80                 if (is_null($fileName)) {
81                         // No filename given
82                         throw new FileIsEmptyException(null, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
83                 }
84
85                 // Try to open a handler
86                 $filePointer = @fopen($fileName, $mode);
87                 if (($filePointer === null) || ($filePointer === false)) {
88                         // Something bad happend
89                         throw new FilePointerNotOpenedException ($fileName, self::EXCEPTION_FILE_POINTER_INVALID);
90                 }
91
92                 // Create new instance
93                 $pointerInstance = new FrameworkFileOutputPointer();
94
95                 // Set file pointer and file name
96                 $pointerInstance->setPointer($filePointer);
97                 $pointerInstance->setFileName($fileName);
98
99                 // Return the instance
100                 return $pointerInstance;
101         }
102
103         /**
104          * Write data to a file pointer
105          *
106          * @param               $dataStream     The data stream we shall write to the file
107          * @return      mixed           The result of fwrite()
108          * @throws      NullPointerException    If the file pointer instance
109          *                                                              is not set by setPointer()
110          * @throws      InvalidFileResourceException    If there is being set
111          *                                                                      an invalid file resource
112          */
113         public function writeToFile ($dataStream) {
114                 if (is_null($this->getPointer())) {
115                         // Pointer not initialized
116                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
117                 } elseif (!is_resource($this->getPointer())) {
118                         // Pointer is not a valid resource!
119                         throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
120                 }
121
122                 // Read data from the file pointer and return it
123                 return fwrite($this->getPointer(), $dataStream);
124         }
125
126         /**
127          * Close a file source and set it's instance to null and the file name
128          * to empty
129          *
130          * @return      void
131          * @throws      NullPointerException    If the file pointer instance
132          *                                                              is not set by setPointer()
133          * @throws      InvalidFileResourceException    If there is being set
134          */
135         public function closeFile () {
136                 if (is_null($this->getPointer())) {
137                         // Pointer not initialized
138                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
139                 } elseif (!is_resource($this->getPointer())) {
140                         // Pointer is not a valid resource!
141                         throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
142                 }
143
144                 // Close the file pointer and reset the instance variable
145                 @fclose($this->getPointer());
146                 $this->setPointer(null);
147                 $this->setFileName("");
148         }
149
150         /**
151          * Setter for the file pointer
152          *
153          * @param               $filePointer    File resource
154          * @return      void
155          */
156         public final function setPointer ($filePointer) {
157                 // Sanity-check if the pointer is a valid file resource
158                 if (is_resource($filePointer) || is_null($filePointer)) {
159                         // Is a valid resource
160                         $this->filePointer = $filePointer;
161                 } else {
162                         // Throw exception
163                         throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
164                 }
165         }
166
167         /**
168          * Getter for the file pointer
169          *
170          * @return      $filePointer    The file pointer which shall be a valid
171          *                                              file resource
172          */
173         public final function getPointer () {
174                 return $this->filePointer;
175         }
176
177         /**
178          * Setter for file name
179          *
180          * @param               $fileName               The new file name
181          * @return      void
182          */
183         public final function setFileName ($fileName) {
184                 $fileName = (string) $fileName;
185                 $this->fileName = $fileName;
186         }
187
188         /**
189          * Getter for file name
190          *
191          * @return      $fileName               The current file name
192          */
193         public final function getFileName () {
194                 return $this->fileName;
195         }
196 }
197
198 // [EOF]
199 ?>