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