3 namespace CoreFramework\Filesystem\File;
5 // Import framework stuff
6 use CoreFramework\Object\BaseFrameworkSystem;
9 * An abstract file class
11 * @author Roland Haeder <webmaster@ship-simu.org>
13 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14 * @license GNU GPL 3.0 or any newer version
15 * @link http://www.ship-simu.org
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 class BaseAbstractFile extends BaseFrameworkSystem implements FilePointer, CloseableFile {
32 * Counter for total entries
34 private $totalEntries = 0;
37 * The current file we are working in
39 private $fileName = '';
42 * Protected constructor
44 * @param $className Name of the class
47 protected function __construct ($className) {
48 // Call parent constructor
49 parent::__construct($className);
53 * Destructor for cleaning purposes, etc
57 public final function __destruct() {
58 // Try to close a file
61 // Call the parent destructor
66 * "Getter" for abstracted file size
68 * @return $fileSize Size of abstracted file
70 public function getFileSize () {
71 // Call pointer instanze
72 return $this->getPointerInstance()->getFileSize();
76 * Getter for total entries
78 * @return $totalEntries Total entries in this file
80 public final function getCounter () {
82 return $this->totalEntries;
86 * Setter for total entries
88 * @param $totalEntries Total entries in this file
91 protected final function setCounter ($counter) {
93 $this->totalEntries = $counter;
101 protected final function incrementCounter () {
103 $this->totalEntries++;
107 * Getter for the file pointer
109 * @return $filePointer The file pointer which shall be a valid file resource
110 * @throws UnsupportedOperationException If this method is called
112 public final function getPointer () {
113 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
117 * Setter for file name
119 * @param $fileName The new file name
122 protected final function setFileName ($fileName) {
123 $fileName = (string) $fileName;
124 $this->fileName = $fileName;
128 * Getter for file name
130 * @return $fileName The current file name
132 public final function getFileName () {
133 return $this->fileName;
137 * Close a file source and set it's instance to null and the file name
142 public function closeFile () {
144 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: fileName=%s - CALLED!', __METHOD__, __LINE__, $this->getFileName()));
146 // Close down pointer instance as well by unsetting it
147 $this->unsetPointerInstance();
150 $this->setFileName('');
153 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: EXIT!', __METHOD__, __LINE__));
159 * @return $size Size (in bytes) of file
160 * @todo Handle seekStatus
162 public function size () {
163 // Call pointer instance
164 return $this->getPointerInstance()->size();
168 * Read data a file pointer
170 * @return mixed The result of fread()
171 * @throws NullPointerException If the file pointer instance
172 * is not set by setPointer()
173 * @throws InvalidResourceException If there is being set
175 public function readFromFile () {
176 // Call pointer instance
177 return $this->getPointerInstance()->readFromFile();
181 * Write data to a file pointer
183 * @param $dataStream The data stream we shall write to the file
184 * @return mixed Number of writes bytes or FALSE on error
185 * @throws NullPointerException If the file pointer instance
186 * is not set by setPointer()
187 * @throws InvalidResourceException If there is being set
188 * an invalid file resource
190 public function writeToFile ($dataStream) {
191 // Call pointer instance
192 return $this->getPointerInstance()->writeToFile($dataStream);
196 * Determines whether the EOF has been reached
198 * @return $isEndOfFileReached Whether the EOF has been reached
200 public final function isEndOfFileReached () {
201 // Call pointer instance
202 return $this->getPointerInstance()->isEndOfFileReached();