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 - 2020 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) {
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 () {
87 $this->pointerInstance = NULL;
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 return $this->totalEntries;
124 * Setter for total entries
126 * @param $totalEntries Total entries in this file
129 protected final function setCounter ($counter) {
131 $this->totalEntries = $counter;
139 protected final function incrementCounter () {
141 $this->totalEntries++;
145 * Getter for the file object
147 * @return $fileObject An instance of a SplFileObject
149 public final function getFileObject () {
150 return $this->getPointerInstance()->getFileObject();
154 * Close a file source and set it's instance to null and the file name
159 public function closeFile () {
161 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: CALLED!', __METHOD__, __LINE__));
163 // Close down pointer instance as well by unsetting it
164 $this->unsetPointerInstance();
167 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: EXIT!', __METHOD__, __LINE__));
173 * @return $size Size (in bytes) of file
174 * @todo Handle seekStatus
176 public function size () {
177 // Call pointer instance
178 return $this->getPointerInstance()->size();
182 * Read data a file pointer
184 * @return mixed The result of fread()
185 * @throws NullPointerException If the file pointer instance
186 * is not set by setFileObject()
187 * @throws InvalidResourceException If there is being set
189 public function readFromFile () {
190 // Call pointer instance
191 return $this->getPointerInstance()->readFromFile();
195 * Write data to a file pointer
197 * @param $dataStream The data stream we shall write to the file
198 * @return mixed Number of writes bytes or false on error
199 * @throws NullPointerException If the file pointer instance
200 * is not set by setFileObject()
201 * @throws InvalidResourceException If there is being set
202 * an invalid file resource
204 public function writeToFile ($dataStream) {
205 // Call pointer instance
206 return $this->getPointerInstance()->writeToFile($dataStream);
210 * Determines whether the EOF has been reached
212 * @return $isEndOfFileReached Whether the EOF has been reached
214 public final function isEndOfFileReached () {
215 // Call pointer instance
216 return $this->getPointerInstance()->isEndOfFileReached();