3 namespace CoreFramework\Filesystem\File;
5 // Import framework stuff
6 use CoreFramework\Filesystem\CloseableFile;
7 use CoreFramework\Filesystem\FilePointer;
8 use CoreFramework\Object\BaseFrameworkSystem;
11 * An abstract file class
13 * @author Roland Haeder <webmaster@ship-simu.org>
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.ship-simu.org
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 class BaseAbstractFile extends BaseFrameworkSystem implements FilePointer, CloseableFile {
34 * Counter for total entries
36 private $totalEntries = 0;
39 * The current file we are working in
41 private $fileName = '';
44 * Protected constructor
46 * @param $className Name of the class
49 protected function __construct ($className) {
50 // Call parent constructor
51 parent::__construct($className);
55 * Destructor for cleaning purposes, etc
59 public final function __destruct() {
60 // Try to close a file
63 // Call the parent destructor
68 * "Getter" for abstracted file size
70 * @return $fileSize Size of abstracted file
72 public function getFileSize () {
73 // Call pointer instanze
74 return $this->getPointerInstance()->getFileSize();
78 * Getter for total entries
80 * @return $totalEntries Total entries in this file
82 public final function getCounter () {
84 return $this->totalEntries;
88 * Setter for total entries
90 * @param $totalEntries Total entries in this file
93 protected final function setCounter ($counter) {
95 $this->totalEntries = $counter;
103 protected final function incrementCounter () {
105 $this->totalEntries++;
109 * Getter for the file pointer
111 * @return $filePointer The file pointer which shall be a valid file resource
112 * @throws UnsupportedOperationException If this method is called
114 public final function getPointer () {
115 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
119 * Setter for file name
121 * @param $fileName The new file name
124 protected final function setFileName ($fileName) {
125 $fileName = (string) $fileName;
126 $this->fileName = $fileName;
130 * Getter for file name
132 * @return $fileName The current file name
134 public final function getFileName () {
135 return $this->fileName;
139 * Close a file source and set it's instance to null and the file name
144 public function closeFile () {
146 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: fileName=%s - CALLED!', __METHOD__, __LINE__, $this->getFileName()));
148 // Close down pointer instance as well by unsetting it
149 $this->unsetPointerInstance();
152 $this->setFileName('');
155 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: EXIT!', __METHOD__, __LINE__));
161 * @return $size Size (in bytes) of file
162 * @todo Handle seekStatus
164 public function size () {
165 // Call pointer instance
166 return $this->getPointerInstance()->size();
170 * Read data a file pointer
172 * @return mixed The result of fread()
173 * @throws NullPointerException If the file pointer instance
174 * is not set by setPointer()
175 * @throws InvalidResourceException If there is being set
177 public function readFromFile () {
178 // Call pointer instance
179 return $this->getPointerInstance()->readFromFile();
183 * Write data to a file pointer
185 * @param $dataStream The data stream we shall write to the file
186 * @return mixed Number of writes bytes or false on error
187 * @throws NullPointerException If the file pointer instance
188 * is not set by setPointer()
189 * @throws InvalidResourceException If there is being set
190 * an invalid file resource
192 public function writeToFile ($dataStream) {
193 // Call pointer instance
194 return $this->getPointerInstance()->writeToFile($dataStream);
198 * Determines whether the EOF has been reached
200 * @return $isEndOfFileReached Whether the EOF has been reached
202 public final function isEndOfFileReached () {
203 // Call pointer instance
204 return $this->getPointerInstance()->isEndOfFileReached();