Skip to content

Commit

Permalink
Gh 2304: remove finder dep (#2306)
Browse files Browse the repository at this point in the history
* trying to fix

* Remove symfony finder
  • Loading branch information
dantleech committed Jun 18, 2023
1 parent 48564ea commit bdd1dfb
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 19 deletions.
Empty file added lib/Amp/Mod.php
Empty file.
33 changes: 33 additions & 0 deletions lib/Indexer/Adapter/Php/PhpIndexerLister.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Phpactor\Indexer\Adapter\Php;

use Generator;
use Phpactor\Indexer\Model\IndexInfo;
use Phpactor\Indexer\Model\IndexLister;
use SplFileInfo;
use Symfony\Component\Filesystem\Path;

class PhpIndexerLister implements IndexLister
{
public function __construct(private string $indexDirectory)
{
}
public function list(): Generator
{
$indexes = array_filter(array_map(
fn (string|false $name) => $name ? Path::join($this->indexDirectory, $name) : '',
array_filter(
(array)scandir($this->indexDirectory),
fn (string|false $name) => !in_array($name, ['.', '..'])
),
), fn (string $indexPath) => is_dir($indexPath));

foreach ($indexes as $indexPath) {
$info = IndexInfo::fromSplFileInfo(new SplFileInfo($indexPath));
// warmup the size
$info->size();
yield $info;
}
}
}
20 changes: 4 additions & 16 deletions lib/Indexer/Extension/Command/IndexCleanCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;
use Phpactor\Indexer\Model\IndexInfo;
use Phpactor\Indexer\Model\IndexInfos;
use Phpactor\Indexer\Model\IndexLister;
use Phpactor\Indexer\Util\Filesystem as PhpactorFilesystem;
use RuntimeException;
use Symfony\Component\Console\Command\Command;
Expand All @@ -18,14 +19,13 @@
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;

class IndexCleanCommand extends Command
{
public const ARG_INDEX_NAME = 'name';
public const OPT_CLEAN_ALL = 'all';

public function __construct(private string $indexDirectory, private Filesystem $filesystem)
public function __construct(private IndexLister $indexLister, private Filesystem $filesystem)
{
parent::__construct();
}
Expand Down Expand Up @@ -164,21 +164,9 @@ private function getInteractiveAnswer(IndexInfos $infos, InputInterface $input,

private function getIndicies(OutputInterface $output): IndexInfos
{
$finder = (new Finder())
->directories()
->in([$this->indexDirectory])
->sortByName()
->depth('==0')
;
$fileInfos = iterator_to_array($finder);
$progress = new ProgressBar($output, count($fileInfos));

$indexes = [];
foreach ($fileInfos as $fileInfo) {
$info = IndexInfo::fromSplFileInfo($fileInfo);

// warmup the size
$info->size();
$progress = new ProgressBar($output);
foreach ($this->indexLister->list() as $info) {
$indexes[] = $info;
$progress->advance();
}
Expand Down
3 changes: 2 additions & 1 deletion lib/Indexer/Extension/IndexerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Phpactor\Extension\WorseReflection\WorseReflectionExtension;
use Phpactor\Extension\FilePathResolver\FilePathResolverExtension;
use Phpactor\FilePathResolver\PathResolver;
use Phpactor\Indexer\Adapter\Php\PhpIndexerLister;
use Phpactor\Indexer\Adapter\ReferenceFinder\IndexedNameSearcher;
use Phpactor\Indexer\Adapter\ReferenceFinder\Util\ContainerTypeResolver;
use Phpactor\Indexer\Adapter\Worse\IndexerClassSourceLocator;
Expand Down Expand Up @@ -175,7 +176,7 @@ private function registerCommands(ContainerBuilder $container): void
)->resolve($container->parameter(self::PARAM_INDEX_PATH)->string());

$indexPath = dirname($indexPath);
return new IndexCleanCommand($indexPath, new Filesystem());
return new IndexCleanCommand(new PhpIndexerLister($indexPath), new Filesystem());
}, [ ConsoleExtension::TAG_COMMAND => ['name' => 'index:clean']]);

$container->register(IndexQueryCommand::class, function (Container $container) {
Expand Down
4 changes: 2 additions & 2 deletions lib/Indexer/Model/IndexInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Phpactor\Indexer\Model;

use Phpactor\Indexer\Util\Filesystem;
use SplFileInfo;
use Symfony\Component\Filesystem\Path;
use Symfony\Component\Finder\SplFileInfo;

class IndexInfo
{
Expand All @@ -23,7 +23,7 @@ public static function fromSplFileInfo(SplFileInfo $fileInfo): self
{
return new self(
$fileInfo->getRealPath(),
$fileInfo->getRelativePathname(),
basename($fileInfo->getPathname()),
null,
$fileInfo->getCTime(),
(function (SplFileInfo $info) {
Expand Down
13 changes: 13 additions & 0 deletions lib/Indexer/Model/IndexLister.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Phpactor\Indexer\Model;

use Generator;

interface IndexLister
{
/**
* @return Generator<IndexInfo>
*/
public function list(): Generator;
}
31 changes: 31 additions & 0 deletions lib/Indexer/Tests/Adapter/Php/PhpIndexListerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Phpactor\Indexer\Tests\Adapter\Php;

use Phpactor\Indexer\Adapter\Php\PhpIndexerLister;
use Phpactor\Indexer\Tests\IntegrationTestCase;

class PhpIndexListerTest extends IntegrationTestCase
{
protected function setUp(): void
{
$this->workspace()->reset();
}

public function testListsIndexes(): void
{
$this->workspace()->mkdir('index1');
$this->workspace()->mkdir('index2');
$this->workspace()->put('file1.txt', '');

$lister = $this->lister();

$infos = iterator_to_array($lister->list());
self::assertCount(2, $infos);
}

private function lister(): PhpIndexerLister
{
return (new PhpIndexerLister($this->workspace()->path()));
}
}

0 comments on commit bdd1dfb

Please sign in to comment.