3 namespace Org\Mxchange\CoreFramework\Filesytem\Pointer;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Filesystem\FrameworkDirectory;
7 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
8 use Org\Mxchange\CoreFramework\Generic\NullPointerException;
9 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
12 use Org\Mxchange\CoreFramework\Deprecated\PathIsNoDirectoryException;
15 use \DirectoryIterator;
16 use \InvalidArgumentException;
19 * A class for directory reading and getting its contents, no recursion!
21 * @author Roland Haeder <webmaster@shipsimu.org>
23 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
24 * @license GNU GPL 3.0 or any newer version
25 * @link http://www.shipsimu.org
27 * This program is free software: you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published by
29 * the Free Software Foundation, either version 3 of the License, or
30 * (at your option) any later version.
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License
38 * along with this program. If not, see <http://www.gnu.org/licenses/>.
40 class FrameworkDirectoryPointer extends BaseFrameworkSystem implements FrameworkDirectory {
42 * The current path we are working in
44 private $pathName = '';
47 * The directory iterator instance
49 private $iteratorInstance = NULL;
52 * Protected constructor
54 private function __construct () {
55 // Call parent constructor
56 parent::__construct(__CLASS__);
60 * Destructor for cleaning purposes, etc
62 public function __destruct() {
63 // Is there a resource pointer? Then we have to close the directory here!
64 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('FRAMEWORK-DIRECTORY-POINTER: DESTRUCTED!');
65 if ($this->getDirectoryIteratorInstance() instanceof DirectoryIterator) {
66 // Try to close a directory
67 $this->closeDirectory();
70 // Call the parent destructor
74 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('FRAMEWORK-DIRECTORY-POINTER: EXIT!');
78 * Create a directory pointer based on the given path. The path will also
81 * @param $pathName The path name we shall pass to opendir()
82 * @return $pointerInstance A prepared instance of FrameworkDirectoryPointer
83 * @throws NullPointerException If the provided path name is null
84 * @throws InvalidPathStringException If the provided path name is not a string
85 * @throws PathIsNoDirectoryException If the provided path name is not valid
86 * @throws PathReadProtectedException If the provided path name is read-protected
88 public static final function createFrameworkDirectoryPointer (string $pathName) {
89 // Some pre-sanity checks...
90 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('FRAMEWORK-DIRECTORY-POINTER: pathName=%s - CALLED!', $pathName));
91 if (empty($pathName)) {
93 throw new InvalidArgumentException('Parameter "pathName" is empty', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
94 } elseif (!is_dir($pathName)) {
96 throw new PathIsNoDirectoryException($pathName, self::EXCEPTION_INVALID_PATH_NAME);
97 } elseif (!is_readable($pathName)) {
99 throw new PathReadProtectedException($pathName, self::EXCEPTION_READ_PROTECED_PATH);
102 // Create new instance
103 $pointerInstance = new FrameworkDirectoryPointer();
105 // Get an iterator for the directory
106 $iteratorInstance = new DirectoryIterator($pathName);
108 // ... and rewind back
109 $iteratorInstance->rewind();
111 // Set directory pointer and path name
112 $pointerInstance->setDirectoryIteratorInstance($iteratorInstance);
113 $pointerInstance->setPathName($pathName);
115 // Return the instance
116 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('DIRECTORY-POINTER: pointerInstance=%s - EXIT!', $pointerInstance->__toString()));
117 return $pointerInstance;
121 * Read raw lines of data from a directory pointer and return the data
123 * @return $currentEntry Current entry from encapsulated iterator
125 public function readRawDirectory () {
126 // Can the next entry be read?
127 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('DIRECTORY-POINTER: CALLED!');
128 assert($this->getDirectoryIteratorInstance()->valid());
130 // Read data from the directory pointer and return it
131 $currentEntry = $this->getDirectoryIteratorInstance()->current();
133 // Return found entry
134 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('DIRECTORY-POINTER: currentEntry[%s]=%s - EXIT!', gettype($currentEntry), $currentEntry));
135 return $currentEntry;
139 * Read lines from the current directory pointer except some parts
141 * @param $except Some parts of a directory we want to ignore. Valid: directory and file names, other values will be silently ignored
142 * @return SplFileInfo An instance of a SplFileInfo class
143 * @throws InvalidArgumentException If a parameter has an invalid value
145 public function readDirectoryExcept (array $except = []) {
146 // No exceptions given?
147 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('DIRECTORY-POINTER: except()=%d - CALLED!', count($except)));
148 if (count($except) == 0) {
149 // No exception given, so read all files and directories, but not recursive
150 throw new InvalidArgumentException('Parameter "except" is an empty array', FrameworkInterface::EXCEPTION_INVALID_ARGUMENT);
151 } elseif (!$this->getDirectoryIteratorInstance()->valid()) {
152 // No more left to read
153 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('DIRECTORY-POINTER: EOD reached - EXIT!');
158 $fileInfoInstance = NULL;
160 // Read a raw line...
161 $currentEntry = $this->readRawDirectory();
163 // Shall we exclude directories?
164 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('DIRECTORY-POINTER: currentEntry[]=%s', gettype($currentEntry)));
165 if (is_object($currentEntry)) {
167 $fileInfoInstance = $currentEntry;
169 // Is it a dot-directory or excluded?
170 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('DIRECTORY-POINTER: fileInfoInstance->filename=%s,isDot=%d', $fileInfoInstance->getFilename(), intval($this->getDirectoryIteratorInstance()->isDot())));
171 while ($this->getDirectoryIteratorInstance()->valid() && ($this->getDirectoryIteratorInstance()->isDot() || in_array($fileInfoInstance->getFilename(), $except))) {
172 // Update current instance
173 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('DIRECTORY-POINTER: fileInfoInstance->filename=%s,isDot=%d', $fileInfoInstance->getFilename(), intval($this->getDirectoryIteratorInstance()->isDot())));
174 $fileInfoInstance = $this->readRawDirectory();
177 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage('DIRECTORY-POINTER: Invoking directoryIteratorInstance->next() ...');
178 $this->getDirectoryIteratorInstance()->next();
183 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('DIRECTORY-POINTER: fileInfoInstance[%s]=%s - EXIT!', gettype($fileInfoInstance), $fileInfoInstance));
184 return $fileInfoInstance;
188 * Close a directory source and set it's instance to null and the path name
193 public function closeDirectory () {
194 // Close the directory by unsetting it
195 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('DIRECTORY-POINTER: CALLED!');
196 $this->unsetDirectoryIteratorInstance();
197 $this->setPathName('');
200 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('DIRECTORY-POINTER: CALLED!');
204 * Setter for the directory pointer
206 * @param $iteratorInstance An instanceof a DirectoryIterator class
209 protected final function setDirectoryIteratorInstance (DirectoryIterator $iteratorInstance) {
211 $this->iteratorInstance = $iteratorInstance;
215 * Getter for the directory pointer
217 * @return $iteratorInstance The directory pointer which shall be a valid directory resource
219 public final function getDirectoryIteratorInstance () {
220 return $this->iteratorInstance;
224 * Remove directory iterator instance (effectively closing it) by setting
225 * it to NULL. This will trigger a call on the destructor which will then
226 * "close" the iterator.
228 * @param $iteratorInstance An instanceof a DirectoryIterator class
231 protected final function unsetDirectoryIteratorInstance () {
232 // "Unset" the instance
233 $this->iteratorInstance = NULL;
237 * Setter for path name
239 * @param $pathName The new path name
242 protected final function setPathName (string $pathName) {
243 $this->pathName = $pathName;
247 * Getter for path name
249 * @return $pathName The current path name
251 public final function getPathName () {
252 return $this->pathName;