2925a76cbc149ffee6864abe2b01fbfc79f7a4ec
[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, 2010 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      FileIoException         If fopen() returns not a file resource
65          * @return      void
66          */
67         public final static function createFrameworkFileOutputPointer ($fileName, $mode) {
68                 // Some pre-sanity checks...
69                 if (is_null($fileName)) {
70                         // No filename given
71                         throw new FileIsEmptyException(null, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
72                 } // END - if
73
74                 // Try to open a handler
75                 $filePointer = @fopen($fileName, $mode);
76                 if (($filePointer === null) || ($filePointer === false)) {
77                         // Something bad happend
78                         throw new FileIoException ($fileName, self::EXCEPTION_FILE_POINTER_INVALID);
79                 } // END - if
80
81                 // Create new instance
82                 $pointerInstance = new FrameworkFileOutputPointer();
83
84                 // Set file pointer and file name
85                 $pointerInstance->setPointer($filePointer);
86                 $pointerInstance->setFileName($fileName);
87
88                 // Return the instance
89                 return $pointerInstance;
90         }
91
92         /**
93          * Write data to a file pointer
94          *
95          * @param       $dataStream             The data stream we shall write to the file
96          * @return      mixed                   The result of fwrite()
97          * @throws      NullPointerException    If the file pointer instance
98          *                                                                      is not set by setPointer()
99          * @throws      InvalidResourceException        If there is being set
100          *                                                                                      an invalid file resource
101          */
102         public function writeToFile ($dataStream) {
103                 if (is_null($this->getPointer())) {
104                         // Pointer not initialized
105                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
106                 } elseif (!is_resource($this->getPointer())) {
107                         // Pointer is not a valid resource!
108                         throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE);
109                 }
110
111                 // Read data from the file pointer and return it
112                 return fwrite($this->getPointer(), $dataStream);
113         }
114
115         /**
116          * Close a file source and set it's instance to null and the file name
117          * to empty
118          *
119          * @return      void
120          * @throws      NullPointerException    If the file pointer instance
121          *                                                                      is not set by setPointer()
122          * @throws      InvalidResourceException        If there is being set
123          */
124         public function closeFile () {
125                 if (is_null($this->getPointer())) {
126                         // Pointer not initialized
127                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
128                 } elseif (!is_resource($this->getPointer())) {
129                         // Pointer is not a valid resource!
130                         throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE);
131                 }
132
133                 // Close the file pointer and reset the instance variable
134                 @fclose($this->getPointer());
135                 $this->setPointer(null);
136                 $this->setFileName('');
137         }
138
139         /**
140          * Setter for the file pointer
141          *
142          * @param       $filePointer    File resource
143          * @return      void
144          */
145         public final function setPointer ($filePointer) {
146                 // Sanity-check if pointer is a valid file resource
147                 if (is_resource($filePointer) || is_null($filePointer)) {
148                         // Is a valid resource
149                         $this->filePointer = $filePointer;
150                 } else {
151                         // Throw exception
152                         throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE);
153                 }
154         }
155
156         /**
157          * Getter for the file pointer
158          *
159          * @return      $filePointer    The file pointer which shall be a valid
160          *                                                      file resource
161          */
162         public final function getPointer () {
163                 return $this->filePointer;
164         }
165
166         /**
167          * Setter for file name
168          *
169          * @param       $fileName       The new file name
170          * @return      void
171          */
172         public final function setFileName ($fileName) {
173                 $fileName = (string) $fileName;
174                 $this->fileName = $fileName;
175         }
176
177         /**
178          * Getter for file name
179          *
180          * @return      $fileName       The current file name
181          */
182         public final function getFileName () {
183                 return $this->fileName;
184         }
185 }
186
187 // [EOF]
188 ?>