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