(no commit message)
[shipsimu.git] / inc / classes / main / io / class_FrameworkDirectoryPointer.php
1 <?php
2 /**
3  * A class for directory reading and getting its contents
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0
7  * @copyright   Copyright(c) 2007, 2008 Roland Haeder, this is free software
8  * @license             GNU GPL 3.0 or any newer version
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  */
23 class FrameworkDirectoryPointer extends BaseFrameworkSystem {
24         /**
25          * The current path we are working in
26          */
27         private $pathName = "";
28
29         /**
30          * The directory pointer
31          */
32         private $dirPointer = null;
33
34         /**
35          * Private constructor
36          */
37         private function __construct () {
38                 // Call parent constructor
39                 parent::constructor(__CLASS__);
40
41                 // Set part description
42                 $this->setPartDescr("Verzeichnis-Handler");
43
44                 // Create unique ID
45                 $this->createUniqueID();
46
47                 // Clean-up a little
48                 $this->removeNumberFormaters();
49         }
50
51         /**
52          * Destructor for cleaning purposes, etc
53          */
54         public function __destruct() {
55                 // Is there a resource pointer? Then we have to close the directory here!
56                 if (is_resource($this->getPointer())) {
57                         // Try to close a directory
58                         $this->closeDirectory();
59                 }
60
61                 // Call the parent destructor
62                 parent::__destruct();
63         }
64
65         /**
66          * Create a directory pointer based on the given path. The path will also
67          * be verified here.
68          *
69          * @param               $pathName                                       The path name we shall pass
70          *                                                                      to opendir()
71          * @param               $inConstructor                          If we are in de/con-structor
72          *                                                                      or from somewhere else
73          * @throws      PathIsEmptyException            If the provided path name
74          *                                                                      is empty
75          * @throws      InvalidPathStringException      If the provided path name is
76          *                                                                      not a string
77          * @throws      PathIsNoDirectoryException      If the provided path name is
78          *                                                                      not valid
79          * @throws      PathReadProtectedException      If the provided path name is
80          *                                                                      read-protected
81          * @throws      DirPointerNotOpened                     If opendir() returns not a
82          *                                                                      directory resource
83          * @return      $pointerInstance                        A prepared instance of
84          *                                                                      FrameworkDirectoryPointer
85          */
86         public final static function createFrameworkDirectoryPointer ($pathName, $inConstructor = false) {
87                 // Some pre-sanity checks...
88                 if (is_null($pathName)) {
89                         // No pathname given
90                         if ($inConstructor) {
91                                 return null;
92                         } else {
93                                 throw new PathIsEmptyException(null, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
94                         }
95                 } elseif (!is_string($pathName)) {
96                         // Is not a string
97                         if ($inConstructor) {
98                                 return null;
99                         } else {
100                                 throw new InvalidPathStringException(null, self::EXCEPTION_INVALID_STRING);
101                         }
102                 } elseif (!is_dir($pathName)) {
103                         // Not a directory
104                         if ($inConstructor) {
105                                 return null;
106                         } else {
107                                 throw new PathIsNoDirectoryException($pathName, self::EXCEPTION_INVALID_PATH_NAME);
108                         }
109                 } elseif (!is_readable($pathName)) {
110                         // Not readable
111                         if ($inConstructor) {
112                                 return null;
113                         } else {
114                                 throw new PathReadProtectedException($pathName, self::EXCEPTION_READ_PROTECED_PATH);
115                         }
116                 }
117
118                 // Try to open a handler
119                 $dirPointer = @opendir($pathName);
120                 if (!is_resource($dirPointer)) {
121                         // Something bad happend
122                         if ($inConstructor) {
123                                 return null;
124                         } else {
125                                 throw new DirPointerNotOpenedException($pathName, self::EXCEPTION_DIR_POINTER_INVALID);
126                         }
127                 }
128
129                 // Create new instance
130                 $pointerInstance = new FrameworkDirectoryPointer();
131
132                 // Set directory pointer and path name
133                 $pointerInstance->setPointer($dirPointer);
134                 $pointerInstance->setPathName($pathName);
135
136                 // Return the instance
137                 return $pointerInstance;
138         }
139
140         /**
141          * Read raw lines of data from a directory pointer and return the data
142          *
143          * @return      string  Directory and/or file names read from the current
144          *                                      directory pointer
145          * @throws      NullPointerException    If the directory pointer instance
146          *                                                              is not set by setPointer()
147          * @throws      InvalidDirectoryResourceException       If there is being set
148          *                                                                              an invalid directory resource
149          */
150         public function readRawDirectory () {
151                 if (is_null($this->getPointer())) {
152                         // Pointer not initialized
153                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
154                 } elseif (!is_resource($this->getPointer())) {
155                         // Pointer is not a valid resource!
156                         throw new InvalidDirectoryResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
157                 }
158
159                 // Read data from the directory pointer and return it
160                 return readdir($this->getPointer());
161         }
162
163         /**
164          * Read lines from the current directory pointer except some parts
165          *
166          * @param               $except Some parts of a directory we want to ignore.
167          *                                      Valid: dirs
168          *                                      Other values will be silently ignored
169          * @return      string  Directory and/or file names read from the current
170          *                                      directory pointer
171          */
172         public function readDirectoryExcept ($except = "") {
173                 if ((empty($except)) || (!is_array($except)) || (count($except) == 0)) {
174                         // No exception given, so read all data
175                         return $this->readRawDirectory();
176                 }
177
178                 // Read a raw line...
179                 $rawLine = $this->readRawDirectory();
180
181                 // Shall we exclude directories?
182                 if ((!is_null($rawLine)) && ($rawLine !== false) && (in_array($rawLine, $except))) {
183                         // Exclude this part
184                         return $this->readDirectoryExcept($except);
185                 } elseif ((!is_null($rawLine)) && ($rawLine !== false)) {
186                         // Return read data
187                         return $rawLine;
188                 }
189
190                 // End pointer reached
191                 return null;
192         }
193
194         /**
195          * Close a directory source and set it's instance to null and the path name
196          * to empty
197          *
198          * @return      void
199          * @throws      NullPointerException    If the directory pointer instance
200          *                                                              is not set by setPointer()
201          * @throws      InvalidDirectoryResourceException       If there is being set
202          */
203         public function closeDirectory () {
204                 if (is_null($this->getPointer())) {
205                         // Pointer not initialized
206                         return;
207                 } elseif (!is_resource($this->getPointer())) {
208                         // Pointer is not a valid resource!
209                         throw new InvalidDirectoryResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
210                 }
211
212                 // Close the directory pointer and reset the instance variable
213                 @closedir($this->getPointer());
214                 $this->setPointer(null);
215                 $this->setPathName("");
216         }
217
218         /**
219          * Setter for the directory pointer
220          *
221          * @param               $dirPointer     The directory resource
222          * @return      void
223          */
224         public final function setPointer ($dirPointer) {
225                 // Sanity-check if the pointer is a valid directory resource
226                 if (is_resource($dirPointer) || is_null($dirPointer)) {
227                         // Is a valid resource
228                         $this->dirPointer = $dirPointer;
229                 } else {
230                         // Throw exception
231                         throw new InvalidDirectoryResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
232                 }
233         }
234
235         /**
236          * Getter for the directory pointer
237          *
238          * @return      $dirPointer     The directory pointer which shall be a valid
239          *                                              directory resource
240          */
241         public final function getPointer () {
242                 return $this->dirPointer;
243         }
244
245         /**
246          * Setter for path name
247          *
248          * @param               $pathName               The new path name
249          * @return      void
250          */
251         public final function setPathName ($pathName) {
252                 $pathName = (string) $pathName;
253                 $this->pathName = $pathName;
254         }
255
256         /**
257          * Getter for path name
258          *
259          * @return      $pathName               The current path name
260          */
261         public final function getPathName () {
262                 return $this->pathName;
263         }
264 }
265
266 // [EOF]
267 ?>