3 * A class for writing files
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2013 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.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__);
44 * Destructor for cleaning purposes, etc
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
53 // Call the parent destructor
58 * Create a file pointer based on the given file. The file will also
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
67 public static final function createFrameworkFileOutputPointer ($fileName, $mode) {
68 // Some pre-sanity checks...
69 if (is_null($fileName)) {
71 throw new FileIsEmptyException(NULL, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
74 // Try to open a handler
75 $filePointer = @fopen($fileName, $mode);
76 if ((is_null($filePointer)) || ($filePointer === FALSE)) {
77 // Something bad happend
78 throw new FileIoException ($fileName, self::EXCEPTION_FILE_POINTER_INVALID);
81 // Create new instance
82 $pointerInstance = new FrameworkFileOutputPointer();
84 // Set file pointer and file name
85 $pointerInstance->setPointer($filePointer);
86 $pointerInstance->setFileName($fileName);
88 // Return the instance
89 return $pointerInstance;
93 * Write data to a file pointer
95 * @param $dataStream The data stream we shall write to the file
96 * @return mixed Number of writes bytes or FALSE on error
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
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);
111 // Write data to the file pointer and return written bytes
112 return fwrite($this->getPointer(), $dataStream);
116 * Close a file source and set it's instance to null and the file name
120 * @throws NullPointerException If the file pointer instance
121 * is not set by setPointer()
122 * @throws InvalidResourceException If there is being set
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);
133 // Close the file pointer and reset the instance variable
134 @fclose($this->getPointer());
135 $this->setPointer(NULL);
136 $this->setFileName('');
140 * Setter for the file pointer
142 * @param $filePointer File resource
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;
152 throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE);
157 * Getter for the file pointer
159 * @return $filePointer The file pointer which shall be a valid
162 public final function getPointer () {
163 return $this->filePointer;
167 * Setter for file name
169 * @param $fileName The new file name
172 public final function setFileName ($fileName) {
173 $fileName = (string) $fileName;
174 $this->fileName = $fileName;
178 * Getter for file name
180 * @return $fileName The current file name
182 public final function getFileName () {
183 return $this->fileName;