Just a space removed
[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.0
7  * @copyright   Copyright(c) 2007, 2008 Roland Haeder, this is free software
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 FrameworkDirectoryPointer extends BaseFrameworkSystem {
25         /**
26          * The current path we are working in
27          */
28         private $pathName = "";
29
30         /**
31          * The directory pointer
32          */
33         private $dirPointer = null;
34
35         /**
36          * Protected constructor
37          */
38         protected function __construct () {
39                 // Call parent constructor
40                 parent::__construct(__CLASS__);
41
42                 // Set part description
43                 $this->setObjectDescription("Helper for handling directories");
44
45                 // Create unique ID
46                 $this->generateUniqueId();
47
48                 // Clean-up a little
49                 $this->removeNumberFormaters();
50         }
51
52         /**
53          * Destructor for cleaning purposes, etc
54          */
55         public function __destruct() {
56                 // Is there a resource pointer? Then we have to close the directory here!
57                 if (is_resource($this->getPointer())) {
58                         // Try to close a directory
59                         $this->closeDirectory();
60                 }
61
62                 // Call the parent destructor
63                 parent::__destruct();
64         }
65
66         /**
67          * Create a directory pointer based on the given path. The path will also
68          * be verified here.
69          *
70          * @param               $pathName                                       The path name we shall pass
71          *                                                                      to opendir()
72          * @param               $inConstructor                          If we are in de/con-structor
73          *                                                                      or from somewhere else
74          * @throws      PathIsEmptyException            If the provided path name
75          *                                                                      is empty
76          * @throws      InvalidPathStringException      If the provided path name is
77          *                                                                      not a string
78          * @throws      PathIsNoDirectoryException      If the provided path name is
79          *                                                                      not valid
80          * @throws      PathReadProtectedException      If the provided path name is
81          *                                                                      read-protected
82          * @throws      DirPointerNotOpened                     If opendir() returns not a
83          *                                                                      directory resource
84          * @return      $pointerInstance                        A prepared instance of
85          *                                                                      FrameworkDirectoryPointer
86          */
87         public final static function createFrameworkDirectoryPointer ($pathName, $inConstructor = false) {
88                 // Some pre-sanity checks...
89                 if (is_null($pathName)) {
90                         // No pathname given
91                         if ($inConstructor) {
92                                 return null;
93                         } else {
94                                 throw new PathIsEmptyException(null, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
95                         }
96                 } elseif (!is_string($pathName)) {
97                         // Is not a string
98                         if ($inConstructor) {
99                                 return null;
100                         } else {
101                                 throw new InvalidPathStringException(null, self::EXCEPTION_INVALID_STRING);
102                         }
103                 } elseif (!is_dir($pathName)) {
104                         // Not a directory
105                         if ($inConstructor) {
106                                 return null;
107                         } else {
108                                 throw new PathIsNoDirectoryException($pathName, self::EXCEPTION_INVALID_PATH_NAME);
109                         }
110                 } elseif (!is_readable($pathName)) {
111                         // Not readable
112                         if ($inConstructor) {
113                                 return null;
114                         } else {
115                                 throw new PathReadProtectedException($pathName, self::EXCEPTION_READ_PROTECED_PATH);
116                         }
117                 }
118
119                 // Try to open a handler
120                 $dirPointer = @opendir($pathName);
121                 if (!is_resource($dirPointer)) {
122                         // Something bad happend
123                         if ($inConstructor) {
124                                 return null;
125                         } else {
126                                 throw new DirPointerNotOpenedException($pathName, self::EXCEPTION_DIR_POINTER_INVALID);
127                         }
128                 }
129
130                 // Create new instance
131                 $pointerInstance = new FrameworkDirectoryPointer();
132
133                 // Set directory pointer and path name
134                 $pointerInstance->setPointer($dirPointer);
135                 $pointerInstance->setPathName($pathName);
136
137                 // Return the instance
138                 return $pointerInstance;
139         }
140
141         /**
142          * Read raw lines of data from a directory pointer and return the data
143          *
144          * @return      string  Directory and/or file names read from the current
145          *                                      directory pointer
146          */
147         public function readRawDirectory () {
148                 // Read data from the directory pointer and return it
149                 return readdir($this->getPointer());
150         }
151
152         /**
153          * Read lines from the current directory pointer except some parts
154          *
155          * @param               $except Some parts of a directory we want to ignore.
156          *                                      Valid: dirs
157          *                                      Other values will be silently ignored
158          * @return      string  Directory and/or file names read from the current
159          *                                      directory pointer
160          */
161         public function readDirectoryExcept ($except = "") {
162                 if ((empty($except)) || (!is_array($except)) || (count($except) == 0)) {
163                         // No exception given, so read all data
164                         return $this->readRawDirectory();
165                 }
166
167                 // Read a raw line...
168                 $rawLine = $this->readRawDirectory();
169
170                 // Shall we exclude directories?
171                 if ((!is_null($rawLine)) && ($rawLine !== false) && (in_array($rawLine, $except))) {
172                         // Exclude this part
173                         return $this->readDirectoryExcept($except);
174                 } elseif ((!is_null($rawLine)) && ($rawLine !== false)) {
175                         // Return read data
176                         return $rawLine;
177                 }
178
179                 // End pointer reached
180                 return null;
181         }
182
183         /**
184          * Close a directory source and set it's instance to null and the path name
185          * to empty
186          *
187          * @return      void
188          */
189         public function closeDirectory () {
190                 // Close the directory pointer and reset the instance variable
191                 @closedir($this->getPointer());
192                 $this->setPointer(null);
193                 $this->setPathName("");
194         }
195
196         /**
197          * Setter for the directory pointer
198          *
199          * @param               $dirPointer     The directory resource
200          * @return      void
201          */
202         public final function setPointer ($dirPointer) {
203                 // Sanity-check if the pointer is a valid directory resource
204                 if (is_resource($dirPointer) || is_null($dirPointer)) {
205                         // Is a valid resource
206                         $this->dirPointer = $dirPointer;
207                 } else {
208                         // Throw exception
209                         throw new InvalidDirectoryResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
210                 }
211         }
212
213         /**
214          * Getter for the directory pointer
215          *
216          * @return      $dirPointer     The directory pointer which shall be a valid
217          *                                              directory resource
218          */
219         public final function getPointer () {
220                 return $this->dirPointer;
221         }
222
223         /**
224          * Setter for path name
225          *
226          * @param               $pathName               The new path name
227          * @return      void
228          */
229         public final function setPathName ($pathName) {
230                 $pathName = (string) $pathName;
231                 $this->pathName = $pathName;
232         }
233
234         /**
235          * Getter for path name
236          *
237          * @return      $pathName               The current path name
238          */
239         public final function getPathName () {
240                 return $this->pathName;
241         }
242 }
243
244 // [EOF]
245 ?>