3 namespace Org\Mxchange\CoreFramework\Filesytem\Pointer;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Filesystem\FrameworkDirectory;
7 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
8 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
11 use \DirectoryIterator;
14 * A class for directory reading and getting its contents, no recursion!
16 * @author Roland Haeder <webmaster@shipsimu.org>
18 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
19 * @license GNU GPL 3.0 or any newer version
20 * @link http://www.shipsimu.org
22 * This program is free software: you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation, either version 3 of the License, or
25 * (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35 class FrameworkDirectoryPointer extends BaseFrameworkSystem implements FrameworkDirectory {
37 * The current path we are working in
39 private $pathName = '';
42 * The directory iterator instance
44 private $iteratorInstance = NULL;
47 * Protected constructor
49 protected function __construct () {
50 // Call parent constructor
51 parent::__construct(__CLASS__);
55 * Destructor for cleaning purposes, etc
57 public function __destruct() {
58 // Is there a resource pointer? Then we have to close the directory here!
59 if ($this->getDirectoryIteratorInstance() instanceof DirectoryIterator) {
60 // Try to close a directory
61 $this->closeDirectory();
64 // Call the parent destructor
69 * Create a directory pointer based on the given path. The path will also
72 * @param $pathName The path name we shall pass to opendir()
73 * @return $pointerInstance A prepared instance of FrameworkDirectoryPointer
74 * @throws NullPointerException If the provided path name is null
75 * @throws InvalidPathStringException If the provided path name is not a string
76 * @throws PathIsNoDirectoryException If the provided path name is not valid
77 * @throws PathReadProtectedException If the provided path name is read-protected
79 public static final function createFrameworkDirectoryPointer ($pathName) {
80 // Some pre-sanity checks...
81 if (is_null($pathName)) {
83 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
84 } elseif (!is_string($pathName)) {
86 throw new InvalidPathStringException(NULL, self::EXCEPTION_INVALID_STRING);
87 } elseif (!is_dir($pathName)) {
89 throw new PathIsNoDirectoryException($pathName, self::EXCEPTION_INVALID_PATH_NAME);
90 } elseif (!is_readable($pathName)) {
92 throw new PathReadProtectedException($pathName, self::EXCEPTION_READ_PROTECED_PATH);
95 // Create new instance
96 $pointerInstance = new FrameworkDirectoryPointer();
98 // Get an iterator for the directory
99 $iteratorInstance = new DirectoryIterator($pathName);
101 // ... and rewind back
102 $iteratorInstance->rewind();
104 // Set directory pointer and path name
105 $pointerInstance->setDirectoryIteratorInstance($iteratorInstance);
106 $pointerInstance->setPathName($pathName);
108 // Return the instance
109 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: Opened pathName=' . $pathName . ' - EXIT!');
110 return $pointerInstance;
114 * Read raw lines of data from a directory pointer and return the data
116 * @return $currentEntry Current entry from encapsulated iterator
118 public function readRawDirectory () {
119 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . '] - CALLED!');
121 // Can the next entry be read?
122 assert($this->getDirectoryIteratorInstance()->valid());
124 // Read data from the directory pointer and return it
125 $currentEntry = $this->getDirectoryIteratorInstance()->current();
127 // Return found entry
128 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: currentEntry[]=' . gettype($currentEntry) . ' - EXIT!');
129 return $currentEntry;
133 * Read lines from the current directory pointer except some parts
135 * @param $except Some parts of a directory we want to ignore. Valid: directory and file names, other values will be silently ignored
136 * @return SplFileInfo An instance of a SplFileInfo class
138 public function readDirectoryExcept (array $except = array()) {
139 // No exceptions given?
140 if (count($except) == 0) {
141 // No exception given, so read all files and directories, but not recursive
142 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: No exceptions given, please use readRawDirectory() instead!');
143 return $this->readRawDirectory();
144 } elseif (!$this->getDirectoryIteratorInstance()->valid()) {
145 // No more left to read
146 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: EOD reached.');
151 $fileInfoInstance = NULL;
153 // Read a raw line...
154 $currentEntry = $this->readRawDirectory();
155 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: currentEntry[]=' . gettype($currentEntry));
157 // Shall we exclude directories?
158 if (is_object($currentEntry)) {
160 $fileInfoInstance = $currentEntry;
161 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: fileInfoInstance[' . gettype($fileInfoInstance) . ']=' . $fileInfoInstance . ',isDot=' . intval($this->getDirectoryIteratorInstance()->isDot()));
163 // Is it a dot-directory or excluded?
164 if (($this->getDirectoryIteratorInstance()->isDot()) && (!in_array($fileInfoInstance, $except))) {
166 $this->getDirectoryIteratorInstance()->next();
169 $fileInfoInstance = $this->readDirectoryExcept($except);
170 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: fileInfoInstance[' . gettype($fileInfoInstance) . ']=' . $fileInfoInstance . ' - Recursive call!');
175 $this->getDirectoryIteratorInstance()->next();
178 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY[' . __METHOD__ . ':' . __LINE__ . ']: fileInfoInstance[' . gettype($fileInfoInstance) . ']=' . $fileInfoInstance);
179 return $fileInfoInstance;
183 * Close a directory source and set it's instance to null and the path name
188 public function closeDirectory () {
189 // Close the directory by unsetting it
190 $this->unsetDirectoryIteratorInstance();
191 $this->setPathName('');
195 * Setter for the directory pointer
197 * @param $iteratorInstance An instanceof a DirectoryIterator class
200 protected final function setDirectoryIteratorInstance (DirectoryIterator $iteratorInstance) {
202 $this->iteratorInstance = $iteratorInstance;
206 * Getter for the directory pointer
208 * @return $iteratorInstance The directory pointer which shall be a valid directory resource
210 public final function getDirectoryIteratorInstance () {
211 return $this->iteratorInstance;
215 * Remove directory iterator instance (effectively closing it) by setting
216 * it to NULL. This will trigger a call on the destructor which will then
217 * "close" the iterator.
219 * @param $iteratorInstance An instanceof a DirectoryIterator class
222 protected final function unsetDirectoryIteratorInstance () {
223 // "Unset" the instance
224 $this->iteratorInstance = NULL;
228 * Setter for path name
230 * @param $pathName The new path name
233 protected final function setPathName ($pathName) {
234 $pathName = (string) $pathName;
235 $this->pathName = $pathName;
239 * Getter for path name
241 * @return $pathName The current path name
243 public final function getPathName () {
244 return $this->pathName;