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