Skip to content

Commit eadaa21

Browse files
authored
phpactorgh-2611: Introduce decorator to expand PHAR files for indexer (phpactor#2634)
1 parent 6478aa0 commit eadaa21

File tree

5 files changed

+58
-38
lines changed

5 files changed

+58
-38
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Changelog
55

66
Features:
77

8-
- PHAR Indexing #2412 @dantleech
8+
- PHAR Indexing #2412 #2611 @dantleech
99

1010
Improvements:
1111

lib/Indexer/Adapter/Filesystem/FilesystemFileListProvider.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Phpactor\Filesystem\Domain\FilePath;
66
use Phpactor\Filesystem\Domain\Filesystem;
7+
use Phpactor\Indexer\Adapter\Php\FileInfoPharExpandingIterator;
78
use Phpactor\Indexer\Model\FileList;
89
use Phpactor\Indexer\Model\FileListProvider;
910
use SplFileInfo;
@@ -45,6 +46,11 @@ public function provideFileList(Index $index, ?string $subPath = null): FileList
4546
});
4647
}
4748

48-
return FileList::fromInfoIterator($files->getSplFileInfoIterator());
49+
return FileList::fromInfoIterator(
50+
new FileInfoPharExpandingIterator(
51+
$files->getSplFileInfoIterator(),
52+
$this->supportedExtensions,
53+
)
54+
);
4955
}
5056
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Phpactor\Indexer\Adapter\Php;
4+
5+
use Iterator;
6+
use IteratorAggregate;
7+
use Phar;
8+
use RecursiveIteratorIterator;
9+
use SplFileInfo;
10+
use Traversable;
11+
use UnexpectedValueException;
12+
13+
/**
14+
* @implements IteratorAggregate<SplFileInfo>
15+
*/
16+
class FileInfoPharExpandingIterator implements IteratorAggregate
17+
{
18+
/**
19+
* @param Iterator<SplFileInfo> $innerIterator
20+
* @param list<string> $supportedExtensions
21+
*/
22+
public function __construct(private Iterator $innerIterator, private array $supportedExtensions = ['php'])
23+
{
24+
}
25+
26+
public function getIterator(): Traversable
27+
{
28+
foreach ($this->innerIterator as $fileInfo) {
29+
if ($fileInfo->getExtension() !== 'phar') {
30+
yield $fileInfo;
31+
}
32+
try {
33+
$phar = new Phar($fileInfo->getPathname());
34+
} catch (UnexpectedValueException) {
35+
continue;
36+
}
37+
$iterator = new RecursiveIteratorIterator($phar);
38+
foreach ($iterator as $fileInfo) {
39+
assert($fileInfo instanceof SplFileInfo);
40+
if (!in_array($fileInfo->getExtension(), $this->supportedExtensions)) {
41+
continue;
42+
}
43+
yield $fileInfo;
44+
}
45+
}
46+
}
47+
}

lib/Indexer/Model/FileList.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Iterator;
88
use IteratorAggregate;
99
use SplFileInfo;
10+
use Traversable;
1011

1112
/**
1213
* @implements \IteratorAggregate<SplFileInfo>
@@ -38,9 +39,9 @@ public static function empty(): self
3839
}
3940

4041
/**
41-
* @param Iterator<SplFileInfo> $splFileInfos
42+
* @param Traversable<SplFileInfo> $splFileInfos
4243
*/
43-
public static function fromInfoIterator(Iterator $splFileInfos): self
44+
public static function fromInfoIterator(Traversable $splFileInfos): self
4445
{
4546
return new self($splFileInfos);
4647
}

lib/Indexer/Model/IndexJob.php

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,12 @@ public function __construct(private IndexBuilder $indexBuilder, private FileList
2121
*/
2222
public function generator(): Generator
2323
{
24-
2524
foreach ($this->fileList as $fileInfo) {
2625
assert($fileInfo instanceof SplFileInfo);
2726
if ($fileInfo->isLink()) {
2827
continue;
2928
}
3029

31-
// TODO: could refactor this to iterate the PHAR in the file list provider.
32-
if ($fileInfo->getExtension() === 'phar') {
33-
try {
34-
$phar = new Phar($fileInfo->getPathname());
35-
} catch (UnexpectedValueException $e) {
36-
continue;
37-
}
38-
iterator_to_array($this->indexPharFile($phar));
39-
yield $fileInfo->getPathname();
40-
continue;
41-
}
42-
4330
$contents = @file_get_contents($fileInfo->getPathname());
4431

4532
if (false === $contents) {
@@ -63,25 +50,4 @@ public function size(): int
6350
{
6451
return $this->fileList->count();
6552
}
66-
/**
67-
* @return Generator<string>
68-
*/
69-
private function indexPharFile(Phar $phar): Generator
70-
{
71-
$iterator = new RecursiveIteratorIterator($phar);
72-
/** @var PharFileInfo $file */
73-
foreach ($iterator as $file) {
74-
if (!$file->isFile()) {
75-
continue;
76-
}
77-
if ($file->getExtension() !== 'php') {
78-
continue;
79-
}
80-
81-
$this->indexBuilder->index(
82-
TextDocumentBuilder::fromUri($file->getPathname())->build()
83-
);
84-
yield $file->getPathname();
85-
}
86-
}
8753
}

0 commit comments

Comments
 (0)