New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Include excluded files #2593
Include excluded files #2593
Changes from all commits
be82766
a542e26
d0e2902
abff473
461e42b
6e1bff6
b2c36ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,30 +86,51 @@ public function byExtensions(array $extensions): self | |
} | ||
|
||
/** | ||
* @param string[] $globPatterns | ||
* @param string[] $includePatterns | ||
* @param string[] $excludePatterns | ||
*/ | ||
public function excludePatterns(array $globPatterns): self | ||
public function includeAndExclude(array $includePatterns = [], array $excludePatterns = []): self | ||
{ | ||
return $this->filter(function (SplFileInfo $info) use ($globPatterns) { | ||
foreach ($globPatterns as $pattern) { | ||
if (Glob::match($info->getPathname(), $pattern)) { | ||
return false; | ||
} | ||
$inclusionMap = []; | ||
if ($includePatterns === []) { | ||
$inclusionMap['/**/*'] = true; | ||
} | ||
|
||
foreach ($includePatterns as $includePattern) { | ||
$inclusionMap[$includePattern] = true; | ||
} | ||
foreach ($excludePatterns as $excludePattern) { | ||
$inclusionMap[$excludePattern] = false; | ||
} | ||
|
||
// Sort map by keys so that more specific paths are getting matched first | ||
uksort($inclusionMap, function (string $a, string $b) { | ||
$partsA = explode(DIRECTORY_SEPARATOR, $a); | ||
$partsB = explode(DIRECTORY_SEPARATOR, $b); | ||
$countDiff = count($partsA) <=> count($partsB); | ||
if ($countDiff !== 0) { | ||
// Longer paths should come first | ||
return -$countDiff; | ||
} | ||
|
||
return true; | ||
foreach ($partsA as $i => $pathPartA) { | ||
if ($pathPartA === '**' || $pathPartA === '*') { | ||
return 1; | ||
} | ||
|
||
$compare = strcmp($pathPartA, $partsB[$i]); | ||
if($compare !== 0) { | ||
return $compare; | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you add a commenta about what this is doing? i.e. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What exactly should I comment here. If they are the same then their order doesn't matter. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. explaining what the different branches mean. It's not obvious what There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh yeah wildcards within a path aren't handled. That's a valid point. |
||
// If none of the path segments were different, then they must be equal | ||
return 0; | ||
dantleech marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); | ||
} | ||
|
||
/** | ||
* @param string[] $globPatterns | ||
*/ | ||
public function includePatterns(array $globPatterns): self | ||
{ | ||
return $this->filter(function (SplFileInfo $info) use ($globPatterns) { | ||
foreach ($globPatterns as $pattern) { | ||
if (Glob::match($info->getPathname(), $pattern)) { | ||
return true; | ||
return $this->filter(function (SplFileInfo $info) use ($inclusionMap): bool { | ||
foreach($inclusionMap as $glob => $isIncluded) { | ||
if (Glob::match($info->getPathname(), $glob)) { | ||
return $isIncluded; | ||
} | ||
} | ||
|
||
|
@@ -133,6 +154,9 @@ public function named(string $name): self | |
))); | ||
} | ||
|
||
/** | ||
* @param Closure(SplFileInfo): bool $closure | ||
*/ | ||
public function filter(Closure $closure): self | ||
{ | ||
return new self(new CallbackFilterIterator($this->iterator, $closure)); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
paths with more segments.