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, 2009 - 2011 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
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.
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.
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/>.
24 class FrameworkDirectoryPointer extends BaseFrameworkSystem {
26 * The current path we are working in
28 private $pathName = '';
31 * The directory pointer
33 private $dirPointer = NULL;
36 * Protected constructor
38 protected function __construct () {
39 // Call parent constructor
40 parent::__construct(__CLASS__);
44 * Destructor for cleaning purposes, etc
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();
53 // Call the parent destructor
58 * Create a directory pointer based on the given path. The path will also
61 * @param $pathName The path name we shall pass to opendir()
62 * @param $inConstructor If we are in de/con-structor or from somewhere
64 * @return $pointerInstance A prepared instance of
65 * FrameworkDirectoryPointer
66 * @throws PathIsEmptyException If the provided path name
68 * @throws InvalidPathStringException If the provided path name is
70 * @throws PathIsNoDirectoryException If the provided path name is
72 * @throws PathReadProtectedException If the provided path name is
74 * @throws DirPointerNotOpened If opendir() returns not a
77 public static final function createFrameworkDirectoryPointer ($pathName, $inConstructor = false) {
78 // Some pre-sanity checks...
79 if (is_null($pathName)) {
84 throw new PathIsEmptyException(null, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
86 } elseif (!is_string($pathName)) {
91 throw new InvalidPathStringException(null, self::EXCEPTION_INVALID_STRING);
93 } elseif (!is_dir($pathName)) {
98 throw new PathIsNoDirectoryException($pathName, self::EXCEPTION_INVALID_PATH_NAME);
100 } elseif (!is_readable($pathName)) {
102 if ($inConstructor) {
105 throw new PathReadProtectedException($pathName, self::EXCEPTION_READ_PROTECED_PATH);
109 // Try to open a handler
110 $dirPointer = @opendir($pathName);
111 if (!is_resource($dirPointer)) {
112 // Something bad happend
113 if ($inConstructor) {
116 throw new DirPointerNotOpenedException($pathName, self::EXCEPTION_DIR_POINTER_INVALID);
120 // Create new instance
121 $pointerInstance = new FrameworkDirectoryPointer();
123 // Set directory pointer and path name
124 $pointerInstance->setPointer($dirPointer);
125 $pointerInstance->setPathName($pathName);
127 // Return the instance
128 return $pointerInstance;
132 * Read raw lines of data from a directory pointer and return the data
134 * @return string Directory and/or file names read from the current
137 public function readRawDirectory () {
138 // Read data from the directory pointer and return it
139 return readdir($this->getPointer());
143 * Read lines from the current directory pointer except some parts
145 * @param $except Some parts of a directory we want to ignore.
147 * Other values will be silently ignored
148 * @return string Directory and/or file names read from the current
151 public function readDirectoryExcept ($except = '') {
152 if ((empty($except)) || (!is_array($except)) || (count($except) == 0)) {
153 // No exception given, so read all data
154 return $this->readRawDirectory();
157 // Read a raw line...
158 $rawLine = $this->readRawDirectory();
160 // Shall we exclude directories?
161 if ((!is_null($rawLine)) && ($rawLine !== false) && (in_array($rawLine, $except))) {
163 return $this->readDirectoryExcept($except);
164 } elseif ((!is_null($rawLine)) && ($rawLine !== false)) {
169 // End pointer reached
174 * Close a directory source and set it's instance to null and the path name
179 public function closeDirectory () {
180 // Close the directory pointer and reset the instance variable
181 @closedir($this->getPointer());
182 $this->setPointer(NULL);
183 $this->setPathName('');
187 * Setter for the directory pointer
189 * @param $dirPointer The directory resource
192 public final function setPointer ($dirPointer) {
193 // Sanity-check if pointer is a valid directory resource
194 if (is_resource($dirPointer) || is_null($dirPointer)) {
195 // Is a valid resource
196 $this->dirPointer = $dirPointer;
199 throw new InvalidResourceException($this, self::EXCEPTION_INVALID_RESOURCE);
204 * Getter for the directory pointer
206 * @return $dirPointer The directory pointer which shall be a valid
209 public final function getPointer () {
210 return $this->dirPointer;
214 * Setter for path name
216 * @param $pathName The new path name
219 public final function setPathName ($pathName) {
220 $pathName = (string) $pathName;
221 $this->pathName = $pathName;
225 * Getter for path name
227 * @return $pathName The current path name
229 public final function getPathName () {
230 return $this->pathName;