-
Notifications
You must be signed in to change notification settings - Fork 2
/
UpdateIndexesCommand.php
106 lines (88 loc) · 2.82 KB
/
UpdateIndexesCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
declare(strict_types=1);
namespace MeiliSearchBundle\Command;
use MeiliSearchBundle\Index\IndexSynchronizerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Style\SymfonyStyle;
use Throwable;
use function sprintf;
/**
* @author Guillaume Loulier <[email protected]>
*/
final class UpdateIndexesCommand extends Command
{
/**
* @var array<string, array>
*/
private $indexes;
/**
* @var IndexSynchronizerInterface
*/
private $indexSynchronizer;
/**
* @var string|null
*/
private $prefix;
/**
* @var string|null
*/
protected static $defaultName = 'meili:update-indexes';
/**
* @param array<string, array> $indexes
* @param IndexSynchronizerInterface $indexSynchronizer
* @param string|null $prefix
*/
public function __construct(
array $indexes,
IndexSynchronizerInterface $indexSynchronizer,
?string $prefix = null
) {
$this->indexes = $indexes;
$this->indexSynchronizer = $indexSynchronizer;
$this->prefix = $prefix;
parent::__construct();
}
/**
* {@inheritdoc}
*/
protected function configure(): void
{
$this
->setDescription('Allow to update the indexes defined in the configuration')
->setDefinition([
new InputOption('force', 'f', InputOption::VALUE_OPTIONAL|InputOption::VALUE_NONE, 'Force the action without asking for confirmation'),
])
;
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);
if (empty($this->indexes)) {
$io->warning('No indexes found, please define at least a single index');
return 1;
}
if ($io->askQuestion(new ConfirmationQuestion('Are you sure that you want to update the indexes?', false)) || $input->getOption('force')) {
try {
$this->indexSynchronizer->updateIndexes($this->indexes, $this->prefix);
} catch (Throwable $throwable) {
$io->error([
'The indexes cannot be updated!',
sprintf('Error: "%s"', $throwable->getMessage())
]);
return 1;
}
$io->success('The indexes has been updated, feel free to query them!');
return 0;
} else {
$io->warning('The indexes update has been discarded');
return 1;
}
}
}