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\FrameworkInterface;
9 use Org\Mxchange\CoreFramework\Generic\UnsupportedOperationException;
10 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
11
12 // Import SPL stuff
13 use \InvalidArgumentException;
14
15 /**
16  * An abstract file class
17  *
18  * @author              Roland Haeder <webmaster@ship-simu.org>
19  * @version             0.0.0
20  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
21  * @license             GNU GPL 3.0 or any newer version
22  * @link                http://www.ship-simu.org
23  *
24  * This program is free software: you can redistribute it and/or modify
25  * it under the terms of the GNU General Public License as published by
26  * the Free Software Foundation, either version 3 of the License, or
27  * (at your option) any later version.
28  *
29  * This program is distributed in the hope that it will be useful,
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32  * GNU General Public License for more details.
33  *
34  * You should have received a copy of the GNU General Public License
35  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36  */
37 abstract class BaseAbstractFile extends BaseFrameworkSystem implements FilePointer, CloseableFile {
38         /**
39          * Counter for total entries
40          */
41         private $totalEntries = 0;
42
43         /**
44          * An instance of a file I/O pointer class (not handler)
45          */
46         private $pointerInstance = NULL;
47
48         /**
49          * Protected constructor
50          *
51          * @param       $className      Name of the class
52          * @return      void
53          */
54         protected function __construct (string $className) {
55                 // Call parent constructor
56                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-ABSTRACT-FILE: className=%s - CONSTRUCTED!', $className));
57                 parent::__construct($className);
58
59                 // Trace message
60                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: EXIT!');
61         }
62
63         /**
64          * Setter for FilePointer instance
65          *
66          * @param       $pointerInstance        An instance of an FilePointer class
67          * @return      void
68          */
69         protected final function setPointerInstance (FilePointer $pointerInstance = NULL) {
70                 $this->pointerInstance = $pointerInstance;
71         }
72
73         /**
74          * Getter for FilePointer instance
75          *
76          * @return      $pointerInstance        An instance of an FilePointer class
77          */
78         public final function getPointerInstance () {
79                 return $this->pointerInstance;
80         }
81
82         /**
83          * Unsets pointer instance which triggers a call of __destruct() if the
84          * instance is still there. This is surely not fatal on already "closed"
85          * file pointer instances.
86          *
87          * I don't want to mess around with above setter by giving it a default
88          * value NULL as setter should always explicitly only set (existing) object
89          * instances and NULL is NULL.
90          *
91          * @return      void
92          */
93         protected final function unsetPointerInstance () {
94                 // Simply invoke setter with no parameter
95                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: CALLED!');
96                 $this->setPointerInstance();
97
98                 // Trace message
99                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: EXIT!');
100         }
101
102         /**
103          * Destructor for cleaning purposes, etc
104          *
105          * @return      void
106          */
107         public final function __destruct() {
108                 // Try to close a file
109                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: DESTRUCTED!');
110                 $this->closeFile();
111
112                 // Call the parent destructor
113                 parent::__destruct();
114
115                 // Trace message
116                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: EXIT!');
117         }
118
119         /**
120          * "Getter" for abstracted file size
121          *
122          * @return      $fileSize       Size of abstracted file
123          */
124         public function getFileSize () {
125                 // Call pointer instanze
126                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: CALLED!');
127                 return $this->getPointerInstance()->getFileSize();
128         }
129
130         /**
131          * Getter for total entries
132          *
133          * @return      $totalEntries   Total entries in this file
134          */
135         public final function getCounter () {
136                 // Get it
137                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-ABSTRACT-FILE: Getting this->totalEntries=%d ... - CALLED!', $this->totalEntries));
138                 return $this->totalEntries;
139         }
140
141         /**
142          * Setter for total entries
143          *
144          * @param       $totalEntries   Total entries in this file
145          * @return      void
146          */
147         protected final function setCounter (int $counter) {
148                 // Set it
149                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-ABSTRACT-FILE: Setting this->totalEntries=%d ... - CALLED!', $counter));
150                 $this->totalEntries = $counter;
151         }
152
153         /**
154          * Increment counter
155          *
156          * @return      void
157          */
158         protected final function incrementCounter () {
159                 // Count up
160                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: CALLED!');
161                 $this->totalEntries++;
162
163                 // Trace message
164                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: EXIT!');
165         }
166
167         /**
168          * Getter for the file object
169          *
170          * @return      $fileObject             An instance of a SplFileObject
171          */
172         public final function getFileObject () {
173                 // Call pointer instanze
174                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: CALLED!');
175                 return $this->getPointerInstance()->getFileObject();
176         }
177
178         /**
179          * Getter for file's name
180          */
181         public final function getFilename () {
182                 // Invole file object's method
183                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: CALLED!');
184                 return $this->getFileObject()->getFilename();
185         }
186
187         /**
188          * Close a file source and set it's instance to null and the file name
189          * to empty
190          *
191          * @return      void
192          */
193         public function closeFile () {
194                 // Close down pointer instance as well by unsetting it
195                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: CALLED!');
196                 $this->unsetPointerInstance();
197
198                 // Trace message
199                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: EXIT!');
200         }
201
202         /**
203          * Size of this file
204          *
205          * @return      $size   Size (in bytes) of file
206          * @todo        Handle seekStatus
207          */
208         public function size () {
209                 // Call pointer instance
210                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: CALLED!');
211                 return $this->getPointerInstance()->size();
212         }
213
214         /**
215          * Read data a file pointer
216          *
217          * @return      mixed   The result of fread()
218          * @throws      NullPointerException    If the file pointer instance
219          *                                                                      is not set by setFileObject()
220          * @throws      InvalidResourceException        If there is being set
221          */
222         public function readFromFile () {
223                 // Call pointer instance
224                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: CALLED!');
225                 return $this->getPointerInstance()->readFromFile();
226         }
227
228         /**
229          * Write data to a file pointer
230          *
231          * @param       $dataStream             The data stream we shall write to the file
232          * @return      mixed                   Number of writes bytes or false on error
233          * @throws      NullPointerException    If the file pointer instance
234          *                                                                      is not set by setFileObject()
235          * @throws      InvalidResourceException        If there is being set
236          *                                                                                      an invalid file resource
237          */
238         public function writeToFile (string $dataStream) {
239                 // Check parameter
240                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('BASE-ABSTRACT-FILE: dataStream()=%d - CALLED!', strlen($dataStream)));
241                 if (empty($dataStream)) {
242                         // Throw IAE
243                         throw new InvalidArgumentException('Parameter "dataStream" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
244                 }
245
246                 // Call pointer instance
247                 return $this->getPointerInstance()->writeToFile($dataStream);
248         }
249
250         /**
251          * Determines whether the EOF has been reached
252          *
253          * @return      $isEndOfFileReached             Whether the EOF has been reached
254          */
255         public final function isEndOfFileReached () {
256                 // Call pointer instance
257                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('BASE-ABSTRACT-FILE: CALLED!');
258                 return $this->getPointerInstance()->isEndOfFileReached();
259         }
260
261 }