Renamed classes/main/ to main/classes/ + added FuseFeature, an upcoming feature
[core.git] / inc / main / classes / file_directories / directory / class_FrameworkDirectoryPointer.php
1 <?php
2 /**
3  * A class for directory reading and getting its contents, no recursion!
4  *
5  * @author              Roland Haeder <webmaster@shipsimu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.shipsimu.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 implements FrameworkDirectory {
25         /**
26          * The current path we are working in
27          */
28         private $pathName = '';
29
30         /**
31          * The directory iterator instance
32          */
33         private $iteratorInstance = 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 ($this->getDirectoryIteratorInstance() instanceof DirectoryIterator) {
49                         // Try to close a directory
50                         $this->closeDirectory();
51                 } // END - if
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 else
63          * @return      $pointerInstance        A prepared instance of FrameworkDirectoryPointer
64          * @throws      PathIsEmptyException    If the provided path name is empty
65          * @throws      InvalidPathStringException      If the provided path name is not a string
66          * @throws      PathIsNoDirectoryException      If the provided path name is not valid
67          * @throws      PathReadProtectedException      If the provided path name is read-protected
68          * @todo        Get rid of inConstructor, could be old-lost code.
69          */
70         public static final function createFrameworkDirectoryPointer ($pathName, $inConstructor = FALSE) {
71                 // Some pre-sanity checks...
72                 if (is_null($pathName)) {
73                         // No pathname given
74                         if ($inConstructor) {
75                                 return NULL;
76                         } else {
77                                 throw new PathIsEmptyException(NULL, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
78                         }
79                 } elseif (!is_string($pathName)) {
80                         // Is not a string
81                         if ($inConstructor) {
82                                 return NULL;
83                         } else {
84                                 throw new InvalidPathStringException(NULL, self::EXCEPTION_INVALID_STRING);
85                         }
86                 } elseif (!is_dir($pathName)) {
87                         // Not a directory
88                         if ($inConstructor) {
89                                 return NULL;
90                         } else {
91                                 throw new PathIsNoDirectoryException($pathName, self::EXCEPTION_INVALID_PATH_NAME);
92                         }
93                 } elseif (!is_readable($pathName)) {
94                         // Not readable
95                         if ($inConstructor) {
96                                 return NULL;
97                         } else {
98                                 throw new PathReadProtectedException($pathName, self::EXCEPTION_READ_PROTECED_PATH);
99                         }
100                 }
101
102                 // Create new instance
103                 $pointerInstance = new FrameworkDirectoryPointer();
104
105                 // Get an iterator for the directory
106                 $iteratorInstance = new DirectoryIterator($pathName);
107
108                 // ... and rewind back
109                 $iteratorInstance->rewind();
110
111                 // Set directory pointer and path name
112                 $pointerInstance->setDirectoryIteratorInstance($iteratorInstance);
113                 $pointerInstance->setPathName($pathName);
114
115                 // Return the instance
116                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: Opened pathName=' . $pathName . ' - EXIT!');
117                 return $pointerInstance;
118         }
119
120         /**
121          * Read raw lines of data from a directory pointer and return the data
122          *
123          * @return      $currentEntry   Current entry from encapsulated iterator
124          */
125         public function readRawDirectory () {
126                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . '] - CALLED!');
127
128                 // Can the next entry be read?
129                 assert($this->getDirectoryIteratorInstance()->valid());
130
131                 // Read data from the directory pointer and return it
132                 $currentEntry = $this->getDirectoryIteratorInstance()->current();
133
134                 // Return found entry
135                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: currentEntry[]=' . gettype($currentEntry) . ' - EXIT!');
136                 return $currentEntry;
137         }
138
139         /**
140          * Read lines from the current directory pointer except some parts
141          *
142          * @param       $except         Some parts of a directory we want to ignore. Valid: directory and file names, other values will be silently ignored
143          * @return      string          Directory and/or file names read from the current directory pointer
144          */
145         public function readDirectoryExcept (array $except = array()) {
146                 // No exceptions given?
147                 if (count($except) == 0) {
148                         // No exception given, so read all files and directories, but not recursive
149                         self::createDebugInstance(__CLASS__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: No exceptions given, please use readRawDirectory() instead!');
150                         return $this->readRawDirectory();
151                 } elseif (!$this->getDirectoryIteratorInstance()->valid()) {
152                         // No more left to read
153                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: EOD reached.');
154                         return NULL;
155                 }
156
157                 // Init raw line
158                 $rawLine = NULL;
159
160                 // Read a raw line...
161                 $currentEntry = $this->readRawDirectory();
162                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: currentEntry[]=' . gettype($currentEntry));
163
164                 // Shall we exclude directories?
165                 if (is_object($currentEntry)) {
166                         // Get file name
167                         $rawLine = $currentEntry->getFilename();
168                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: rawLine[' . gettype($rawLine) . ']=' . $rawLine . ',isDot=' . intval($this->getDirectoryIteratorInstance()->isDot()));
169
170                         // Is it a dot-directory or excluded?
171                         if (($this->getDirectoryIteratorInstance()->isDot()) || (in_array($rawLine, $except))) {
172                                 // To next entry
173                                 $this->getDirectoryIteratorInstance()->next();
174
175                                 // Exclude this part
176                                 $rawLine = $this->readDirectoryExcept($except);
177                                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: rawline[' . gettype($rawLine) . ']=' . $rawLine . ' - Recursive call!');
178                         } // END - if
179                 } // END - if
180
181                 // To next entry
182                 $this->getDirectoryIteratorInstance()->next();
183
184                 // Return read line
185                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: rawLine[' . gettype($rawLine) . ']=' . $rawLine);
186                 return $rawLine;
187         }
188
189         /**
190          * Close a directory source and set it's instance to null and the path name
191          * to empty
192          *
193          * @return      void
194          */
195         public function closeDirectory () {
196                 // Close the directory by unsetting it
197                 $this->unsetDirectoryIteratorInstance();
198                 $this->setPathName('');
199         }
200
201         /**
202          * Setter for the directory pointer
203          *
204          * @param       $iteratorInstance       An instanceof a DirectoryIterator class
205          * @return      void
206          */
207         protected final function setDirectoryIteratorInstance (DirectoryIterator $iteratorInstance) {
208                 // Set instance
209                 $this->iteratorInstance = $iteratorInstance;
210         }
211
212         /**
213          * Getter for the directory pointer
214          *
215          * @return      $iteratorInstance       The directory pointer which shall be a valid directory resource
216          */
217         public final function getDirectoryIteratorInstance () {
218                 return $this->iteratorInstance;
219         }
220
221         /**
222          * Remove directory iterator instance (effectively closing it) by setting
223          * it to NULL. This will trigger a call on the destructor which will then
224          * "close" the iterator.
225          *
226          * @param       $iteratorInstance       An instanceof a DirectoryIterator class
227          * @return      void
228          */
229         protected final function unsetDirectoryIteratorInstance () {
230                 // "Unset" the instance
231                 $this->iteratorInstance = NULL;
232         }
233
234         /**
235          * Setter for path name
236          *
237          * @param       $pathName       The new path name
238          * @return      void
239          */
240         protected final function setPathName ($pathName) {
241                 $pathName = (string) $pathName;
242                 $this->pathName = $pathName;
243         }
244
245         /**
246          * Getter for path name
247          *
248          * @return      $pathName       The current path name
249          */
250         public final function getPathName () {
251                 return $this->pathName;
252         }
253 }
254
255 // [EOF]
256 ?>