Skip to content

Commit

Permalink
Allow to select which index to index
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Jul 4, 2024
1 parent 771300d commit 3790050
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
24 changes: 23 additions & 1 deletion src/Command/IndexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Setono\SyliusMeilisearchPlugin\Message\Command\Index;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Messenger\MessageBusInterface;
Expand All @@ -25,9 +27,29 @@ public function __construct(
parent::__construct();
}

protected function configure(): void
{
$this->addArgument('indexes', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, 'Names of the index(es) to index', $this->indexRegistry->getNames());
}

protected function interact(InputInterface $input, OutputInterface $output): void
{
/** @var list<string> $indexes */
$indexes = $input->getArgument('indexes');

foreach ($indexes as $index) {
if (!$this->indexRegistry->has($index)) {
throw new RuntimeException(sprintf('No index exists with the name "%s". Available indexes are: [%s]', $index, implode(', ', $this->indexRegistry->getNames())));
}
}
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
foreach ($this->indexRegistry as $index) {
/** @var list<string> $indexes */
$indexes = $input->getArgument('indexes');

foreach ($indexes as $index) {
$this->commandBus->dispatch(new Index($index));
}

Expand Down
24 changes: 22 additions & 2 deletions src/Config/IndexRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
*/
final class IndexRegistry implements \IteratorAggregate, IndexRegistryInterface
{
public function has(string $name): bool
{
return isset($this->indexes[$name]);
}

/**
* An array of indexes, indexed by the name of the index
*
Expand Down Expand Up @@ -44,20 +49,35 @@ public function getByEntity(object|string $entity): array

$indexes = [];

foreach ($this->indexes as $index) {
foreach ($this->indexes as $name => $index) {
if ($index->hasEntity($entity)) {
$indexes[] = $index;
$indexes[$name] = $index;
}
}

return $indexes;
}

public function getAll(): array
{
return $this->indexes;
}

public function getNames(): array
{
return array_keys($this->indexes);
}

/**
* @return \ArrayIterator<string, Index>
*/
public function getIterator(): \ArrayIterator
{
return new \ArrayIterator($this->indexes);
}

public function count(): int
{
return count($this->indexes);
}
}
22 changes: 19 additions & 3 deletions src/Config/IndexRegistryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
/**
* @extends \Traversable<Index>
*/
interface IndexRegistryInterface extends \Traversable
interface IndexRegistryInterface extends \Traversable, \Countable
{
public function has(string $name): bool;

/**
* @throws \InvalidArgumentException if an index with the same name already exists
*/
Expand All @@ -20,11 +22,25 @@ public function add(Index $index): void;
public function get(string $name): Index;

/**
* This method returns the index where the $class is configured
* This method returns the indexes where the $class is configured
*
* @param object|class-string $entity
*
* @return list<Index>
* @return array<string, Index>
*/
public function getByEntity(object|string $entity): array;

/**
* Returns all indexes, indexed by their name
*
* @return array<string, Index>
*/
public function getAll(): array;

/**
* Returns a list of all index names
*
* @return list<string>
*/
public function getNames(): array;
}

0 comments on commit 3790050

Please sign in to comment.