3 * A class for writing files
5 * @author Roland Haeder <webmaster@mxchange.org>
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.ship-simu.org
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.
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.
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/>.
24 class FrameworkFileOutputPointer extends BaseFrameworkSystem {
26 * The current file we are working in
28 private $fileName = "";
33 private $filePointer = null;
36 * Protected constructor
38 protected function __construct () {
39 // Call parent constructor
40 parent::__construct(__CLASS__);
42 // Set part description
43 $this->setObjectDescription("Handler for writing to files");
46 $this->createUniqueID();
49 $this->removeNumberFormaters();
53 * Destructor for cleaning purposes, etc
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
62 // Call the parent destructor
67 * Create a file pointer based on the given file. The file will also
70 * @param $fileName The file name we shall pass
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
78 public final static function createFrameworkFileOutputPointer ($fileName, $mode) {
79 // Some pre-sanity checks...
80 if (is_null($fileName)) {
82 throw new FileIsEmptyException(null, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
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);
92 // Create new instance
93 $pointerInstance = new FrameworkFileOutputPointer();
95 // Set file pointer and file name
96 $pointerInstance->setPointer($filePointer);
97 $pointerInstance->setFileName($fileName);
99 // Return the instance
100 return $pointerInstance;
104 * Write data to a file pointer
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
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);
122 // Read data from the file pointer and return it
123 return fwrite($this->getPointer(), $dataStream);
127 * Close a file source and set it's instance to null and the file name
131 * @throws NullPointerException If the file pointer instance
132 * is not set by setPointer()
133 * @throws InvalidFileResourceException If there is being set
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);
144 // Close the file pointer and reset the instance variable
145 @fclose($this->getPointer());
146 $this->setPointer(null);
147 $this->setFileName("");
151 * Setter for the file pointer
153 * @param $filePointer File resource
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;
163 throw new InvalidFileResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
168 * Getter for the file pointer
170 * @return $filePointer The file pointer which shall be a valid
173 public final function getPointer () {
174 return $this->filePointer;
178 * Setter for file name
180 * @param $fileName The new file name
183 public final function setFileName ($fileName) {
184 $fileName = (string) $fileName;
185 $this->fileName = $fileName;
189 * Getter for file name
191 * @return $fileName The current file name
193 public final function getFileName () {
194 return $this->fileName;