diff --git a/src/Console/Command/Indexer.php b/src/Console/Command/Indexer.php index f7cc21e..40d93a3 100644 --- a/src/Console/Command/Indexer.php +++ b/src/Console/Command/Indexer.php @@ -25,7 +25,7 @@ class Indexer extends Command { private IndexerProgressBar $progressBar; private SymfonyStyle $io; - private TypifiedInput $input; + private OutputInterface $output; /** * phpcs:ignore @@ -90,14 +90,15 @@ protected function execute( OutputInterface $output ): int { - $this->input = new TypifiedInput($input); + $typedInput = new TypifiedInput($input); + $this->output = $output; $this->io = new SymfonyStyle($input, $output); $this->progressBar = $this->progressBarFactory->create($output); - $paths = $this->input->getArrayArgument('paths'); + $paths = $typedInput->getArrayArgument('paths'); $cleanupThreshold = empty($paths) - ? $this->input->getIntOption('cleanup-threshold') + ? $typedInput->getIntOption('cleanup-threshold') : 0; if (empty($paths)) { @@ -108,18 +109,18 @@ protected function execute( } $parameter = new IndexerParameter( - $this->input->getStringArgument('solr-core'), + $typedInput->getStringArgument('solr-core'), $cleanupThreshold, - $this->input->getIntOption('chunk-size'), + $typedInput->getIntOption('chunk-size'), $paths ); $this->solrIndexerBuilder - ->resourceDir($this->input->getStringArgument('resource-dir')) + ->resourceDir($typedInput->getStringArgument('resource-dir')) ->progressBar($this->progressBar) ->documentEnricherList($this->documentEnricherList) ->solrConnectionUrl( - $this->input->getStringArgument('solr-connection-url') + $typedInput->getStringArgument('solr-connection-url') ); $indexer = $this->solrIndexerBuilder->build(); @@ -133,7 +134,11 @@ protected function execute( protected function errorReport(): void { foreach ($this->progressBar->getErrors() as $error) { - $this->io->error($error->getMessage()); + if ($this->io->isVerbose() && $this->getApplication() !== null) { + $this->getApplication()->renderThrowable($error, $this->output); + } else { + $this->io->error($error->getMessage()); + } } } } diff --git a/src/Console/Command/ResourceBaseLocatorBuilder.php b/src/Console/Command/ResourceBaseLocatorBuilder.php new file mode 100644 index 0000000..f6c3c7b --- /dev/null +++ b/src/Console/Command/ResourceBaseLocatorBuilder.php @@ -0,0 +1,24 @@ +resourceDir = $resourceDir; @@ -65,14 +68,8 @@ public function solrConnectionUrl( public function build(): SolrIndexer { - $subDirectory = null; - if (is_dir($this->resourceDir . '/objects')) { - $subDirectory = 'objects'; - } - $_SERVER['RESOURCE_ROOT'] = $this->resourceDir; - $resourceBaseLocator = new ServerVarResourceBaseLocator( - 'RESOURCE_ROOT', - $subDirectory + $resourceBaseLocator = $this->resourceBaseLocatorBuilder->build( + $this->resourceDir ); $finder = new LocationFinder($resourceBaseLocator); $resourceLoader = new SiteKitLoader($resourceBaseLocator); diff --git a/src/Console/Command/SolrMoreLikeThisBuilder.php b/src/Console/Command/SolrMoreLikeThisBuilder.php index 8f72a7d..68bd995 100644 --- a/src/Console/Command/SolrMoreLikeThisBuilder.php +++ b/src/Console/Command/SolrMoreLikeThisBuilder.php @@ -5,19 +5,26 @@ namespace Atoolo\Search\Console\Command; use Atoolo\Resource\Loader\SiteKitLoader; -use Atoolo\Resource\Loader\StaticResourceBaseLocator; use Atoolo\Search\Service\Search\ExternalResourceFactory; use Atoolo\Search\Service\Search\InternalMediaResourceFactory; use Atoolo\Search\Service\Search\InternalResourceFactory; use Atoolo\Search\Service\Search\SolrMoreLikeThis; use Atoolo\Search\Service\Search\SolrResultToResourceResolver; use Atoolo\Search\Service\SolrParameterClientFactory; +use Psr\Log\LoggerInterface; +use Psr\Log\NullLogger; class SolrMoreLikeThisBuilder { private string $resourceDir; private string $solrConnectionUrl; + public function __construct( + private readonly ResourceBaseLocatorBuilder $resourceBaseLocatorBuilder, + private readonly LoggerInterface $logger = new NullLogger() + ) { + } + public function resourceDir(string $resourceDir): SolrMoreLikeThisBuilder { $this->resourceDir = $resourceDir; @@ -33,7 +40,7 @@ public function solrConnectionUrl( public function build(): SolrMoreLikeThis { - $resourceBaseLocator = new StaticResourceBaseLocator( + $resourceBaseLocator = $this->resourceBaseLocatorBuilder->build( $this->resourceDir ); $resourceLoader = new SiteKitLoader($resourceBaseLocator); @@ -53,7 +60,8 @@ public function build(): SolrMoreLikeThis new InternalMediaResourceFactory($resourceLoader) ]; $solrResultToResourceResolver = new SolrResultToResourceResolver( - $resourceFactoryList + $resourceFactoryList, + $this->logger ); return new SolrMoreLikeThis( diff --git a/src/Console/Command/SolrSelectBuilder.php b/src/Console/Command/SolrSelectBuilder.php index 7e6b3e0..090470f 100644 --- a/src/Console/Command/SolrSelectBuilder.php +++ b/src/Console/Command/SolrSelectBuilder.php @@ -5,7 +5,6 @@ namespace Atoolo\Search\Console\Command; use Atoolo\Resource\Loader\SiteKitLoader; -use Atoolo\Resource\Loader\StaticResourceBaseLocator; use Atoolo\Search\Service\Search\ExternalResourceFactory; use Atoolo\Search\Service\Search\InternalMediaResourceFactory; use Atoolo\Search\Service\Search\InternalResourceFactory; @@ -19,6 +18,11 @@ class SolrSelectBuilder private string $resourceDir; private string $solrConnectionUrl; + public function __construct( + private readonly ResourceBaseLocatorBuilder $resourceBaseLocatorBuilder + ) { + } + public function resourceDir(string $resourceDir): SolrSelectBuilder { $this->resourceDir = $resourceDir; @@ -34,7 +38,7 @@ public function solrConnectionUrl( public function build(): SolrSelect { - $resourceBaseLocator = new StaticResourceBaseLocator( + $resourceBaseLocator = $this->resourceBaseLocatorBuilder->build( $this->resourceDir ); $resourceLoader = new SiteKitLoader($resourceBaseLocator); diff --git a/src/Service/Search/SolrMoreLikeThis.php b/src/Service/Search/SolrMoreLikeThis.php index 2036517..fd694c0 100644 --- a/src/Service/Search/SolrMoreLikeThis.php +++ b/src/Service/Search/SolrMoreLikeThis.php @@ -10,7 +10,7 @@ use Atoolo\Search\Service\SolrClientFactory; use Solarium\Core\Client\Client; use Solarium\QueryType\MoreLikeThis\Query as SolrMoreLikeThisQuery; -use Solarium\QueryType\Select\Result\Result as SelectResult; +use Solarium\QueryType\MoreLikeThis\Result as SolrMoreLikeThisResult; /** * Implementation of the "More-Like-This" on the basis of a Solr index. @@ -58,7 +58,7 @@ private function buildSolrQuery( } private function buildResult( - SelectResult $result + SolrMoreLikeThisResult $result ): SearchResult { $resourceList = $this->resultToResourceResolver diff --git a/test/Console/Command/ResourceBaseLocatorBuilderTest.php b/test/Console/Command/ResourceBaseLocatorBuilderTest.php new file mode 100644 index 0000000..7c3337b --- /dev/null +++ b/test/Console/Command/ResourceBaseLocatorBuilderTest.php @@ -0,0 +1,31 @@ +build($resourceDir); + + $this->assertEquals( + $objectsDir, + $locator->locate(), + 'unexpected resource dir' + ); + } +} diff --git a/test/Console/Command/SolrIndexerBuilderTest.php b/test/Console/Command/SolrIndexerBuilderTest.php index 14f56bf..d0f74db 100644 --- a/test/Console/Command/SolrIndexerBuilderTest.php +++ b/test/Console/Command/SolrIndexerBuilderTest.php @@ -5,6 +5,7 @@ namespace Atoolo\Search\Test\Console\Command; use Atoolo\Search\Console\Command\Io\IndexerProgressBar; +use Atoolo\Search\Console\Command\ResourceBaseLocatorBuilder; use Atoolo\Search\Console\Command\SolrIndexerBuilder; use Atoolo\Search\Service\Indexer\DocumentEnricher; use PHPUnit\Framework\Attributes\CoversClass; @@ -16,14 +17,11 @@ class SolrIndexerBuilderTest extends TestCase public function testBuild(): void { - $resourceDir = __DIR__ . - '/../../../var/test/SolrIndexerBuilderTest'; - $objectsDir = $resourceDir . '/objects'; - mkdir($objectsDir, 0777, true); - - $builder = new SolrIndexerBuilder(); + $builder = new SolrIndexerBuilder( + $this->createStub(ResourceBaseLocatorBuilder::class) + ); $builder - ->resourceDir($resourceDir) + ->resourceDir('test') ->documentEnricherList([$this->createStub(DocumentEnricher::class)]) ->progressBar($this->createStub(IndexerProgressBar::class)) ->solrConnectionUrl('http://localhost:8382'); diff --git a/test/Console/Command/SolrMoreLikeThisBuilderTest.php b/test/Console/Command/SolrMoreLikeThisBuilderTest.php index 8836bf4..00aabcf 100644 --- a/test/Console/Command/SolrMoreLikeThisBuilderTest.php +++ b/test/Console/Command/SolrMoreLikeThisBuilderTest.php @@ -4,6 +4,7 @@ namespace Atoolo\Search\Test\Console\Command; +use Atoolo\Search\Console\Command\ResourceBaseLocatorBuilder; use Atoolo\Search\Console\Command\SolrMoreLikeThisBuilder; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; @@ -13,7 +14,9 @@ class SolrMoreLikeThisBuilderTest extends TestCase { public function testBuild(): void { - $builder = new SolrMoreLikeThisBuilder(); + $builder = new SolrMoreLikeThisBuilder( + $this->createStub(ResourceBaseLocatorBuilder::class) + ); $builder ->resourceDir('test.php') ->solrConnectionUrl('http://localhost:8382'); diff --git a/test/Console/Command/SolrSelectBuilderTest.php b/test/Console/Command/SolrSelectBuilderTest.php index 82c9208..c81d423 100644 --- a/test/Console/Command/SolrSelectBuilderTest.php +++ b/test/Console/Command/SolrSelectBuilderTest.php @@ -4,6 +4,7 @@ namespace Atoolo\Search\Test\Console\Command; +use Atoolo\Search\Console\Command\ResourceBaseLocatorBuilder; use Atoolo\Search\Console\Command\SolrSelectBuilder; use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; @@ -13,7 +14,9 @@ class SolrSelectBuilderTest extends TestCase { public function testBuild(): void { - $builder = new SolrSelectBuilder(); + $builder = new SolrSelectBuilder( + $this->createStub(ResourceBaseLocatorBuilder::class) + ); $builder->resourceDir('test') ->solrConnectionUrl('http://localhost:8382'); diff --git a/test/Service/Search/SolrMoreLikeThisTest.php b/test/Service/Search/SolrMoreLikeThisTest.php index 01a27f3..882b48a 100644 --- a/test/Service/Search/SolrMoreLikeThisTest.php +++ b/test/Service/Search/SolrMoreLikeThisTest.php @@ -15,8 +15,8 @@ use PHPUnit\Framework\TestCase; use Solarium\Client; use Solarium\QueryType\MoreLikeThis\Query as SolrMoreLikeThisQuery; +use Solarium\QueryType\MoreLikeThis\Result as SolrMoreLikeThisResult; use Solarium\QueryType\Select\Query\FilterQuery; -use Solarium\QueryType\Select\Result\Result as SelectResult; #[CoversClass(SolrMoreLikeThis::class)] class SolrMoreLikeThisTest extends TestCase @@ -39,7 +39,7 @@ protected function setUp(): void $client->method('createMoreLikeThis')->willReturn($query); - $result = $this->createStub(SelectResult::class); + $result = $this->createStub(SolrMoreLikeThisResult::class); $client->method('execute')->willReturn($result); $this->resource = $this->createStub(Resource::class);