3 * A class for directory reading and getting its contents
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright(c) 2007, 2008 Roland Haeder, this is free software
8 * @license GNU GPL 3.0 or any newer version
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 class FrameworkDirectoryPointer extends BaseFrameworkSystem {
25 * The current path we are working in
27 private $pathName = "";
30 * The directory pointer
32 private $dirPointer = null;
37 private function __construct () {
38 // Call parent constructor
39 parent::constructor(__CLASS__);
41 // Set part description
42 $this->setPartDescr("Verzeichnis-Handler");
45 $this->createUniqueID();
48 $this->removeNumberFormaters();
52 * Destructor for cleaning purposes, etc
54 public function __destruct() {
55 // Is there a resource pointer? Then we have to close the directory here!
56 if (is_resource($this->getPointer())) {
57 // Try to close a directory
58 $this->closeDirectory();
61 // Call the parent destructor
66 * Create a directory pointer based on the given path. The path will also
69 * @param $pathName The path name we shall pass
71 * @param $inConstructor If we are in de/con-structor
72 * or from somewhere else
73 * @throws PathIsEmptyException If the provided path name
75 * @throws InvalidPathStringException If the provided path name is
77 * @throws PathIsNoDirectoryException If the provided path name is
79 * @throws PathReadProtectedException If the provided path name is
81 * @throws DirPointerNotOpened If opendir() returns not a
83 * @return $pointerInstance A prepared instance of
84 * FrameworkDirectoryPointer
86 public final static function createFrameworkDirectoryPointer ($pathName, $inConstructor = false) {
87 // Some pre-sanity checks...
88 if (is_null($pathName)) {
93 throw new PathIsEmptyException(null, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
95 } elseif (!is_string($pathName)) {
100 throw new InvalidPathStringException(null, self::EXCEPTION_INVALID_STRING);
102 } elseif (!is_dir($pathName)) {
104 if ($inConstructor) {
107 throw new PathIsNoDirectoryException($pathName, self::EXCEPTION_INVALID_PATH_NAME);
109 } elseif (!is_readable($pathName)) {
111 if ($inConstructor) {
114 throw new PathReadProtectedException($pathName, self::EXCEPTION_READ_PROTECED_PATH);
118 // Try to open a handler
119 $dirPointer = @opendir($pathName);
120 if (!is_resource($dirPointer)) {
121 // Something bad happend
122 if ($inConstructor) {
125 throw new DirPointerNotOpenedException($pathName, self::EXCEPTION_DIR_POINTER_INVALID);
129 // Create new instance
130 $pointerInstance = new FrameworkDirectoryPointer();
132 // Set directory pointer and path name
133 $pointerInstance->setPointer($dirPointer);
134 $pointerInstance->setPathName($pathName);
136 // Return the instance
137 return $pointerInstance;
141 * Read raw lines of data from a directory pointer and return the data
143 * @return string Directory and/or file names read from the current
145 * @throws NullPointerException If the directory pointer instance
146 * is not set by setPointer()
147 * @throws InvalidDirectoryResourceException If there is being set
148 * an invalid directory resource
150 public function readRawDirectory () {
151 if (is_null($this->getPointer())) {
152 // Pointer not initialized
153 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
154 } elseif (!is_resource($this->getPointer())) {
155 // Pointer is not a valid resource!
156 throw new InvalidDirectoryResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
159 // Read data from the directory pointer and return it
160 return readdir($this->getPointer());
164 * Read lines from the current directory pointer except some parts
166 * @param $except Some parts of a directory we want to ignore.
168 * Other values will be silently ignored
169 * @return string Directory and/or file names read from the current
172 public function readDirectoryExcept ($except = "") {
173 if ((empty($except)) || (!is_array($except)) || (count($except) == 0)) {
174 // No exception given, so read all data
175 return $this->readRawDirectory();
178 // Read a raw line...
179 $rawLine = $this->readRawDirectory();
181 // Shall we exclude directories?
182 if ((!is_null($rawLine)) && ($rawLine !== false) && (in_array($rawLine, $except))) {
184 return $this->readDirectoryExcept($except);
185 } elseif ((!is_null($rawLine)) && ($rawLine !== false)) {
190 // End pointer reached
195 * Close a directory source and set it's instance to null and the path name
199 * @throws NullPointerException If the directory pointer instance
200 * is not set by setPointer()
201 * @throws InvalidDirectoryResourceException If there is being set
203 public function closeDirectory () {
204 if (is_null($this->getPointer())) {
205 // Pointer not initialized
207 } elseif (!is_resource($this->getPointer())) {
208 // Pointer is not a valid resource!
209 throw new InvalidDirectoryResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
212 // Close the directory pointer and reset the instance variable
213 @closedir($this->getPointer());
214 $this->setPointer(null);
215 $this->setPathName("");
219 * Setter for the directory pointer
221 * @param $dirPointer The directory resource
224 public final function setPointer ($dirPointer) {
225 // Sanity-check if the pointer is a valid directory resource
226 if (is_resource($dirPointer) || is_null($dirPointer)) {
227 // Is a valid resource
228 $this->dirPointer = $dirPointer;
231 throw new InvalidDirectoryResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
236 * Getter for the directory pointer
238 * @return $dirPointer The directory pointer which shall be a valid
241 public final function getPointer () {
242 return $this->dirPointer;
246 * Setter for path name
248 * @param $pathName The new path name
251 public final function setPathName ($pathName) {
252 $pathName = (string) $pathName;
253 $this->pathName = $pathName;
257 * Getter for path name
259 * @return $pathName The current path name
261 public final function getPathName () {
262 return $this->pathName;