3 namespace CoreFramework\Filesystem\File;
5 // Import framework stuff
6 use CoreFramework\Filesystem\CloseableFile;
7 use CoreFramework\Filesystem\FilePointer;
8 use CoreFramework\Generic\UnsupportedOperationException;
9 use 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 - 2017 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 class BaseAbstractFile extends BaseFrameworkSystem implements FilePointer, CloseableFile {
35 * Counter for total entries
37 private $totalEntries = 0;
40 * Protected constructor
42 * @param $className Name of the class
45 protected function __construct ($className) {
46 // Call parent constructor
47 parent::__construct($className);
51 * Destructor for cleaning purposes, etc
55 public final function __destruct() {
56 // Try to close a file
59 // Call the parent destructor
64 * "Getter" for abstracted file size
66 * @return $fileSize Size of abstracted file
68 public function getFileSize () {
69 // Call pointer instanze
70 return $this->getPointerInstance()->getFileSize();
74 * Getter for total entries
76 * @return $totalEntries Total entries in this file
78 public final function getCounter () {
80 return $this->totalEntries;
84 * Setter for total entries
86 * @param $totalEntries Total entries in this file
89 protected final function setCounter ($counter) {
91 $this->totalEntries = $counter;
99 protected final function incrementCounter () {
101 $this->totalEntries++;
105 * Getter for the file object
107 * @return $fileObject An instance of a SplFileObject
108 * @throws UnsupportedOperationException If this method is called
110 public final function getFileObject () {
111 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
115 * Close a file source and set it's instance to null and the file name
120 public function closeFile () {
122 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: CALLED!', __METHOD__, __LINE__));
124 // Close down pointer instance as well by unsetting it
125 $this->unsetPointerInstance();
128 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: EXIT!', __METHOD__, __LINE__));
134 * @return $size Size (in bytes) of file
135 * @todo Handle seekStatus
137 public function size () {
138 // Call pointer instance
139 return $this->getPointerInstance()->size();
143 * Read data a file pointer
145 * @return mixed The result of fread()
146 * @throws NullPointerException If the file pointer instance
147 * is not set by setPointer()
148 * @throws InvalidResourceException If there is being set
150 public function readFromFile () {
151 // Call pointer instance
152 return $this->getPointerInstance()->readFromFile();
156 * Write data to a file pointer
158 * @param $dataStream The data stream we shall write to the file
159 * @return mixed Number of writes bytes or false on error
160 * @throws NullPointerException If the file pointer instance
161 * is not set by setPointer()
162 * @throws InvalidResourceException If there is being set
163 * an invalid file resource
165 public function writeToFile ($dataStream) {
166 // Call pointer instance
167 return $this->getPointerInstance()->writeToFile($dataStream);
171 * Determines whether the EOF has been reached
173 * @return $isEndOfFileReached Whether the EOF has been reached
175 public final function isEndOfFileReached () {
176 // Call pointer instance
177 return $this->getPointerInstance()->isEndOfFileReached();