b867067b0b821b0abd0bf586838a41e563b58935
[core.git] / framework / main / classes / file_directories / class_BaseAbstractFile.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\Filesystem\File;
4
5 // Import framework stuff
6 use CoreFramework\Filesystem\CloseableFile;
7 use CoreFramework\Filesystem\FilePointer;
8 use CoreFramework\Object\BaseFrameworkSystem;
9
10 /**
11  * An abstract file class
12  *
13  * @author              Roland Haeder <webmaster@ship-simu.org>
14  * @version             0.0.0
15  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16  * @license             GNU GPL 3.0 or any newer version
17  * @link                http://www.ship-simu.org
18  *
19  * This program is free software: you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation, either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31  */
32 class BaseAbstractFile extends BaseFrameworkSystem implements FilePointer, CloseableFile {
33         /**
34          * Counter for total entries
35          */
36         private $totalEntries = 0;
37
38         /**
39          * The current file we are working in
40          */
41         private $fileName = '';
42
43         /**
44          * Protected constructor
45          *
46          * @param       $className      Name of the class
47          * @return      void
48          */
49         protected function __construct ($className) {
50                 // Call parent constructor
51                 parent::__construct($className);
52         }
53
54         /**
55          * Destructor for cleaning purposes, etc
56          *
57          * @return      void
58          */
59         public final function __destruct() {
60                 // Try to close a file
61                 $this->closeFile();
62
63                 // Call the parent destructor
64                 parent::__destruct();
65         }
66
67         /**
68          * "Getter" for abstracted file size
69          *
70          * @return      $fileSize       Size of abstracted file
71          */
72         public function getFileSize () {
73                 // Call pointer instanze
74                 return $this->getPointerInstance()->getFileSize();
75         }
76
77         /**
78          * Getter for total entries
79          *
80          * @return      $totalEntries   Total entries in this file
81          */
82         public final function getCounter () {
83                 // Get it
84                 return $this->totalEntries;
85         }
86
87         /**
88          * Setter for total entries
89          *
90          * @param       $totalEntries   Total entries in this file
91          * @return      void
92          */
93         protected final function setCounter ($counter) {
94                 // Set it
95                 $this->totalEntries = $counter;
96         }
97
98         /**
99          * Increment counter
100          *
101          * @return      void
102          */
103         protected final function incrementCounter () {
104                 // Get it
105                 $this->totalEntries++;
106         }
107
108         /**
109          * Getter for the file pointer
110          *
111          * @return      $filePointer    The file pointer which shall be a valid file resource
112          * @throws      UnsupportedOperationException   If this method is called
113          */
114         public final function getPointer () {
115                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
116         }
117
118         /**
119          * Setter for file name
120          *
121          * @param       $fileName       The new file name
122          * @return      void
123          */
124         protected final function setFileName ($fileName) {
125                 $fileName = (string) $fileName;
126                 $this->fileName = $fileName;
127         }
128
129         /**
130          * Getter for file name
131          *
132          * @return      $fileName       The current file name
133          */
134         public final function getFileName () {
135                 return $this->fileName;
136         }
137
138         /**
139          * Close a file source and set it's instance to null and the file name
140          * to empty
141          *
142          * @return      void
143          */
144         public function closeFile () {
145                 // Debug message
146                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: fileName=%s - CALLED!', __METHOD__, __LINE__, $this->getFileName()));
147
148                 // Close down pointer instance as well by unsetting it
149                 $this->unsetPointerInstance();
150
151                 // Remove file name
152                 $this->setFileName('');
153
154                 // Debug message
155                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: EXIT!', __METHOD__, __LINE__));
156         }
157
158         /**
159          * Size of this file
160          *
161          * @return      $size   Size (in bytes) of file
162          * @todo        Handle seekStatus
163          */
164         public function size () {
165                 // Call pointer instance
166                 return $this->getPointerInstance()->size();
167         }
168
169         /**
170          * Read data a file pointer
171          *
172          * @return      mixed   The result of fread()
173          * @throws      NullPointerException    If the file pointer instance
174          *                                                                      is not set by setPointer()
175          * @throws      InvalidResourceException        If there is being set
176          */
177         public function readFromFile () {
178                 // Call pointer instance
179                 return $this->getPointerInstance()->readFromFile();
180         }
181
182         /**
183          * Write data to a file pointer
184          *
185          * @param       $dataStream             The data stream we shall write to the file
186          * @return      mixed                   Number of writes bytes or false on error
187          * @throws      NullPointerException    If the file pointer instance
188          *                                                                      is not set by setPointer()
189          * @throws      InvalidResourceException        If there is being set
190          *                                                                                      an invalid file resource
191          */
192         public function writeToFile ($dataStream) {
193                 // Call pointer instance
194                 return $this->getPointerInstance()->writeToFile($dataStream);
195         }
196
197         /**
198          * Determines whether the EOF has been reached
199          *
200          * @return      $isEndOfFileReached             Whether the EOF has been reached
201          */
202         public final function isEndOfFileReached () {
203                 // Call pointer instance
204                 return $this->getPointerInstance()->isEndOfFileReached();
205         }
206
207 }