3 namespace Org\Mxchange\CoreFramework\Filesystem\File;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Filesystem\CloseableFile;
7 use Org\Mxchange\CoreFramework\Filesystem\FilePointer;
8 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
9 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
12 * An abstract file class
14 * @author Roland Haeder <webmaster@ship-simu.org>
16 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2021 Core Developer Team
17 * @license GNU GPL 3.0 or any newer version
18 * @link http://www.ship-simu.org
20 * This program is free software: you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation, either version 3 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 abstract class BaseAbstractFile extends BaseFrameworkSystem implements FilePointer, CloseableFile {
35 * Counter for total entries
37 private $totalEntries = 0;
40 * An instance of a file I/O pointer class (not handler)
42 private $pointerInstance = NULL;
45 * Protected constructor
47 * @param $className Name of the class
50 protected function __construct (string $className) {
51 // Call parent constructor
52 parent::__construct($className);
56 * Setter for FilePointer instance
58 * @param $pointerInstance An instance of an FilePointer class
61 protected final function setPointerInstance (FilePointer $pointerInstance = NULL) {
62 $this->pointerInstance = $pointerInstance;
66 * Getter for FilePointer instance
68 * @return $pointerInstance An instance of an FilePointer class
70 public final function getPointerInstance () {
71 return $this->pointerInstance;
75 * Unsets pointer instance which triggers a call of __destruct() if the
76 * instance is still there. This is surely not fatal on already "closed"
77 * file pointer instances.
79 * I don't want to mess around with above setter by giving it a default
80 * value NULL as setter should always explicitly only set (existing) object
81 * instances and NULL is NULL.
85 protected final function unsetPointerInstance () {
86 // Simply invoke setter with no parameter
87 $this->setPointerInstance();
91 * Destructor for cleaning purposes, etc
95 public final function __destruct() {
96 // Try to close a file
99 // Call the parent destructor
100 parent::__destruct();
104 * "Getter" for abstracted file size
106 * @return $fileSize Size of abstracted file
108 public function getFileSize () {
109 // Call pointer instanze
110 return $this->getPointerInstance()->getFileSize();
114 * Getter for total entries
116 * @return $totalEntries Total entries in this file
118 public final function getCounter () {
120 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-ABSTRACT-FILE: Getting this->totalEntries=%d ... - CALLED!', $this->totalEntries));
121 return $this->totalEntries;
125 * Setter for total entries
127 * @param $totalEntries Total entries in this file
130 protected final function setCounter (int $counter) {
132 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-ABSTRACT-FILE: Setting this->totalEntries=%d ... - CALLED!', $counter));
133 $this->totalEntries = $counter;
141 protected final function incrementCounter () {
143 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-ABSTRACT-FILE: CALLED!');
144 $this->totalEntries++;
147 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-ABSTRACT-FILE: EXIT!');
151 * Getter for the file object
153 * @return $fileObject An instance of a SplFileObject
155 public final function getFileObject () {
156 return $this->getPointerInstance()->getFileObject();
160 * Getter for file's name
162 public final function getFilename () {
163 // Invole file object's method
164 return $this->getFileObject()->getFilename();
168 * Close a file source and set it's instance to null and the file name
173 public function closeFile () {
174 // Close down pointer instance as well by unsetting it
175 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-ABSTRACT-FILE: CALLED!');
176 $this->unsetPointerInstance();
179 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-ABSTRACT-FILE: EXIT!');
185 * @return $size Size (in bytes) of file
186 * @todo Handle seekStatus
188 public function size () {
189 // Call pointer instance
190 return $this->getPointerInstance()->size();
194 * Read data a file pointer
196 * @return mixed The result of fread()
197 * @throws NullPointerException If the file pointer instance
198 * is not set by setFileObject()
199 * @throws InvalidResourceException If there is being set
201 public function readFromFile () {
202 // Call pointer instance
203 return $this->getPointerInstance()->readFromFile();
207 * Write data to a file pointer
209 * @param $dataStream The data stream we shall write to the file
210 * @return mixed Number of writes bytes or false on error
211 * @throws NullPointerException If the file pointer instance
212 * is not set by setFileObject()
213 * @throws InvalidResourceException If there is being set
214 * an invalid file resource
216 public function writeToFile (string $dataStream) {
217 // Call pointer instance
218 return $this->getPointerInstance()->writeToFile($dataStream);
222 * Determines whether the EOF has been reached
224 * @return $isEndOfFileReached Whether the EOF has been reached
226 public final function isEndOfFileReached () {
227 // Call pointer instance
228 return $this->getPointerInstance()->isEndOfFileReached();