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 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__);
43 $this->removeNumberFormaters();
44 $this->removeSystemArray();
48 * Destructor for cleaning purposes, etc
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();
57 // Call the parent destructor
62 * Create a directory pointer based on the given path. The path will also
65 * @param $pathName The path name we shall pass to opendir()
66 * @param $inConstructor If we are in de/con-structor or from somewhere
68 * @return $pointerInstance A prepared instance of
69 * FrameworkDirectoryPointer
70 * @throws PathIsEmptyException If the provided path name
72 * @throws InvalidPathStringException If the provided path name is
74 * @throws PathIsNoDirectoryException If the provided path name is
76 * @throws PathReadProtectedException If the provided path name is
78 * @throws DirPointerNotOpened If opendir() returns not a
81 public final static function createFrameworkDirectoryPointer ($pathName, $inConstructor = false) {
82 // Some pre-sanity checks...
83 if (is_null($pathName)) {
88 throw new PathIsEmptyException(null, self::EXCEPTION_UNEXPECTED_EMPTY_STRING);
90 } elseif (!is_string($pathName)) {
95 throw new InvalidPathStringException(null, self::EXCEPTION_INVALID_STRING);
97 } elseif (!is_dir($pathName)) {
102 throw new PathIsNoDirectoryException($pathName, self::EXCEPTION_INVALID_PATH_NAME);
104 } elseif (!is_readable($pathName)) {
106 if ($inConstructor) {
109 throw new PathReadProtectedException($pathName, self::EXCEPTION_READ_PROTECED_PATH);
113 // Try to open a handler
114 $dirPointer = @opendir($pathName);
115 if (!is_resource($dirPointer)) {
116 // Something bad happend
117 if ($inConstructor) {
120 throw new DirPointerNotOpenedException($pathName, self::EXCEPTION_DIR_POINTER_INVALID);
124 // Create new instance
125 $pointerInstance = new FrameworkDirectoryPointer();
127 // Set directory pointer and path name
128 $pointerInstance->setPointer($dirPointer);
129 $pointerInstance->setPathName($pathName);
131 // Return the instance
132 return $pointerInstance;
136 * Read raw lines of data from a directory pointer and return the data
138 * @return string Directory and/or file names read from the current
141 public function readRawDirectory () {
142 // Read data from the directory pointer and return it
143 return readdir($this->getPointer());
147 * Read lines from the current directory pointer except some parts
149 * @param $except Some parts of a directory we want to ignore.
151 * Other values will be silently ignored
152 * @return string Directory and/or file names read from the current
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();
161 // Read a raw line...
162 $rawLine = $this->readRawDirectory();
164 // Shall we exclude directories?
165 if ((!is_null($rawLine)) && ($rawLine !== false) && (in_array($rawLine, $except))) {
167 return $this->readDirectoryExcept($except);
168 } elseif ((!is_null($rawLine)) && ($rawLine !== false)) {
173 // End pointer reached
178 * Close a directory source and set it's instance to null and the path name
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('');
191 * Setter for the directory pointer
193 * @param $dirPointer The directory resource
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;
203 throw new InvalidDirectoryResourceException($this, self::EXCEPTION_INVALID_DIRECTORY_POINTER);
208 * Getter for the directory pointer
210 * @return $dirPointer The directory pointer which shall be a valid
213 public final function getPointer () {
214 return $this->dirPointer;
218 * Setter for path name
220 * @param $pathName The new path name
223 public final function setPathName ($pathName) {
224 $pathName = (string) $pathName;
225 $this->pathName = $pathName;
229 * Getter for path name
231 * @return $pathName The current path name
233 public final function getPathName () {
234 return $this->pathName;