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