Skip to content

Commit

Permalink
Take hirarchy into account
Browse files Browse the repository at this point in the history
  • Loading branch information
mamazu committed Mar 9, 2024
1 parent e1fe94f commit ad66886
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 19 deletions.
44 changes: 37 additions & 7 deletions lib/Filesystem/Domain/FileList.php
Expand Up @@ -86,19 +86,49 @@ public function byExtensions(array $extensions): self
}

/**
* @param string[] $globPatterns
* @param string[] $includePatterns
* @param string[] $excludePatterns
*/
public function excludePatterns(array $globPatterns): self
{
return $this->filter(function (SplFileInfo $info) use ($globPatterns) {
foreach ($globPatterns as $pattern) {
if (Glob::match($info->getPathname(), $pattern)) {
return false;
public function includeAndExclude(array $includePatterns, array $excludePatterns): self
{
// Building map of include paths that are sub paths of excludes so that we can include them again
$includedExcludes = [];
foreach($excludePatterns as $excludePattern) {
foreach($includePatterns as $includePattern) {
if (Glob::match($includePattern, $excludePattern)) {
$includedExcludes[$excludePattern][] = $includePattern;
}
}
}

$this->includePatterns($includePatterns);

return $this->filter(function (SplFileInfo $info) use ($excludePatterns, $includedExcludes) {
foreach ($excludePatterns as $pattern) {
if (!Glob::match($info->getPathname(), $pattern)) {
continue;
}

$includePatterns = $includedExcludes[$pattern] ?? [];
foreach($includePatterns as $includePattern) {
if (Glob::match($info->getPathname(), $includePattern)) {
return true;
}
}
return false;
}

return true;
});

}

/**
* @param string[] $globPatterns
*/
public function excludePatterns(array $globPatterns): self
{
return $this->includeAndExclude([], $globPatterns);
}

/**
Expand Down
16 changes: 16 additions & 0 deletions lib/Filesystem/Tests/Unit/Domain/FileListTest.php
Expand Up @@ -140,6 +140,22 @@ public function testIncldesFilesMatchingPatterns(): void
]));
}

public function testIncludesExcludePatterns(): void
{

$list = FileList::fromFilePaths([
FilePath::fromString('/vendor/cache/important/bartest.php'),
FilePath::fromString('/vendor/cache/important/footest.php'),
FilePath::fromString('/vendor/cache/bar.php'),
FilePath::fromString('/vendor/cache/foo.php'),
])->includeAndExclude(
includePatterns: [ '/src/**/*', '/vendor/cache/important/**/*'],
excludePatterns: [ '/vendor/**/*' ],
);

self::assertCount(2, $list);
}

public function testContainingString(): void
{
$this->workspace()->put('one', 'one two three');
Expand Down
13 changes: 4 additions & 9 deletions lib/Indexer/Adapter/Filesystem/FilesystemFileListProvider.php
Expand Up @@ -30,15 +30,10 @@ public function provideFileList(Index $index, ?string $subPath = null): FileList
return FileList::fromSingleFilePath($subPath);
}

$files = $this->filesystem->fileList()->byExtensions($this->supportedExtensions);

if ($this->includePatterns) {
$files = $files->includePatterns($this->includePatterns);
}

if ($this->excludePatterns) {
$files = $files->excludePatterns($this->excludePatterns);
}
$files = $this->filesystem->fileList()
->byExtensions($this->supportedExtensions)
->includeAndExclude($this->includePatterns, $this->excludePatterns)
;

if ($subPath) {
$files = $files->within(FilePath::fromString($subPath));
Expand Down
4 changes: 1 addition & 3 deletions lib/Indexer/Extension/IndexerExtension.php
Expand Up @@ -224,9 +224,7 @@ private function registerModel(ContainerBuilder $container): void
->setIncludePatterns($container->get(self::SERVICE_INDEXER_INCLUDE_PATTERNS))
/** @phpstan-ignore-next-line */
->setSupportedExtensions($container->parameter(self::PARAM_SUPPORTED_EXTENSIONS)->value())
->setFollowSymlinks(
(bool) $container->getParameter(self::PARAM_INDEXER_FOLLOW_SYMLINKS),
)
->setFollowSymlinks($container->parameter(self::PARAM_INDEXER_FOLLOW_SYMLINKS)->bool())
->setStubPaths($container->getParameter(self::PARAM_STUB_PATHS));
});

Expand Down

0 comments on commit ad66886

Please sign in to comment.