f48b884b2112a7e94d155cfc273d56b3e1fadb2a
[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 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          * @throws      DirPointerNotOpenedException    If opendir() returns not a directory resource
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                 // Try to open a handler
103                 $dirPointer = @opendir($pathName);
104                 if (!is_resource($dirPointer)) {
105                         // Something bad happend
106                         if ($inConstructor) {
107                                 return NULL;
108                         } else {
109                                 throw new DirPointerNotOpenedException($pathName, self::EXCEPTION_DIR_POINTER_INVALID);
110                         }
111                 }
112
113                 // Create new instance
114                 $pointerInstance = new FrameworkDirectoryPointer();
115
116                 // Set directory pointer and path name
117                 $pointerInstance->setPointer($dirPointer);
118                 $pointerInstance->setPathName($pathName);
119
120                 // Return the instance
121                 return $pointerInstance;
122         }
123
124         /**
125          * Read raw lines of data from a directory pointer and return the data
126          *
127          * @return      string  Directory and/or file names read from the current directory pointer
128          */
129         public function readRawDirectory () {
130                 // Read data from the directory pointer and return it
131                 return readdir($this->getPointer());
132         }
133
134         /**
135          * Read lines from the current directory pointer except some parts
136          *
137          * @param       $except         Some parts of a directory we want to ignore. Valid: directory and file names, other values will be silently ignored
138          * @return      string          Directory and/or file names read from the current directory pointer
139          */
140         public function readDirectoryExcept (array $except = array('.', '..')) {
141                 if (count($except) == 0) {
142                         // No exception given, so read all data
143                         self::createDebugInstance(__CLASS__)->debugOutput('DIRECTORY: No exceptions given, please use readRawDirectory() instead!');
144                         return $this->readRawDirectory();
145                 } // END - if
146
147                 // Read a raw line...
148                 $rawLine = $this->readRawDirectory();
149
150                 // Shall we exclude directories?
151                 if ((!is_null($rawLine)) && ($rawLine !== false) && (!in_array($rawLine, $except))) {
152                         // Return read data
153                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DIRECTORY: rawLine[' . gettype($rawLine) . ']=' . $rawLine);
154                         return $rawLine;
155                 } elseif ((!is_null($rawLine)) && ($rawLine !== false)) {
156                         // Exclude this part
157                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DIRECTORY: rawline[' . gettype($rawLine) . ']=' . $rawLine . ' - Recursive call!');
158                         return $this->readDirectoryExcept($except);
159                 }
160
161                 // End pointer reached
162                 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('DIRECTORY: Returning NULL!');
163                 return NULL;
164         }
165
166         /**
167          * Close a directory source and set it's instance to null and the path name
168          * to empty
169          *
170          * @return      void
171          */
172         public function closeDirectory () {
173                 // Close the directory pointer and reset the instance variable
174                 @closedir($this->getPointer());
175                 $this->setPointer(NULL);
176                 $this->setPathName('');
177         }
178
179         /**
180          * Setter for the directory pointer
181          *
182          * @param       $dirPointer             The directory resource
183          * @return      void
184          */
185         public final function setPointer ($dirPointer) {
186                 // Sanity-check if pointer is a valid directory resource
187                 if (is_resource($dirPointer) || is_null($dirPointer)) {
188                         // Is a valid resource
189                         $this->dirPointer = $dirPointer;
190                 } else {
191                         // Throw exception
192                         throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE);
193                 }
194         }
195
196         /**
197          * Getter for the directory pointer
198          *
199          * @return      $dirPointer             The directory pointer which shall be a valid directory resource
200          */
201         public final function getPointer () {
202                 return $this->dirPointer;
203         }
204
205         /**
206          * Setter for path name
207          *
208          * @param       $pathName       The new path name
209          * @return      void
210          */
211         public final function setPathName ($pathName) {
212                 $pathName = (string) $pathName;
213                 $this->pathName = $pathName;
214         }
215
216         /**
217          * Getter for path name
218          *
219          * @return      $pathName       The current path name
220          */
221         public final function getPathName () {
222                 return $this->pathName;
223         }
224 }
225
226 // [EOF]
227 ?>