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