Upgrade framework
This commit is contained in:
368
vendor/symfony/finder/Finder.php
vendored
368
vendor/symfony/finder/Finder.php
vendored
@@ -13,12 +13,14 @@ namespace Symfony\Component\Finder;
|
||||
|
||||
use Symfony\Component\Finder\Comparator\DateComparator;
|
||||
use Symfony\Component\Finder\Comparator\NumberComparator;
|
||||
use Symfony\Component\Finder\Exception\DirectoryNotFoundException;
|
||||
use Symfony\Component\Finder\Iterator\CustomFilterIterator;
|
||||
use Symfony\Component\Finder\Iterator\DateRangeFilterIterator;
|
||||
use Symfony\Component\Finder\Iterator\DepthRangeFilterIterator;
|
||||
use Symfony\Component\Finder\Iterator\ExcludeDirectoryFilterIterator;
|
||||
use Symfony\Component\Finder\Iterator\FilecontentFilterIterator;
|
||||
use Symfony\Component\Finder\Iterator\FilenameFilterIterator;
|
||||
use Symfony\Component\Finder\Iterator\LazyIterator;
|
||||
use Symfony\Component\Finder\Iterator\SizeRangeFilterIterator;
|
||||
use Symfony\Component\Finder\Iterator\SortableIterator;
|
||||
|
||||
@@ -29,41 +31,42 @@ use Symfony\Component\Finder\Iterator\SortableIterator;
|
||||
*
|
||||
* All rules may be invoked several times.
|
||||
*
|
||||
* All methods return the current Finder object to allow easy chaining:
|
||||
* All methods return the current Finder object to allow chaining:
|
||||
*
|
||||
* $finder = Finder::create()->files()->name('*.php')->in(__DIR__);
|
||||
* $finder = Finder::create()->files()->name('*.php')->in(__DIR__);
|
||||
*
|
||||
* @author Fabien Potencier <fabien@symfony.com>
|
||||
*
|
||||
* @implements \IteratorAggregate<string, SplFileInfo>
|
||||
*/
|
||||
class Finder implements \IteratorAggregate, \Countable
|
||||
{
|
||||
const IGNORE_VCS_FILES = 1;
|
||||
const IGNORE_DOT_FILES = 2;
|
||||
public const IGNORE_VCS_FILES = 1;
|
||||
public const IGNORE_DOT_FILES = 2;
|
||||
public const IGNORE_VCS_IGNORED_FILES = 4;
|
||||
|
||||
private $mode = 0;
|
||||
private $names = array();
|
||||
private $notNames = array();
|
||||
private $exclude = array();
|
||||
private $filters = array();
|
||||
private $depths = array();
|
||||
private $sizes = array();
|
||||
private $followLinks = false;
|
||||
private $sort = false;
|
||||
private $ignore = 0;
|
||||
private $dirs = array();
|
||||
private $dates = array();
|
||||
private $iterators = array();
|
||||
private $contains = array();
|
||||
private $notContains = array();
|
||||
private $paths = array();
|
||||
private $notPaths = array();
|
||||
private $ignoreUnreadableDirs = false;
|
||||
private int $mode = 0;
|
||||
private array $names = [];
|
||||
private array $notNames = [];
|
||||
private array $exclude = [];
|
||||
private array $filters = [];
|
||||
private array $depths = [];
|
||||
private array $sizes = [];
|
||||
private bool $followLinks = false;
|
||||
private bool $reverseSorting = false;
|
||||
private \Closure|int|false $sort = false;
|
||||
private int $ignore = 0;
|
||||
private array $dirs = [];
|
||||
private array $dates = [];
|
||||
private array $iterators = [];
|
||||
private array $contains = [];
|
||||
private array $notContains = [];
|
||||
private array $paths = [];
|
||||
private array $notPaths = [];
|
||||
private bool $ignoreUnreadableDirs = false;
|
||||
|
||||
private static $vcsPatterns = array('.svn', '_svn', 'CVS', '_darcs', '.arch-params', '.monotone', '.bzr', '.git', '.hg');
|
||||
private static array $vcsPatterns = ['.svn', '_svn', 'CVS', '_darcs', '.arch-params', '.monotone', '.bzr', '.git', '.hg'];
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->ignore = static::IGNORE_VCS_FILES | static::IGNORE_DOT_FILES;
|
||||
@@ -71,10 +74,8 @@ class Finder implements \IteratorAggregate, \Countable
|
||||
|
||||
/**
|
||||
* Creates a new Finder.
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public static function create()
|
||||
public static function create(): static
|
||||
{
|
||||
return new static();
|
||||
}
|
||||
@@ -84,7 +85,7 @@ class Finder implements \IteratorAggregate, \Countable
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function directories()
|
||||
public function directories(): static
|
||||
{
|
||||
$this->mode = Iterator\FileTypeFilterIterator::ONLY_DIRECTORIES;
|
||||
|
||||
@@ -96,7 +97,7 @@ class Finder implements \IteratorAggregate, \Countable
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function files()
|
||||
public function files(): static
|
||||
{
|
||||
$this->mode = Iterator\FileTypeFilterIterator::ONLY_FILES;
|
||||
|
||||
@@ -108,19 +109,22 @@ class Finder implements \IteratorAggregate, \Countable
|
||||
*
|
||||
* Usage:
|
||||
*
|
||||
* $finder->depth('> 1') // the Finder will start matching at level 1.
|
||||
* $finder->depth('< 3') // the Finder will descend at most 3 levels of directories below the starting point.
|
||||
* $finder->depth('> 1') // the Finder will start matching at level 1.
|
||||
* $finder->depth('< 3') // the Finder will descend at most 3 levels of directories below the starting point.
|
||||
* $finder->depth(['>= 1', '< 3'])
|
||||
*
|
||||
* @param string|int $level The depth level expression
|
||||
* @param string|int|string[]|int[] $levels The depth level expression or an array of depth levels
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @see DepthRangeFilterIterator
|
||||
* @see NumberComparator
|
||||
*/
|
||||
public function depth($level)
|
||||
public function depth(string|int|array $levels): static
|
||||
{
|
||||
$this->depths[] = new Comparator\NumberComparator($level);
|
||||
foreach ((array) $levels as $level) {
|
||||
$this->depths[] = new Comparator\NumberComparator($level);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -130,12 +134,13 @@ class Finder implements \IteratorAggregate, \Countable
|
||||
*
|
||||
* The date must be something that strtotime() is able to parse:
|
||||
*
|
||||
* $finder->date('since yesterday');
|
||||
* $finder->date('until 2 days ago');
|
||||
* $finder->date('> now - 2 hours');
|
||||
* $finder->date('>= 2005-10-15');
|
||||
* $finder->date('since yesterday');
|
||||
* $finder->date('until 2 days ago');
|
||||
* $finder->date('> now - 2 hours');
|
||||
* $finder->date('>= 2005-10-15');
|
||||
* $finder->date(['>= 2005-10-15', '<= 2006-05-27']);
|
||||
*
|
||||
* @param string $date A date range string
|
||||
* @param string|string[] $dates A date range string or an array of date ranges
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
@@ -143,9 +148,11 @@ class Finder implements \IteratorAggregate, \Countable
|
||||
* @see DateRangeFilterIterator
|
||||
* @see DateComparator
|
||||
*/
|
||||
public function date($date)
|
||||
public function date(string|array $dates): static
|
||||
{
|
||||
$this->dates[] = new Comparator\DateComparator($date);
|
||||
foreach ((array) $dates as $date) {
|
||||
$this->dates[] = new Comparator\DateComparator($date);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -155,19 +162,20 @@ class Finder implements \IteratorAggregate, \Countable
|
||||
*
|
||||
* You can use patterns (delimited with / sign), globs or simple strings.
|
||||
*
|
||||
* $finder->name('*.php')
|
||||
* $finder->name('/\.php$/') // same as above
|
||||
* $finder->name('test.php')
|
||||
* $finder->name('*.php')
|
||||
* $finder->name('/\.php$/') // same as above
|
||||
* $finder->name('test.php')
|
||||
* $finder->name(['test.py', 'test.php'])
|
||||
*
|
||||
* @param string $pattern A pattern (a regexp, a glob, or a string)
|
||||
* @param string|string[] $patterns A pattern (a regexp, a glob, or a string) or an array of patterns
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @see FilenameFilterIterator
|
||||
*/
|
||||
public function name($pattern)
|
||||
public function name(string|array $patterns): static
|
||||
{
|
||||
$this->names[] = $pattern;
|
||||
$this->names = array_merge($this->names, (array) $patterns);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -175,15 +183,15 @@ class Finder implements \IteratorAggregate, \Countable
|
||||
/**
|
||||
* Adds rules that files must not match.
|
||||
*
|
||||
* @param string $pattern A pattern (a regexp, a glob, or a string)
|
||||
* @param string|string[] $patterns A pattern (a regexp, a glob, or a string) or an array of patterns
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @see FilenameFilterIterator
|
||||
*/
|
||||
public function notName($pattern)
|
||||
public function notName(string|array $patterns): static
|
||||
{
|
||||
$this->notNames[] = $pattern;
|
||||
$this->notNames = array_merge($this->notNames, (array) $patterns);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -193,18 +201,19 @@ class Finder implements \IteratorAggregate, \Countable
|
||||
*
|
||||
* Strings or PCRE patterns can be used:
|
||||
*
|
||||
* $finder->contains('Lorem ipsum')
|
||||
* $finder->contains('/Lorem ipsum/i')
|
||||
* $finder->contains('Lorem ipsum')
|
||||
* $finder->contains('/Lorem ipsum/i')
|
||||
* $finder->contains(['dolor', '/ipsum/i'])
|
||||
*
|
||||
* @param string $pattern A pattern (string or regexp)
|
||||
* @param string|string[] $patterns A pattern (string or regexp) or an array of patterns
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @see FilecontentFilterIterator
|
||||
*/
|
||||
public function contains($pattern)
|
||||
public function contains(string|array $patterns): static
|
||||
{
|
||||
$this->contains[] = $pattern;
|
||||
$this->contains = array_merge($this->contains, (array) $patterns);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -214,18 +223,19 @@ class Finder implements \IteratorAggregate, \Countable
|
||||
*
|
||||
* Strings or PCRE patterns can be used:
|
||||
*
|
||||
* $finder->notContains('Lorem ipsum')
|
||||
* $finder->notContains('/Lorem ipsum/i')
|
||||
* $finder->notContains('Lorem ipsum')
|
||||
* $finder->notContains('/Lorem ipsum/i')
|
||||
* $finder->notContains(['lorem', '/dolor/i'])
|
||||
*
|
||||
* @param string $pattern A pattern (string or regexp)
|
||||
* @param string|string[] $patterns A pattern (string or regexp) or an array of patterns
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @see FilecontentFilterIterator
|
||||
*/
|
||||
public function notContains($pattern)
|
||||
public function notContains(string|array $patterns): static
|
||||
{
|
||||
$this->notContains[] = $pattern;
|
||||
$this->notContains = array_merge($this->notContains, (array) $patterns);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -235,20 +245,21 @@ class Finder implements \IteratorAggregate, \Countable
|
||||
*
|
||||
* You can use patterns (delimited with / sign) or simple strings.
|
||||
*
|
||||
* $finder->path('some/special/dir')
|
||||
* $finder->path('/some\/special\/dir/') // same as above
|
||||
* $finder->path('some/special/dir')
|
||||
* $finder->path('/some\/special\/dir/') // same as above
|
||||
* $finder->path(['some dir', 'another/dir'])
|
||||
*
|
||||
* Use only / as dirname separator.
|
||||
*
|
||||
* @param string $pattern A pattern (a regexp or a string)
|
||||
* @param string|string[] $patterns A pattern (a regexp or a string) or an array of patterns
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @see FilenameFilterIterator
|
||||
*/
|
||||
public function path($pattern)
|
||||
public function path(string|array $patterns): static
|
||||
{
|
||||
$this->paths[] = $pattern;
|
||||
$this->paths = array_merge($this->paths, (array) $patterns);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -258,20 +269,21 @@ class Finder implements \IteratorAggregate, \Countable
|
||||
*
|
||||
* You can use patterns (delimited with / sign) or simple strings.
|
||||
*
|
||||
* $finder->notPath('some/special/dir')
|
||||
* $finder->notPath('/some\/special\/dir/') // same as above
|
||||
* $finder->notPath('some/special/dir')
|
||||
* $finder->notPath('/some\/special\/dir/') // same as above
|
||||
* $finder->notPath(['some/file.txt', 'another/file.log'])
|
||||
*
|
||||
* Use only / as dirname separator.
|
||||
*
|
||||
* @param string $pattern A pattern (a regexp or a string)
|
||||
* @param string|string[] $patterns A pattern (a regexp or a string) or an array of patterns
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @see FilenameFilterIterator
|
||||
*/
|
||||
public function notPath($pattern)
|
||||
public function notPath(string|array $patterns): static
|
||||
{
|
||||
$this->notPaths[] = $pattern;
|
||||
$this->notPaths = array_merge($this->notPaths, (array) $patterns);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -279,20 +291,23 @@ class Finder implements \IteratorAggregate, \Countable
|
||||
/**
|
||||
* Adds tests for file sizes.
|
||||
*
|
||||
* $finder->size('> 10K');
|
||||
* $finder->size('<= 1Ki');
|
||||
* $finder->size(4);
|
||||
* $finder->size('> 10K');
|
||||
* $finder->size('<= 1Ki');
|
||||
* $finder->size(4);
|
||||
* $finder->size(['> 10K', '< 20K'])
|
||||
*
|
||||
* @param string|int $size A size range string or an integer
|
||||
* @param string|int|string[]|int[] $sizes A size range string or an integer or an array of size ranges
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @see SizeRangeFilterIterator
|
||||
* @see NumberComparator
|
||||
*/
|
||||
public function size($size)
|
||||
public function size(string|int|array $sizes): static
|
||||
{
|
||||
$this->sizes[] = new Comparator\NumberComparator($size);
|
||||
foreach ((array) $sizes as $size) {
|
||||
$this->sizes[] = new Comparator\NumberComparator($size);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -300,13 +315,17 @@ class Finder implements \IteratorAggregate, \Countable
|
||||
/**
|
||||
* Excludes directories.
|
||||
*
|
||||
* Directories passed as argument must be relative to the ones defined with the `in()` method. For example:
|
||||
*
|
||||
* $finder->in(__DIR__)->exclude('ruby');
|
||||
*
|
||||
* @param string|array $dirs A directory path or an array of directories
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @see ExcludeDirectoryFilterIterator
|
||||
*/
|
||||
public function exclude($dirs)
|
||||
public function exclude(string|array $dirs): static
|
||||
{
|
||||
$this->exclude = array_merge($this->exclude, (array) $dirs);
|
||||
|
||||
@@ -316,13 +335,13 @@ class Finder implements \IteratorAggregate, \Countable
|
||||
/**
|
||||
* Excludes "hidden" directories and files (starting with a dot).
|
||||
*
|
||||
* @param bool $ignoreDotFiles Whether to exclude "hidden" files or not
|
||||
* This option is enabled by default.
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @see ExcludeDirectoryFilterIterator
|
||||
*/
|
||||
public function ignoreDotFiles($ignoreDotFiles)
|
||||
public function ignoreDotFiles(bool $ignoreDotFiles): static
|
||||
{
|
||||
if ($ignoreDotFiles) {
|
||||
$this->ignore |= static::IGNORE_DOT_FILES;
|
||||
@@ -336,13 +355,13 @@ class Finder implements \IteratorAggregate, \Countable
|
||||
/**
|
||||
* Forces the finder to ignore version control directories.
|
||||
*
|
||||
* @param bool $ignoreVCS Whether to exclude VCS files or not
|
||||
* This option is enabled by default.
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @see ExcludeDirectoryFilterIterator
|
||||
*/
|
||||
public function ignoreVCS($ignoreVCS)
|
||||
public function ignoreVCS(bool $ignoreVCS): static
|
||||
{
|
||||
if ($ignoreVCS) {
|
||||
$this->ignore |= static::IGNORE_VCS_FILES;
|
||||
@@ -353,6 +372,24 @@ class Finder implements \IteratorAggregate, \Countable
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Forces Finder to obey .gitignore and ignore files based on rules listed there.
|
||||
*
|
||||
* This option is disabled by default.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function ignoreVCSIgnored(bool $ignoreVCSIgnored): static
|
||||
{
|
||||
if ($ignoreVCSIgnored) {
|
||||
$this->ignore |= static::IGNORE_VCS_IGNORED_FILES;
|
||||
} else {
|
||||
$this->ignore &= ~static::IGNORE_VCS_IGNORED_FILES;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds VCS patterns.
|
||||
*
|
||||
@@ -360,7 +397,7 @@ class Finder implements \IteratorAggregate, \Countable
|
||||
*
|
||||
* @param string|string[] $pattern VCS patterns to ignore
|
||||
*/
|
||||
public static function addVCSPattern($pattern)
|
||||
public static function addVCSPattern(string|array $pattern)
|
||||
{
|
||||
foreach ((array) $pattern as $p) {
|
||||
self::$vcsPatterns[] = $p;
|
||||
@@ -376,13 +413,11 @@ class Finder implements \IteratorAggregate, \Countable
|
||||
*
|
||||
* This can be slow as all the matching files and directories must be retrieved for comparison.
|
||||
*
|
||||
* @param \Closure $closure An anonymous function
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @see SortableIterator
|
||||
*/
|
||||
public function sort(\Closure $closure)
|
||||
public function sort(\Closure $closure): static
|
||||
{
|
||||
$this->sort = $closure;
|
||||
|
||||
@@ -398,9 +433,9 @@ class Finder implements \IteratorAggregate, \Countable
|
||||
*
|
||||
* @see SortableIterator
|
||||
*/
|
||||
public function sortByName()
|
||||
public function sortByName(bool $useNaturalSort = false): static
|
||||
{
|
||||
$this->sort = Iterator\SortableIterator::SORT_BY_NAME;
|
||||
$this->sort = $useNaturalSort ? Iterator\SortableIterator::SORT_BY_NAME_NATURAL : Iterator\SortableIterator::SORT_BY_NAME;
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -414,7 +449,7 @@ class Finder implements \IteratorAggregate, \Countable
|
||||
*
|
||||
* @see SortableIterator
|
||||
*/
|
||||
public function sortByType()
|
||||
public function sortByType(): static
|
||||
{
|
||||
$this->sort = Iterator\SortableIterator::SORT_BY_TYPE;
|
||||
|
||||
@@ -432,13 +467,25 @@ class Finder implements \IteratorAggregate, \Countable
|
||||
*
|
||||
* @see SortableIterator
|
||||
*/
|
||||
public function sortByAccessedTime()
|
||||
public function sortByAccessedTime(): static
|
||||
{
|
||||
$this->sort = Iterator\SortableIterator::SORT_BY_ACCESSED_TIME;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverses the sorting.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function reverseSorting(): static
|
||||
{
|
||||
$this->reverseSorting = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts files and directories by the last inode changed time.
|
||||
*
|
||||
@@ -452,7 +499,7 @@ class Finder implements \IteratorAggregate, \Countable
|
||||
*
|
||||
* @see SortableIterator
|
||||
*/
|
||||
public function sortByChangedTime()
|
||||
public function sortByChangedTime(): static
|
||||
{
|
||||
$this->sort = Iterator\SortableIterator::SORT_BY_CHANGED_TIME;
|
||||
|
||||
@@ -470,7 +517,7 @@ class Finder implements \IteratorAggregate, \Countable
|
||||
*
|
||||
* @see SortableIterator
|
||||
*/
|
||||
public function sortByModifiedTime()
|
||||
public function sortByModifiedTime(): static
|
||||
{
|
||||
$this->sort = Iterator\SortableIterator::SORT_BY_MODIFIED_TIME;
|
||||
|
||||
@@ -483,13 +530,11 @@ class Finder implements \IteratorAggregate, \Countable
|
||||
* The anonymous function receives a \SplFileInfo and must return false
|
||||
* to remove files.
|
||||
*
|
||||
* @param \Closure $closure An anonymous function
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @see CustomFilterIterator
|
||||
*/
|
||||
public function filter(\Closure $closure)
|
||||
public function filter(\Closure $closure): static
|
||||
{
|
||||
$this->filters[] = $closure;
|
||||
|
||||
@@ -501,7 +546,7 @@ class Finder implements \IteratorAggregate, \Countable
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function followLinks()
|
||||
public function followLinks(): static
|
||||
{
|
||||
$this->followLinks = true;
|
||||
|
||||
@@ -513,13 +558,11 @@ class Finder implements \IteratorAggregate, \Countable
|
||||
*
|
||||
* By default, scanning unreadable directories content throws an AccessDeniedException.
|
||||
*
|
||||
* @param bool $ignore
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function ignoreUnreadableDirs($ignore = true)
|
||||
public function ignoreUnreadableDirs(bool $ignore = true): static
|
||||
{
|
||||
$this->ignoreUnreadableDirs = (bool) $ignore;
|
||||
$this->ignoreUnreadableDirs = $ignore;
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -527,27 +570,28 @@ class Finder implements \IteratorAggregate, \Countable
|
||||
/**
|
||||
* Searches files and directories which match defined rules.
|
||||
*
|
||||
* @param string|array $dirs A directory path or an array of directories
|
||||
* @param string|string[] $dirs A directory path or an array of directories
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @throws \InvalidArgumentException if one of the directories does not exist
|
||||
* @throws DirectoryNotFoundException if one of the directories does not exist
|
||||
*/
|
||||
public function in($dirs)
|
||||
public function in(string|array $dirs): static
|
||||
{
|
||||
$resolvedDirs = array();
|
||||
$resolvedDirs = [];
|
||||
|
||||
foreach ((array) $dirs as $dir) {
|
||||
if (is_dir($dir)) {
|
||||
$resolvedDirs[] = $dir;
|
||||
} elseif ($glob = glob($dir, (defined('GLOB_BRACE') ? GLOB_BRACE : 0) | GLOB_ONLYDIR)) {
|
||||
$resolvedDirs = array_merge($resolvedDirs, $glob);
|
||||
$resolvedDirs[] = [$this->normalizeDir($dir)];
|
||||
} elseif ($glob = glob($dir, (\defined('GLOB_BRACE') ? \GLOB_BRACE : 0) | \GLOB_ONLYDIR | \GLOB_NOSORT)) {
|
||||
sort($glob);
|
||||
$resolvedDirs[] = array_map([$this, 'normalizeDir'], $glob);
|
||||
} else {
|
||||
throw new \InvalidArgumentException(sprintf('The "%s" directory does not exist.', $dir));
|
||||
throw new DirectoryNotFoundException(sprintf('The "%s" directory does not exist.', $dir));
|
||||
}
|
||||
}
|
||||
|
||||
$this->dirs = array_merge($this->dirs, $resolvedDirs);
|
||||
$this->dirs = array_merge($this->dirs, ...$resolvedDirs);
|
||||
|
||||
return $this;
|
||||
}
|
||||
@@ -557,29 +601,41 @@ class Finder implements \IteratorAggregate, \Countable
|
||||
*
|
||||
* This method implements the IteratorAggregate interface.
|
||||
*
|
||||
* @return \Iterator|SplFileInfo[] An iterator
|
||||
* @return \Iterator<string, SplFileInfo>
|
||||
*
|
||||
* @throws \LogicException if the in() method has not been called
|
||||
*/
|
||||
public function getIterator()
|
||||
public function getIterator(): \Iterator
|
||||
{
|
||||
if (0 === count($this->dirs) && 0 === count($this->iterators)) {
|
||||
if (0 === \count($this->dirs) && 0 === \count($this->iterators)) {
|
||||
throw new \LogicException('You must call one of in() or append() methods before iterating over a Finder.');
|
||||
}
|
||||
|
||||
if (1 === count($this->dirs) && 0 === count($this->iterators)) {
|
||||
return $this->searchInDirectory($this->dirs[0]);
|
||||
if (1 === \count($this->dirs) && 0 === \count($this->iterators)) {
|
||||
$iterator = $this->searchInDirectory($this->dirs[0]);
|
||||
|
||||
if ($this->sort || $this->reverseSorting) {
|
||||
$iterator = (new Iterator\SortableIterator($iterator, $this->sort, $this->reverseSorting))->getIterator();
|
||||
}
|
||||
|
||||
return $iterator;
|
||||
}
|
||||
|
||||
$iterator = new \AppendIterator();
|
||||
foreach ($this->dirs as $dir) {
|
||||
$iterator->append($this->searchInDirectory($dir));
|
||||
$iterator->append(new \IteratorIterator(new LazyIterator(function () use ($dir) {
|
||||
return $this->searchInDirectory($dir);
|
||||
})));
|
||||
}
|
||||
|
||||
foreach ($this->iterators as $it) {
|
||||
$iterator->append($it);
|
||||
}
|
||||
|
||||
if ($this->sort || $this->reverseSorting) {
|
||||
$iterator = (new Iterator\SortableIterator($iterator, $this->sort, $this->reverseSorting))->getIterator();
|
||||
}
|
||||
|
||||
return $iterator;
|
||||
}
|
||||
|
||||
@@ -588,22 +644,21 @@ class Finder implements \IteratorAggregate, \Countable
|
||||
*
|
||||
* The set can be another Finder, an Iterator, an IteratorAggregate, or even a plain array.
|
||||
*
|
||||
* @param mixed $iterator
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @throws \InvalidArgumentException When the given argument is not iterable.
|
||||
* @throws \InvalidArgumentException when the given argument is not iterable
|
||||
*/
|
||||
public function append($iterator)
|
||||
public function append(iterable $iterator): static
|
||||
{
|
||||
if ($iterator instanceof \IteratorAggregate) {
|
||||
$this->iterators[] = $iterator->getIterator();
|
||||
} elseif ($iterator instanceof \Iterator) {
|
||||
$this->iterators[] = $iterator;
|
||||
} elseif ($iterator instanceof \Traversable || is_array($iterator)) {
|
||||
} elseif (is_iterable($iterator)) {
|
||||
$it = new \ArrayIterator();
|
||||
foreach ($iterator as $file) {
|
||||
$it->append($file instanceof \SplFileInfo ? $file : new \SplFileInfo($file));
|
||||
$file = $file instanceof \SplFileInfo ? $file : new \SplFileInfo($file);
|
||||
$it[$file->getPathname()] = $file;
|
||||
}
|
||||
$this->iterators[] = $it;
|
||||
} else {
|
||||
@@ -614,32 +669,40 @@ class Finder implements \IteratorAggregate, \Countable
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts all the results collected by the iterators.
|
||||
*
|
||||
* @return int
|
||||
* Check if any results were found.
|
||||
*/
|
||||
public function count()
|
||||
public function hasResults(): bool
|
||||
{
|
||||
foreach ($this->getIterator() as $_) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts all the results collected by the iterators.
|
||||
*/
|
||||
public function count(): int
|
||||
{
|
||||
return iterator_count($this->getIterator());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $dir
|
||||
*
|
||||
* @return \Iterator
|
||||
*/
|
||||
private function searchInDirectory($dir)
|
||||
private function searchInDirectory(string $dir): \Iterator
|
||||
{
|
||||
$exclude = $this->exclude;
|
||||
$notPaths = $this->notPaths;
|
||||
|
||||
if (static::IGNORE_VCS_FILES === (static::IGNORE_VCS_FILES & $this->ignore)) {
|
||||
$this->exclude = array_merge($this->exclude, self::$vcsPatterns);
|
||||
$exclude = array_merge($exclude, self::$vcsPatterns);
|
||||
}
|
||||
|
||||
if (static::IGNORE_DOT_FILES === (static::IGNORE_DOT_FILES & $this->ignore)) {
|
||||
$this->notPaths[] = '#(^|/)\..+(/|$)#';
|
||||
$notPaths[] = '#(^|/)\..+(/|$)#';
|
||||
}
|
||||
|
||||
$minDepth = 0;
|
||||
$maxDepth = PHP_INT_MAX;
|
||||
$maxDepth = \PHP_INT_MAX;
|
||||
|
||||
foreach ($this->depths as $comparator) {
|
||||
switch ($comparator->getOperator()) {
|
||||
@@ -668,13 +731,13 @@ class Finder implements \IteratorAggregate, \Countable
|
||||
|
||||
$iterator = new Iterator\RecursiveDirectoryIterator($dir, $flags, $this->ignoreUnreadableDirs);
|
||||
|
||||
if ($this->exclude) {
|
||||
$iterator = new Iterator\ExcludeDirectoryFilterIterator($iterator, $this->exclude);
|
||||
if ($exclude) {
|
||||
$iterator = new Iterator\ExcludeDirectoryFilterIterator($iterator, $exclude);
|
||||
}
|
||||
|
||||
$iterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::SELF_FIRST);
|
||||
|
||||
if ($minDepth > 0 || $maxDepth < PHP_INT_MAX) {
|
||||
if ($minDepth > 0 || $maxDepth < \PHP_INT_MAX) {
|
||||
$iterator = new Iterator\DepthRangeFilterIterator($iterator, $minDepth, $maxDepth);
|
||||
}
|
||||
|
||||
@@ -702,15 +765,34 @@ class Finder implements \IteratorAggregate, \Countable
|
||||
$iterator = new Iterator\CustomFilterIterator($iterator, $this->filters);
|
||||
}
|
||||
|
||||
if ($this->paths || $this->notPaths) {
|
||||
$iterator = new Iterator\PathFilterIterator($iterator, $this->paths, $this->notPaths);
|
||||
if ($this->paths || $notPaths) {
|
||||
$iterator = new Iterator\PathFilterIterator($iterator, $this->paths, $notPaths);
|
||||
}
|
||||
|
||||
if ($this->sort) {
|
||||
$iteratorAggregate = new Iterator\SortableIterator($iterator, $this->sort);
|
||||
$iterator = $iteratorAggregate->getIterator();
|
||||
if (static::IGNORE_VCS_IGNORED_FILES === (static::IGNORE_VCS_IGNORED_FILES & $this->ignore)) {
|
||||
$iterator = new Iterator\VcsIgnoredFilterIterator($iterator, $dir);
|
||||
}
|
||||
|
||||
return $iterator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalizes given directory names by removing trailing slashes.
|
||||
*
|
||||
* Excluding: (s)ftp:// or ssh2.(s)ftp:// wrapper
|
||||
*/
|
||||
private function normalizeDir(string $dir): string
|
||||
{
|
||||
if ('/' === $dir) {
|
||||
return $dir;
|
||||
}
|
||||
|
||||
$dir = rtrim($dir, '/'.\DIRECTORY_SEPARATOR);
|
||||
|
||||
if (preg_match('#^(ssh2\.)?s?ftp://#', $dir)) {
|
||||
$dir .= '/';
|
||||
}
|
||||
|
||||
return $dir;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user