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 Org\Mxchange\CoreFramework\Deprecated\PathIsNoDirectoryException;
14 use \DirectoryIterator;
17 * A class for directory reading and getting its contents, no recursion!
19 * @author Roland Haeder <webmaster@shipsimu.org>
21 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
22 * @license GNU GPL 3.0 or any newer version
23 * @link http://www.shipsimu.org
25 * This program is free software: you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation, either version 3 of the License, or
28 * (at your option) any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with this program. If not, see <http://www.gnu.org/licenses/>.
38 class FrameworkDirectoryPointer extends BaseFrameworkSystem implements FrameworkDirectory {
40 * The current path we are working in
42 private $pathName = '';
45 * The directory iterator instance
47 private $iteratorInstance = NULL;
50 * Protected constructor
52 protected function __construct () {
53 // Call parent constructor
54 parent::__construct(__CLASS__);
58 * Destructor for cleaning purposes, etc
60 public function __destruct() {
61 // Is there a resource pointer? Then we have to close the directory here!
62 if ($this->getDirectoryIteratorInstance() instanceof DirectoryIterator) {
63 // Try to close a directory
64 $this->closeDirectory();
67 // Call the parent destructor
72 * Create a directory pointer based on the given path. The path will also
75 * @param $pathName The path name we shall pass to opendir()
76 * @return $pointerInstance A prepared instance of FrameworkDirectoryPointer
77 * @throws NullPointerException If the provided path name is null
78 * @throws InvalidPathStringException If the provided path name is not a string
79 * @throws PathIsNoDirectoryException If the provided path name is not valid
80 * @throws PathReadProtectedException If the provided path name is read-protected
82 public static final function createFrameworkDirectoryPointer ($pathName) {
83 // Some pre-sanity checks...
84 if (is_null($pathName)) {
86 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
87 } elseif (!is_string($pathName)) {
89 throw new InvalidPathStringException(NULL, self::EXCEPTION_INVALID_STRING);
90 } elseif (!is_dir($pathName)) {
92 throw new PathIsNoDirectoryException($pathName, self::EXCEPTION_INVALID_PATH_NAME);
93 } elseif (!is_readable($pathName)) {
95 throw new PathReadProtectedException($pathName, self::EXCEPTION_READ_PROTECED_PATH);
98 // Create new instance
99 $pointerInstance = new FrameworkDirectoryPointer();
101 // Get an iterator for the directory
102 $iteratorInstance = new DirectoryIterator($pathName);
104 // ... and rewind back
105 $iteratorInstance->rewind();
107 // Set directory pointer and path name
108 $pointerInstance->setDirectoryIteratorInstance($iteratorInstance);
109 $pointerInstance->setPathName($pathName);
111 // Return the instance
112 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY-POINTER: Opened pathName=' . $pathName . ' - EXIT!');
113 return $pointerInstance;
117 * Read raw lines of data from a directory pointer and return the data
119 * @return $currentEntry Current entry from encapsulated iterator
121 public function readRawDirectory () {
122 // Can the next entry be read?
123 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY-POINTER: CALLED!');
124 assert($this->getDirectoryIteratorInstance()->valid());
126 // Read data from the directory pointer and return it
127 $currentEntry = $this->getDirectoryIteratorInstance()->current();
129 // Return found entry
130 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DIRECTORY-POINTER: currentEntry[%s]=%s - EXIT!', gettype($currentEntry), $currentEntry));
131 return $currentEntry;
135 * Read lines from the current directory pointer except some parts
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 SplFileInfo An instance of a SplFileInfo class
140 public function readDirectoryExcept (array $except = []) {
141 // No exceptions given?
142 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DIRECTORY-POINTER: except()=%d - CALLED!', count($except)));
143 if (count($except) == 0) {
144 // No exception given, so read all files and directories, but not recursive
145 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY-POINTER: No exceptions given, please use readRawDirectory() instead!');
146 return $this->readRawDirectory();
147 } elseif (!$this->getDirectoryIteratorInstance()->valid()) {
148 // No more left to read
149 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY-POINTER: EOD reached - EXIT!');
154 $fileInfoInstance = NULL;
156 // Read a raw line...
157 $currentEntry = $this->readRawDirectory();
159 // Shall we exclude directories?
160 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DIRECTORY-POINTER: currentEntry[]=%s', gettype($currentEntry)));
161 if (is_object($currentEntry)) {
163 $fileInfoInstance = $currentEntry;
165 // Is it a dot-directory or excluded?
166 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DIRECTORY-POINTER: fileInfoInstance->filename=%s,isDot=%d', $fileInfoInstance->getFilename(), intval($this->getDirectoryIteratorInstance()->isDot())));
167 while ($this->getDirectoryIteratorInstance()->valid() && ($this->getDirectoryIteratorInstance()->isDot() || in_array($fileInfoInstance->getFilename(), $except))) {
168 // Update current instance
169 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DIRECTORY-POINTER: fileInfoInstance->filename=%s,isDot=%d', $fileInfoInstance->getFilename(), intval($this->getDirectoryIteratorInstance()->isDot())));
170 $fileInfoInstance = $this->readRawDirectory();
173 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY-POINTER: Invoking directoryIteratorInstance->next() ...');
174 $this->getDirectoryIteratorInstance()->next();
179 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput(sprintf('DIRECTORY-POINTER: fileInfoInstance[%s]=%s - EXIT!', gettype($fileInfoInstance), $fileInfoInstance));
180 return $fileInfoInstance;
184 * Close a directory source and set it's instance to null and the path name
189 public function closeDirectory () {
190 // Close the directory by unsetting it
191 //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('DIRECTORY-POINTER: CALLED!');
192 $this->unsetDirectoryIteratorInstance();
193 $this->setPathName('');
197 * Setter for the directory pointer
199 * @param $iteratorInstance An instanceof a DirectoryIterator class
202 protected final function setDirectoryIteratorInstance (DirectoryIterator $iteratorInstance) {
204 $this->iteratorInstance = $iteratorInstance;
208 * Getter for the directory pointer
210 * @return $iteratorInstance The directory pointer which shall be a valid directory resource
212 public final function getDirectoryIteratorInstance () {
213 return $this->iteratorInstance;
217 * Remove directory iterator instance (effectively closing it) by setting
218 * it to NULL. This will trigger a call on the destructor which will then
219 * "close" the iterator.
221 * @param $iteratorInstance An instanceof a DirectoryIterator class
224 protected final function unsetDirectoryIteratorInstance () {
225 // "Unset" the instance
226 $this->iteratorInstance = NULL;
230 * Setter for path name
232 * @param $pathName The new path name
235 protected final function setPathName (string $pathName) {
236 $this->pathName = $pathName;
240 * Getter for path name
242 * @return $pathName The current path name
244 public final function getPathName () {
245 return $this->pathName;