Skip to content

Commit

Permalink
gh-2611: Introduce decorator to expand PHAR files for indexer (#2634)
Browse files Browse the repository at this point in the history
  • Loading branch information
dantleech committed Apr 6, 2024
1 parent 6478aa0 commit eadaa21
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 38 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -5,7 +5,7 @@ Changelog

Features:

- PHAR Indexing #2412 @dantleech
- PHAR Indexing #2412 #2611 @dantleech

Improvements:

Expand Down
Expand Up @@ -4,6 +4,7 @@

use Phpactor\Filesystem\Domain\FilePath;
use Phpactor\Filesystem\Domain\Filesystem;
use Phpactor\Indexer\Adapter\Php\FileInfoPharExpandingIterator;
use Phpactor\Indexer\Model\FileList;
use Phpactor\Indexer\Model\FileListProvider;
use SplFileInfo;
Expand Down Expand Up @@ -45,6 +46,11 @@ public function provideFileList(Index $index, ?string $subPath = null): FileList
});
}

return FileList::fromInfoIterator($files->getSplFileInfoIterator());
return FileList::fromInfoIterator(
new FileInfoPharExpandingIterator(
$files->getSplFileInfoIterator(),
$this->supportedExtensions,
)
);
}
}
47 changes: 47 additions & 0 deletions lib/Indexer/Adapter/Php/FileInfoPharExpandingIterator.php
@@ -0,0 +1,47 @@
<?php

namespace Phpactor\Indexer\Adapter\Php;

use Iterator;
use IteratorAggregate;
use Phar;
use RecursiveIteratorIterator;
use SplFileInfo;
use Traversable;
use UnexpectedValueException;

/**
* @implements IteratorAggregate<SplFileInfo>
*/
class FileInfoPharExpandingIterator implements IteratorAggregate
{
/**
* @param Iterator<SplFileInfo> $innerIterator
* @param list<string> $supportedExtensions
*/
public function __construct(private Iterator $innerIterator, private array $supportedExtensions = ['php'])
{
}

public function getIterator(): Traversable
{
foreach ($this->innerIterator as $fileInfo) {
if ($fileInfo->getExtension() !== 'phar') {
yield $fileInfo;
}
try {
$phar = new Phar($fileInfo->getPathname());
} catch (UnexpectedValueException) {
continue;
}
$iterator = new RecursiveIteratorIterator($phar);
foreach ($iterator as $fileInfo) {
assert($fileInfo instanceof SplFileInfo);
if (!in_array($fileInfo->getExtension(), $this->supportedExtensions)) {
continue;
}
yield $fileInfo;
}
}
}
}
5 changes: 3 additions & 2 deletions lib/Indexer/Model/FileList.php
Expand Up @@ -7,6 +7,7 @@
use Iterator;
use IteratorAggregate;
use SplFileInfo;
use Traversable;

/**
* @implements \IteratorAggregate<SplFileInfo>
Expand Down Expand Up @@ -38,9 +39,9 @@ public static function empty(): self
}

/**
* @param Iterator<SplFileInfo> $splFileInfos
* @param Traversable<SplFileInfo> $splFileInfos
*/
public static function fromInfoIterator(Iterator $splFileInfos): self
public static function fromInfoIterator(Traversable $splFileInfos): self
{
return new self($splFileInfos);
}
Expand Down
34 changes: 0 additions & 34 deletions lib/Indexer/Model/IndexJob.php
Expand Up @@ -21,25 +21,12 @@ public function __construct(private IndexBuilder $indexBuilder, private FileList
*/
public function generator(): Generator
{

foreach ($this->fileList as $fileInfo) {
assert($fileInfo instanceof SplFileInfo);
if ($fileInfo->isLink()) {
continue;
}

// TODO: could refactor this to iterate the PHAR in the file list provider.
if ($fileInfo->getExtension() === 'phar') {
try {
$phar = new Phar($fileInfo->getPathname());
} catch (UnexpectedValueException $e) {
continue;
}
iterator_to_array($this->indexPharFile($phar));
yield $fileInfo->getPathname();
continue;
}

$contents = @file_get_contents($fileInfo->getPathname());

if (false === $contents) {
Expand All @@ -63,25 +50,4 @@ public function size(): int
{
return $this->fileList->count();
}
/**
* @return Generator<string>
*/
private function indexPharFile(Phar $phar): Generator
{
$iterator = new RecursiveIteratorIterator($phar);
/** @var PharFileInfo $file */
foreach ($iterator as $file) {
if (!$file->isFile()) {
continue;
}
if ($file->getExtension() !== 'php') {
continue;
}

$this->indexBuilder->index(
TextDocumentBuilder::fromUri($file->getPathname())->build()
);
yield $file->getPathname();
}
}
}

0 comments on commit eadaa21

Please sign in to comment.