Skip to content

Commit

Permalink
feat: cli command atoolo:dump-index-document
Browse files Browse the repository at this point in the history
  • Loading branch information
sitepark-veltrup committed Feb 13, 2024
1 parent cf32291 commit 52da0f6
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
4 changes: 4 additions & 0 deletions config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ services:
arguments:
- !tagged_iterator { tag: 'atoolo.search.indexer.documentEnricher.schema2x' }

Atoolo\Search\Console\Command\DumpIndexDocument:
arguments:
- !tagged_iterator { tag: 'atoolo.search.indexer.documentEnricher.schema2x' }

Atoolo\Search\Console\Application:
public: true
arguments:
Expand Down
101 changes: 101 additions & 0 deletions src/Console/Command/DumpIndexDocument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

declare(strict_types=1);

namespace Atoolo\Search\Console\Command;

use Atoolo\Resource\Loader\ServerVarResourceBaseLocator;
use Atoolo\Resource\Loader\SiteKitLoader;
use Atoolo\Search\Console\Command\Io\TypifiedInput;
use Atoolo\Search\Service\Indexer\DocumentEnricher;
use Atoolo\Search\Service\Indexer\IndexDocument;
use Atoolo\Search\Service\Indexer\IndexSchema2xDocument;
use JsonException;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

#[AsCommand(
name: 'atoolo:dump-index-document',
description: 'Dump a index document'
)]
class DumpIndexDocument extends Command
{
/**
* phpcs:ignore
* @param iterable<DocumentEnricher<IndexDocument>> $documentEnricherList
*/
public function __construct(
private readonly iterable $documentEnricherList
) {
parent::__construct();
}

protected function configure(): void
{
$this
->setHelp('Command to dump a index-document')
->addArgument(
'resource-dir',
InputArgument::REQUIRED,
'Resource directory whose data is to be indexed.'
)
->addArgument(
'paths',
InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
'Resources paths or directories of resources to be indexed.'
)
;
}

/**
* @throws JsonException
*/
protected function execute(
InputInterface $input,
OutputInterface $output
): int {

$typedInput = new TypifiedInput($input);

$resourceDir = $typedInput->getStringArgument('resource-dir');

$subDirectory = null;
if (is_dir($resourceDir . '/objects')) {
$subDirectory = 'objects';
}

$_SERVER['RESOURCE_ROOT'] = $resourceDir;
$resourceBaseLocator = new ServerVarResourceBaseLocator(
'RESOURCE_ROOT',
$subDirectory
);

$paths = $typedInput->getArrayArgument('paths');
$resourceLoader = new SiteKitLoader($resourceBaseLocator);

foreach ($paths as $path) {
$resource = $resourceLoader->load($path);
$doc = new IndexSchema2xDocument();
$processId = 'process-id';

foreach ($this->documentEnricherList as $enricher) {
/** @var IndexSchema2xDocument $doc */
$doc = $enricher->enrichDocument(
$resource,
$doc,
$processId
);
}

echo json_encode(
$doc->getFields(),
JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT
);
}

return Command::SUCCESS;
}
}

0 comments on commit 52da0f6

Please sign in to comment.