|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Meilisearch\Bundle\Command; |
| 6 | + |
| 7 | +use Meilisearch\Bundle\Collection; |
| 8 | +use Meilisearch\Bundle\EventListener\ConsoleOutputSubscriber; |
| 9 | +use Meilisearch\Bundle\Model\Aggregator; |
| 10 | +use Meilisearch\Bundle\SearchService; |
| 11 | +use Meilisearch\Bundle\Services\SettingsUpdater; |
| 12 | +use Symfony\Component\Console\Input\InputInterface; |
| 13 | +use Symfony\Component\Console\Input\InputOption; |
| 14 | +use Symfony\Component\Console\Output\OutputInterface; |
| 15 | +use Symfony\Component\Console\Style\SymfonyStyle; |
| 16 | +use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
| 17 | + |
| 18 | +final class MeilisearchUpdateSettingsCommand extends IndexCommand |
| 19 | +{ |
| 20 | + private SettingsUpdater $settingsUpdater; |
| 21 | + private EventDispatcherInterface $eventDispatcher; |
| 22 | + |
| 23 | + public function __construct(SearchService $searchService, SettingsUpdater $settingsUpdater, EventDispatcherInterface $eventDispatcher) |
| 24 | + { |
| 25 | + parent::__construct($searchService); |
| 26 | + |
| 27 | + $this->settingsUpdater = $settingsUpdater; |
| 28 | + $this->eventDispatcher = $eventDispatcher; |
| 29 | + } |
| 30 | + |
| 31 | + public static function getDefaultName(): string |
| 32 | + { |
| 33 | + return 'meilisearch:update-settings'; |
| 34 | + } |
| 35 | + |
| 36 | + public static function getDefaultDescription(): string |
| 37 | + { |
| 38 | + return 'Push settings to meilisearch'; |
| 39 | + } |
| 40 | + |
| 41 | + protected function configure(): void |
| 42 | + { |
| 43 | + $this |
| 44 | + ->setDescription(self::getDefaultDescription()) |
| 45 | + ->addOption('indices', 'i', InputOption::VALUE_OPTIONAL, 'Comma-separated list of index names') |
| 46 | + ->addOption( |
| 47 | + 'response-timeout', |
| 48 | + 't', |
| 49 | + InputOption::VALUE_REQUIRED, |
| 50 | + 'Timeout (in ms) to get response from the search engine', |
| 51 | + self::DEFAULT_RESPONSE_TIMEOUT |
| 52 | + ) |
| 53 | + ; |
| 54 | + } |
| 55 | + |
| 56 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 57 | + { |
| 58 | + $this->eventDispatcher->addSubscriber(new ConsoleOutputSubscriber(new SymfonyStyle($input, $output))); |
| 59 | + |
| 60 | + $indexes = $this->getEntitiesFromArgs($input, $output); |
| 61 | + $entitiesToIndex = $this->entitiesToIndex($indexes); |
| 62 | + $responseTimeout = ((int) $input->getOption('response-timeout')) ?: self::DEFAULT_RESPONSE_TIMEOUT; |
| 63 | + |
| 64 | + /** @var array $index */ |
| 65 | + foreach ($entitiesToIndex as $index) { |
| 66 | + $entityClassName = $index['class']; |
| 67 | + |
| 68 | + if (!$this->searchService->isSearchable($entityClassName)) { |
| 69 | + continue; |
| 70 | + } |
| 71 | + |
| 72 | + $this->settingsUpdater->update($index['prefixed_name'], $responseTimeout); |
| 73 | + } |
| 74 | + |
| 75 | + $output->writeln('<info>Done!</info>'); |
| 76 | + |
| 77 | + return 0; |
| 78 | + } |
| 79 | + |
| 80 | + private function entitiesToIndex($indexes): array |
| 81 | + { |
| 82 | + foreach ($indexes as $key => $index) { |
| 83 | + $entityClassName = $index['class']; |
| 84 | + |
| 85 | + if (!is_subclass_of($entityClassName, Aggregator::class)) { |
| 86 | + continue; |
| 87 | + } |
| 88 | + |
| 89 | + $indexes->forget($key); |
| 90 | + |
| 91 | + $indexes = new Collection(array_merge( |
| 92 | + $indexes->all(), |
| 93 | + array_map( |
| 94 | + static fn ($entity) => ['name' => $index['name'], 'prefixed_name' => $index['prefixed_name'], 'class' => $entity], |
| 95 | + $entityClassName::getEntities() |
| 96 | + ) |
| 97 | + )); |
| 98 | + } |
| 99 | + |
| 100 | + return array_unique($indexes->all(), SORT_REGULAR); |
| 101 | + } |
| 102 | +} |
0 commit comments