├── ChangeLog.md ├── LICENSE ├── README.md ├── SECURITY.md ├── composer.json └── src ├── ExcludeIterator.php ├── Facade.php ├── Factory.php └── Iterator.php /ChangeLog.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). 4 | 5 | ## [6.0.0] - 2025-02-07 6 | 7 | ### Removed 8 | 9 | * This component is no longer supported on PHP 8.2 10 | 11 | ## [5.1.0] - 2024-08-27 12 | 13 | ### Added 14 | 15 | * [#83](https://github.com/sebastianbergmann/php-file-iterator/pull/83): Support for "Globstar" pattern 16 | 17 | ## [5.0.1] - 2024-07-03 18 | 19 | ### Changed 20 | 21 | * This project now uses PHPStan instead of Psalm for static analysis 22 | 23 | ## [5.0.0] - 2024-02-02 24 | 25 | ### Removed 26 | 27 | * This component is no longer supported on PHP 8.1 28 | 29 | ## [4.1.0] - 2023-08-31 30 | 31 | ### Added 32 | 33 | * [#81](https://github.com/sebastianbergmann/php-file-iterator/issues/81): Accept `array|string $paths` in `Facade::getFilesAsArray()` 34 | 35 | ## [4.0.2] - 2023-05-07 36 | 37 | ### Fixed 38 | 39 | * [#80](https://github.com/sebastianbergmann/php-file-iterator/pull/80): Ignore unresolvable symbolic link 40 | 41 | ## [4.0.1] - 2023-02-10 42 | 43 | ### Fixed 44 | 45 | * [#67](https://github.com/sebastianbergmann/php-file-iterator/issues/61): Excluded directories are traversed unnecessarily 46 | 47 | ## [4.0.0] - 2023-02-03 48 | 49 | ### Removed 50 | 51 | * The optional `$commonPath` parameter of `SebastianBergmann\FileIterator\Facade` as well as the functionality it controlled has been removed 52 | * The `SebastianBergmann\FileIterator\Factory` and `SebastianBergmann\FileIterator\Iterator` classes are now marked `@internal` 53 | * This component is no longer supported on PHP 7.3, PHP 7.4 and PHP 8.0 54 | 55 | ## [3.0.6] - 2021-12-02 56 | 57 | ### Changed 58 | 59 | * [#73](https://github.com/sebastianbergmann/php-file-iterator/pull/73): Micro performance improvements on parsing paths 60 | 61 | ## [3.0.5] - 2020-09-28 62 | 63 | ### Changed 64 | 65 | * Changed PHP version constraint in `composer.json` from `^7.3 || ^8.0` to `>=7.3` 66 | 67 | ## [3.0.4] - 2020-07-11 68 | 69 | ### Fixed 70 | 71 | * [#67](https://github.com/sebastianbergmann/php-file-iterator/issues/67): `TypeError` in `SebastianBergmann\FileIterator\Iterator::accept()` 72 | 73 | ## [3.0.3] - 2020-06-26 74 | 75 | ### Added 76 | 77 | * This component is now supported on PHP 8 78 | 79 | ## [3.0.2] - 2020-06-15 80 | 81 | ### Changed 82 | 83 | * Tests etc. are now ignored for archive exports 84 | 85 | ## [3.0.1] - 2020-04-18 86 | 87 | ### Fixed 88 | 89 | * [#64](https://github.com/sebastianbergmann/php-file-iterator/issues/64): Release tarball contains Composer PHAR 90 | 91 | ## [3.0.0] - 2020-02-07 92 | 93 | ### Removed 94 | 95 | * This component is no longer supported on PHP 7.1 and PHP 7.2 96 | 97 | ## [2.0.5] - 2021-12-02 98 | 99 | ### Changed 100 | 101 | * [#73](https://github.com/sebastianbergmann/php-file-iterator/pull/73): Micro performance improvements on parsing paths 102 | 103 | ### Fixed 104 | 105 | * [#74](https://github.com/sebastianbergmann/php-file-iterator/pull/74): Document return type of `SebastianBergmann\FileIterator\Iterator::accept()` so that Symfony's `DebugClassLoader` does not trigger a deprecation warning 106 | 107 | ## [2.0.4] - 2021-07-19 108 | 109 | ### Changed 110 | 111 | * Added `ReturnTypeWillChange` attribute to `SebastianBergmann\FileIterator\Iterator::accept()` because the return type of `\FilterIterator::accept()` will change in PHP 8.1 112 | 113 | ## [2.0.3] - 2020-11-30 114 | 115 | ### Changed 116 | 117 | * Changed PHP version constraint in `composer.json` from `^7.1` to `>=7.1` 118 | 119 | ## [2.0.2] - 2018-09-13 120 | 121 | ### Fixed 122 | 123 | * [#48](https://github.com/sebastianbergmann/php-file-iterator/issues/48): Excluding an array that contains false ends up excluding the current working directory 124 | 125 | ## [2.0.1] - 2018-06-11 126 | 127 | ### Fixed 128 | 129 | * [#46](https://github.com/sebastianbergmann/php-file-iterator/issues/46): Regression with hidden parent directory 130 | 131 | ## [2.0.0] - 2018-05-28 132 | 133 | ### Fixed 134 | 135 | * [#30](https://github.com/sebastianbergmann/php-file-iterator/issues/30): Exclude is not considered if it is a parent of the base path 136 | 137 | ### Changed 138 | 139 | * This component now uses namespaces 140 | 141 | ### Removed 142 | 143 | * This component is no longer supported on PHP 5.3, PHP 5.4, PHP 5.5, PHP 5.6, and PHP 7.0 144 | 145 | ## [1.4.5] - 2017-11-27 146 | 147 | ### Fixed 148 | 149 | * [#37](https://github.com/sebastianbergmann/php-file-iterator/issues/37): Regression caused by fix for [#30](https://github.com/sebastianbergmann/php-file-iterator/issues/30) 150 | 151 | ## [1.4.4] - 2017-11-27 152 | 153 | ### Fixed 154 | 155 | * [#30](https://github.com/sebastianbergmann/php-file-iterator/issues/30): Exclude is not considered if it is a parent of the base path 156 | 157 | ## [1.4.3] - 2017-11-25 158 | 159 | ### Fixed 160 | 161 | * [#34](https://github.com/sebastianbergmann/php-file-iterator/issues/34): Factory should use canonical directory names 162 | 163 | ## [1.4.2] - 2016-11-26 164 | 165 | No changes 166 | 167 | ## [1.4.1] - 2015-07-26 168 | 169 | No changes 170 | 171 | ## 1.4.0 - 2015-04-02 172 | 173 | ### Added 174 | 175 | * [#23](https://github.com/sebastianbergmann/php-file-iterator/pull/23): Added support for wildcards (glob) in exclude 176 | 177 | [6.0.0]: https://github.com/sebastianbergmann/php-file-iterator/compare/5.1...6.0.0 178 | [5.1.0]: https://github.com/sebastianbergmann/php-file-iterator/compare/5.0.1...5.1.0 179 | [5.0.1]: https://github.com/sebastianbergmann/php-file-iterator/compare/5.0.0...5.0.1 180 | [5.0.0]: https://github.com/sebastianbergmann/php-file-iterator/compare/4.1...5.0.0 181 | [4.1.0]: https://github.com/sebastianbergmann/php-file-iterator/compare/4.0.2...4.1.0 182 | [4.0.2]: https://github.com/sebastianbergmann/php-file-iterator/compare/4.0.1...4.0.2 183 | [4.0.1]: https://github.com/sebastianbergmann/php-file-iterator/compare/4.0.0...4.0.1 184 | [4.0.0]: https://github.com/sebastianbergmann/php-file-iterator/compare/3.0.6...4.0.0 185 | [3.0.6]: https://github.com/sebastianbergmann/php-file-iterator/compare/3.0.5...3.0.6 186 | [3.0.5]: https://github.com/sebastianbergmann/php-file-iterator/compare/3.0.4...3.0.5 187 | [3.0.4]: https://github.com/sebastianbergmann/php-file-iterator/compare/3.0.3...3.0.4 188 | [3.0.3]: https://github.com/sebastianbergmann/php-file-iterator/compare/3.0.2...3.0.3 189 | [3.0.2]: https://github.com/sebastianbergmann/php-file-iterator/compare/3.0.1...3.0.2 190 | [3.0.1]: https://github.com/sebastianbergmann/php-file-iterator/compare/3.0.0...3.0.1 191 | [3.0.0]: https://github.com/sebastianbergmann/php-file-iterator/compare/2.0.5...3.0.0 192 | [2.0.5]: https://github.com/sebastianbergmann/php-file-iterator/compare/2.0.4...2.0.5 193 | [2.0.4]: https://github.com/sebastianbergmann/php-file-iterator/compare/2.0.3...2.0.4 194 | [2.0.3]: https://github.com/sebastianbergmann/php-file-iterator/compare/2.0.2...2.0.3 195 | [2.0.2]: https://github.com/sebastianbergmann/php-file-iterator/compare/2.0.1...2.0.2 196 | [2.0.1]: https://github.com/sebastianbergmann/php-file-iterator/compare/2.0.0...2.0.1 197 | [2.0.0]: https://github.com/sebastianbergmann/php-file-iterator/compare/1.4.5...2.0.0 198 | [1.4.5]: https://github.com/sebastianbergmann/php-file-iterator/compare/1.4.4...1.4.5 199 | [1.4.4]: https://github.com/sebastianbergmann/php-file-iterator/compare/1.4.3...1.4.4 200 | [1.4.3]: https://github.com/sebastianbergmann/php-file-iterator/compare/1.4.2...1.4.3 201 | [1.4.2]: https://github.com/sebastianbergmann/php-file-iterator/compare/1.4.1...1.4.2 202 | [1.4.1]: https://github.com/sebastianbergmann/php-file-iterator/compare/1.4.0...1.4.1 203 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2009-2025, Sebastian Bergmann 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | 1. Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | 3. Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Latest Stable Version](https://poser.pugx.org/phpunit/php-file-iterator/v)](https://packagist.org/packages/phpunit/php-file-iterator) 2 | [![CI Status](https://github.com/sebastianbergmann/php-file-iterator/workflows/CI/badge.svg)](https://github.com/sebastianbergmann/php-file-iterator/actions) 3 | [![Type Coverage](https://shepherd.dev/github/sebastianbergmann/php-file-iterator/coverage.svg)](https://shepherd.dev/github/sebastianbergmann/php-file-iterator) 4 | [![codecov](https://codecov.io/gh/sebastianbergmann/php-file-iterator/branch/main/graph/badge.svg)](https://codecov.io/gh/sebastianbergmann/php-file-iterator) 5 | 6 | # php-file-iterator 7 | 8 | ## Installation 9 | 10 | You can add this library as a local, per-project dependency to your project using [Composer](https://getcomposer.org/): 11 | 12 | composer require phpunit/php-file-iterator 13 | 14 | If you only need this library during development, for instance to run your project's test suite, then you should add it as a development-time dependency: 15 | 16 | composer require --dev phpunit/php-file-iterator 17 | 18 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | If you believe you have found a security vulnerability in the library that is developed in this repository, please report it to us through coordinated disclosure. 4 | 5 | **Please do not report security vulnerabilities through public GitHub issues, discussions, or pull requests.** 6 | 7 | Instead, please email `sebastian@phpunit.de`. 8 | 9 | Please include as much of the information listed below as you can to help us better understand and resolve the issue: 10 | 11 | * The type of issue 12 | * Full paths of source file(s) related to the manifestation of the issue 13 | * The location of the affected source code (tag/branch/commit or direct URL) 14 | * Any special configuration required to reproduce the issue 15 | * Step-by-step instructions to reproduce the issue 16 | * Proof-of-concept or exploit code (if possible) 17 | * Impact of the issue, including how an attacker might exploit the issue 18 | 19 | This information will help us triage your report more quickly. 20 | 21 | ## Web Context 22 | 23 | The library that is developed in this repository was either extracted from [PHPUnit](https://github.com/sebastianbergmann/phpunit) or developed specifically as a dependency for PHPUnit. 24 | 25 | The library is developed with a focus on development environments and the command-line. No specific testing or hardening with regard to using the library in an HTTP or web context or with untrusted input data is performed. The library might also contain functionality that intentionally exposes internal application data for debugging purposes. 26 | 27 | If the library is used in a web application, the application developer is responsible for filtering inputs or escaping outputs as necessary and for verifying that the used functionality is safe for use within the intended context. 28 | 29 | Vulnerabilities specific to the use outside a development context will be fixed as applicable, provided that the fix does not have an averse effect on the primary use case for development purposes. 30 | 31 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "phpunit/php-file-iterator", 3 | "description": "FilterIterator implementation that filters files based on a list of suffixes.", 4 | "type": "library", 5 | "keywords": [ 6 | "iterator", 7 | "filesystem" 8 | ], 9 | "homepage": "https://github.com/sebastianbergmann/php-file-iterator/", 10 | "license": "BSD-3-Clause", 11 | "authors": [ 12 | { 13 | "name": "Sebastian Bergmann", 14 | "email": "sebastian@phpunit.de", 15 | "role": "lead" 16 | } 17 | ], 18 | "support": { 19 | "issues": "https://github.com/sebastianbergmann/php-file-iterator/issues", 20 | "security": "https://github.com/sebastianbergmann/php-file-iterator/security/policy" 21 | }, 22 | "config": { 23 | "platform": { 24 | "php": "8.3.0" 25 | }, 26 | "optimize-autoloader": true, 27 | "sort-packages": true 28 | }, 29 | "prefer-stable": true, 30 | "require": { 31 | "php": ">=8.3" 32 | }, 33 | "require-dev": { 34 | "phpunit/phpunit": "^12.0" 35 | }, 36 | "autoload": { 37 | "classmap": [ 38 | "src/" 39 | ] 40 | }, 41 | "extra": { 42 | "branch-alias": { 43 | "dev-main": "6.0-dev" 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/ExcludeIterator.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | namespace SebastianBergmann\FileIterator; 11 | 12 | use function assert; 13 | use function str_starts_with; 14 | use RecursiveDirectoryIterator; 15 | use RecursiveFilterIterator; 16 | use SplFileInfo; 17 | 18 | /** 19 | * @internal This class is not covered by the backward compatibility promise for phpunit/php-file-iterator 20 | */ 21 | final class ExcludeIterator extends RecursiveFilterIterator 22 | { 23 | /** 24 | * @var list 25 | */ 26 | private array $exclude; 27 | 28 | /** 29 | * @param list $exclude 30 | */ 31 | public function __construct(RecursiveDirectoryIterator $iterator, array $exclude) 32 | { 33 | parent::__construct($iterator); 34 | 35 | $this->exclude = $exclude; 36 | } 37 | 38 | public function accept(): bool 39 | { 40 | $current = $this->current(); 41 | 42 | assert($current instanceof SplFileInfo); 43 | 44 | $path = $current->getRealPath(); 45 | 46 | if ($path === false) { 47 | return false; 48 | } 49 | 50 | foreach ($this->exclude as $exclude) { 51 | if (str_starts_with($path, $exclude)) { 52 | return false; 53 | } 54 | } 55 | 56 | return true; 57 | } 58 | 59 | public function hasChildren(): bool 60 | { 61 | return $this->getInnerIterator()->hasChildren(); 62 | } 63 | 64 | public function getChildren(): self 65 | { 66 | return new self( 67 | $this->getInnerIterator()->getChildren(), 68 | $this->exclude, 69 | ); 70 | } 71 | 72 | public function getInnerIterator(): RecursiveDirectoryIterator 73 | { 74 | $innerIterator = parent::getInnerIterator(); 75 | 76 | assert($innerIterator instanceof RecursiveDirectoryIterator); 77 | 78 | return $innerIterator; 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/Facade.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | namespace SebastianBergmann\FileIterator; 11 | 12 | use function array_unique; 13 | use function assert; 14 | use function sort; 15 | use SplFileInfo; 16 | 17 | /** 18 | * @no-named-arguments Parameter names are not covered by the backward compatibility promise for PHPUnit 19 | */ 20 | final class Facade 21 | { 22 | /** 23 | * @param list|non-empty-string $paths 24 | * @param list|string $suffixes 25 | * @param list|string $prefixes 26 | * @param list $exclude 27 | * 28 | * @return list 29 | */ 30 | public function getFilesAsArray(array|string $paths, array|string $suffixes = '', array|string $prefixes = '', array $exclude = []): array 31 | { 32 | $iterator = (new Factory)->getFileIterator($paths, $suffixes, $prefixes, $exclude); 33 | 34 | $files = []; 35 | 36 | foreach ($iterator as $file) { 37 | assert($file instanceof SplFileInfo); 38 | 39 | $file = $file->getRealPath(); 40 | 41 | if ($file) { 42 | $files[] = $file; 43 | } 44 | } 45 | 46 | $files = array_unique($files); 47 | 48 | sort($files); 49 | 50 | return $files; 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/Factory.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | namespace SebastianBergmann\FileIterator; 11 | 12 | use const GLOB_ONLYDIR; 13 | use function array_filter; 14 | use function array_map; 15 | use function array_merge; 16 | use function array_unique; 17 | use function array_values; 18 | use function glob; 19 | use function is_dir; 20 | use function is_string; 21 | use function realpath; 22 | use function sort; 23 | use function stripos; 24 | use function substr; 25 | use AppendIterator; 26 | use FilesystemIterator; 27 | use RecursiveDirectoryIterator; 28 | use RecursiveIteratorIterator; 29 | 30 | /** 31 | * @internal This class is not covered by the backward compatibility promise for phpunit/php-file-iterator 32 | */ 33 | final class Factory 34 | { 35 | /** 36 | * @param list|non-empty-string $paths 37 | * @param list|string $suffixes 38 | * @param list|string $prefixes 39 | * @param list $exclude 40 | * 41 | * @phpstan-ignore missingType.generics 42 | */ 43 | public function getFileIterator(array|string $paths, array|string $suffixes = '', array|string $prefixes = '', array $exclude = []): AppendIterator 44 | { 45 | if (is_string($paths)) { 46 | $paths = [$paths]; 47 | } 48 | 49 | $paths = $this->resolveWildcards($paths); 50 | $exclude = $this->resolveWildcards($exclude); 51 | 52 | if (is_string($prefixes)) { 53 | if ($prefixes !== '') { 54 | $prefixes = [$prefixes]; 55 | } else { 56 | $prefixes = []; 57 | } 58 | } 59 | 60 | if (is_string($suffixes)) { 61 | if ($suffixes !== '') { 62 | $suffixes = [$suffixes]; 63 | } else { 64 | $suffixes = []; 65 | } 66 | } 67 | 68 | $iterator = new AppendIterator; 69 | 70 | foreach ($paths as $path) { 71 | if (is_dir($path)) { 72 | $iterator->append( 73 | new Iterator( 74 | $path, 75 | new RecursiveIteratorIterator( 76 | new ExcludeIterator( 77 | new RecursiveDirectoryIterator($path, FilesystemIterator::FOLLOW_SYMLINKS | FilesystemIterator::SKIP_DOTS), 78 | $exclude, 79 | ), 80 | ), 81 | $suffixes, 82 | $prefixes, 83 | ), 84 | ); 85 | } 86 | } 87 | 88 | return $iterator; 89 | } 90 | 91 | /** 92 | * @param list $paths 93 | * 94 | * @return list 95 | */ 96 | private function resolveWildcards(array $paths): array 97 | { 98 | $_paths = [[]]; 99 | 100 | foreach ($paths as $path) { 101 | $locals = $this->globstar($path); 102 | 103 | if ($locals !== []) { 104 | $_paths[] = array_map('\realpath', $locals); 105 | } else { 106 | // @codeCoverageIgnoreStart 107 | $_paths[] = [realpath($path)]; 108 | // @codeCoverageIgnoreEnd 109 | } 110 | } 111 | 112 | return array_values(array_filter(array_merge(...$_paths))); 113 | } 114 | 115 | /** 116 | * @see https://gist.github.com/funkjedi/3feee27d873ae2297b8e2370a7082aad 117 | * 118 | * @return list 119 | */ 120 | private function globstar(string $pattern): array 121 | { 122 | if (stripos($pattern, '**') === false) { 123 | $files = glob($pattern, GLOB_ONLYDIR); 124 | } else { 125 | $position = stripos($pattern, '**'); 126 | $rootPattern = substr($pattern, 0, $position - 1); 127 | $restPattern = substr($pattern, $position + 2); 128 | 129 | $patterns = [$rootPattern . $restPattern]; 130 | $rootPattern .= '/*'; 131 | 132 | while ($directories = glob($rootPattern, GLOB_ONLYDIR)) { 133 | $rootPattern .= '/*'; 134 | 135 | foreach ($directories as $directory) { 136 | $patterns[] = $directory . $restPattern; 137 | } 138 | } 139 | 140 | $files = []; 141 | 142 | foreach ($patterns as $_pattern) { 143 | $files = array_merge($files, $this->globstar($_pattern)); 144 | } 145 | } 146 | 147 | if ($files !== false) { 148 | $files = array_unique($files); 149 | 150 | sort($files); 151 | 152 | return $files; 153 | } 154 | 155 | // @codeCoverageIgnoreStart 156 | return []; 157 | // @codeCoverageIgnoreEnd 158 | } 159 | } 160 | -------------------------------------------------------------------------------- /src/Iterator.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | namespace SebastianBergmann\FileIterator; 11 | 12 | use function preg_match; 13 | use function realpath; 14 | use function str_ends_with; 15 | use function str_replace; 16 | use function str_starts_with; 17 | use FilterIterator; 18 | use SplFileInfo; 19 | 20 | /** 21 | * @template-extends FilterIterator 22 | * 23 | * @internal This class is not covered by the backward compatibility promise for phpunit/php-file-iterator 24 | */ 25 | final class Iterator extends FilterIterator 26 | { 27 | public const int PREFIX = 0; 28 | public const int SUFFIX = 1; 29 | private false|string $basePath; 30 | 31 | /** 32 | * @var list 33 | */ 34 | private array $suffixes; 35 | 36 | /** 37 | * @var list 38 | */ 39 | private array $prefixes; 40 | 41 | /** 42 | * @param list $suffixes 43 | * @param list $prefixes 44 | */ 45 | public function __construct(string $basePath, \Iterator $iterator, array $suffixes = [], array $prefixes = []) 46 | { 47 | $this->basePath = realpath($basePath); 48 | $this->prefixes = $prefixes; 49 | $this->suffixes = $suffixes; 50 | 51 | parent::__construct($iterator); 52 | } 53 | 54 | public function accept(): bool 55 | { 56 | $current = $this->getInnerIterator()->current(); 57 | 58 | $filename = $current->getFilename(); 59 | $realPath = $current->getRealPath(); 60 | 61 | if ($realPath === false) { 62 | // @codeCoverageIgnoreStart 63 | return false; 64 | // @codeCoverageIgnoreEnd 65 | } 66 | 67 | return $this->acceptPath($realPath) && 68 | $this->acceptPrefix($filename) && 69 | $this->acceptSuffix($filename); 70 | } 71 | 72 | private function acceptPath(string $path): bool 73 | { 74 | // Filter files in hidden directories by checking path that is relative to the base path. 75 | if (preg_match('=/\.[^/]*/=', str_replace((string) $this->basePath, '', $path)) === 1) { 76 | return false; 77 | } 78 | 79 | return true; 80 | } 81 | 82 | private function acceptPrefix(string $filename): bool 83 | { 84 | return $this->acceptSubString($filename, $this->prefixes, self::PREFIX); 85 | } 86 | 87 | private function acceptSuffix(string $filename): bool 88 | { 89 | return $this->acceptSubString($filename, $this->suffixes, self::SUFFIX); 90 | } 91 | 92 | /** 93 | * @param list $subStrings 94 | */ 95 | private function acceptSubString(string $filename, array $subStrings, int $type): bool 96 | { 97 | if ($subStrings === []) { 98 | return true; 99 | } 100 | 101 | foreach ($subStrings as $string) { 102 | if (($type === self::PREFIX && str_starts_with($filename, $string)) || 103 | ($type === self::SUFFIX && str_ends_with($filename, $string))) { 104 | return true; 105 | } 106 | } 107 | 108 | return false; 109 | } 110 | } 111 | --------------------------------------------------------------------------------