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