7f827a8e0f5af1735fe53d1b33a93f2555dffefa
[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\Generic\UnsupportedOperationException;
9 use 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 - 2017 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 class BaseAbstractFile extends BaseFrameworkSystem implements FilePointer, CloseableFile {
34         /**
35          * Counter for total entries
36          */
37         private $totalEntries = 0;
38
39         /**
40          * The current file we are working in
41          */
42         private $fileName = '';
43
44         /**
45          * Protected constructor
46          *
47          * @param       $className      Name of the class
48          * @return      void
49          */
50         protected function __construct ($className) {
51                 // Call parent constructor
52                 parent::__construct($className);
53         }
54
55         /**
56          * Destructor for cleaning purposes, etc
57          *
58          * @return      void
59          */
60         public final function __destruct() {
61                 // Try to close a file
62                 $this->closeFile();
63
64                 // Call the parent destructor
65                 parent::__destruct();
66         }
67
68         /**
69          * "Getter" for abstracted file size
70          *
71          * @return      $fileSize       Size of abstracted file
72          */
73         public function getFileSize () {
74                 // Call pointer instanze
75                 return $this->getPointerInstance()->getFileSize();
76         }
77
78         /**
79          * Getter for total entries
80          *
81          * @return      $totalEntries   Total entries in this file
82          */
83         public final function getCounter () {
84                 // Get it
85                 return $this->totalEntries;
86         }
87
88         /**
89          * Setter for total entries
90          *
91          * @param       $totalEntries   Total entries in this file
92          * @return      void
93          */
94         protected final function setCounter ($counter) {
95                 // Set it
96                 $this->totalEntries = $counter;
97         }
98
99         /**
100          * Increment counter
101          *
102          * @return      void
103          */
104         protected final function incrementCounter () {
105                 // Get it
106                 $this->totalEntries++;
107         }
108
109         /**
110          * Getter for the file pointer
111          *
112          * @return      $filePointer    The file pointer which shall be a valid file resource
113          * @throws      UnsupportedOperationException   If this method is called
114          */
115         public final function getPointer () {
116                 throw new UnsupportedOperationException(array($this, __FUNCTION__), self::EXCEPTION_UNSPPORTED_OPERATION);
117         }
118
119         /**
120          * Setter for file name
121          *
122          * @param       $fileName       The new file name
123          * @return      void
124          */
125         protected final function setFileName ($fileName) {
126                 $fileName = (string) $fileName;
127                 $this->fileName = $fileName;
128         }
129
130         /**
131          * Getter for file name
132          *
133          * @return      $fileName       The current file name
134          */
135         public final function getFileName () {
136                 return $this->fileName;
137         }
138
139         /**
140          * Close a file source and set it's instance to null and the file name
141          * to empty
142          *
143          * @return      void
144          */
145         public function closeFile () {
146                 // Debug message
147                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: fileName=%s - CALLED!', __METHOD__, __LINE__, $this->getFileName()));
148
149                 // Close down pointer instance as well by unsetting it
150                 $this->unsetPointerInstance();
151
152                 // Remove file name
153                 $this->setFileName('');
154
155                 // Debug message
156                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('[%s:%d]: EXIT!', __METHOD__, __LINE__));
157         }
158
159         /**
160          * Size of this file
161          *
162          * @return      $size   Size (in bytes) of file
163          * @todo        Handle seekStatus
164          */
165         public function size () {
166                 // Call pointer instance
167                 return $this->getPointerInstance()->size();
168         }
169
170         /**
171          * Read data a file pointer
172          *
173          * @return      mixed   The result of fread()
174          * @throws      NullPointerException    If the file pointer instance
175          *                                                                      is not set by setPointer()
176          * @throws      InvalidResourceException        If there is being set
177          */
178         public function readFromFile () {
179                 // Call pointer instance
180                 return $this->getPointerInstance()->readFromFile();
181         }
182
183         /**
184          * Write data to a file pointer
185          *
186          * @param       $dataStream             The data stream we shall write to the file
187          * @return      mixed                   Number of writes bytes or false on error
188          * @throws      NullPointerException    If the file pointer instance
189          *                                                                      is not set by setPointer()
190          * @throws      InvalidResourceException        If there is being set
191          *                                                                                      an invalid file resource
192          */
193         public function writeToFile ($dataStream) {
194                 // Call pointer instance
195                 return $this->getPointerInstance()->writeToFile($dataStream);
196         }
197
198         /**
199          * Determines whether the EOF has been reached
200          *
201          * @return      $isEndOfFileReached             Whether the EOF has been reached
202          */
203         public final function isEndOfFileReached () {
204                 // Call pointer instance
205                 return $this->getPointerInstance()->isEndOfFileReached();
206         }
207
208 }