]> git.mxchange.org Git - core.git/blob - framework/main/classes/file_directories/class_BaseAbstractFile.php
Continued:
[core.git] / framework / main / classes / file_directories / class_BaseAbstractFile.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Filesystem\File;
4
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;
10
11 /**
12  * An abstract file class
13  *
14  * @author              Roland Haeder <webmaster@ship-simu.org>
15  * @version             0.0.0
16  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2022 Core Developer Team
17  * @license             GNU GPL 3.0 or any newer version
18  * @link                http://www.ship-simu.org
19  *
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.
24  *
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.
29  *
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/>.
32  */
33 abstract class BaseAbstractFile extends BaseFrameworkSystem implements FilePointer, CloseableFile {
34         /**
35          * Counter for total entries
36          */
37         private $totalEntries = 0;
38
39         /**
40          * An instance of a file I/O pointer class (not handler)
41          */
42         private $pointerInstance = NULL;
43
44         /**
45          * Protected constructor
46          *
47          * @param       $className      Name of the class
48          * @return      void
49          */
50         protected function __construct (string $className) {
51                 // Call parent constructor
52                 parent::__construct($className);
53         }
54
55         /**
56          * Setter for FilePointer instance
57          *
58          * @param       $pointerInstance        An instance of an FilePointer class
59          * @return      void
60          */
61         protected final function setPointerInstance (FilePointer $pointerInstance = NULL) {
62                 $this->pointerInstance = $pointerInstance;
63         }
64
65         /**
66          * Getter for FilePointer instance
67          *
68          * @return      $pointerInstance        An instance of an FilePointer class
69          */
70         public final function getPointerInstance () {
71                 return $this->pointerInstance;
72         }
73
74         /**
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.
78          *
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.
82          *
83          * @return      void
84          */
85         protected final function unsetPointerInstance () {
86                 // Simply invoke setter with no parameter
87                 $this->setPointerInstance();
88         }
89
90         /**
91          * Destructor for cleaning purposes, etc
92          *
93          * @return      void
94          */
95         public final function __destruct() {
96                 // Try to close a file
97                 $this->closeFile();
98
99                 // Call the parent destructor
100                 parent::__destruct();
101         }
102
103         /**
104          * "Getter" for abstracted file size
105          *
106          * @return      $fileSize       Size of abstracted file
107          */
108         public function getFileSize () {
109                 // Call pointer instanze
110                 return $this->getPointerInstance()->getFileSize();
111         }
112
113         /**
114          * Getter for total entries
115          *
116          * @return      $totalEntries   Total entries in this file
117          */
118         public final function getCounter () {
119                 // Get it
120                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-ABSTRACT-FILE: Getting this->totalEntries=%d ... - CALLED!', $this->totalEntries));
121                 return $this->totalEntries;
122         }
123
124         /**
125          * Setter for total entries
126          *
127          * @param       $totalEntries   Total entries in this file
128          * @return      void
129          */
130         protected final function setCounter (int $counter) {
131                 // Set it
132                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('BASE-ABSTRACT-FILE: Setting this->totalEntries=%d ... - CALLED!', $counter));
133                 $this->totalEntries = $counter;
134         }
135
136         /**
137          * Increment counter
138          *
139          * @return      void
140          */
141         protected final function incrementCounter () {
142                 // Get it
143                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-ABSTRACT-FILE: CALLED!');
144                 $this->totalEntries++;
145
146                 // Trace message
147                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-ABSTRACT-FILE: EXIT!');
148         }
149
150         /**
151          * Getter for the file object
152          *
153          * @return      $fileObject             An instance of a SplFileObject
154          */
155         public final function getFileObject () {
156                 return $this->getPointerInstance()->getFileObject();
157         }
158
159         /**
160          * Getter for file's name
161          */
162         public final function getFilename () {
163                 // Invole file object's method
164                 return $this->getFileObject()->getFilename();
165         }
166
167         /**
168          * Close a file source and set it's instance to null and the file name
169          * to empty
170          *
171          * @return      void
172          */
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();
177
178                 // Trace message
179                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('BASE-ABSTRACT-FILE: EXIT!');
180         }
181
182         /**
183          * Size of this file
184          *
185          * @return      $size   Size (in bytes) of file
186          * @todo        Handle seekStatus
187          */
188         public function size () {
189                 // Call pointer instance
190                 return $this->getPointerInstance()->size();
191         }
192
193         /**
194          * Read data a file pointer
195          *
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
200          */
201         public function readFromFile () {
202                 // Call pointer instance
203                 return $this->getPointerInstance()->readFromFile();
204         }
205
206         /**
207          * Write data to a file pointer
208          *
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
215          */
216         public function writeToFile (string $dataStream) {
217                 // Call pointer instance
218                 return $this->getPointerInstance()->writeToFile($dataStream);
219         }
220
221         /**
222          * Determines whether the EOF has been reached
223          *
224          * @return      $isEndOfFileReached             Whether the EOF has been reached
225          */
226         public final function isEndOfFileReached () {
227                 // Call pointer instance
228                 return $this->getPointerInstance()->isEndOfFileReached();
229         }
230
231 }