-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathPurgeCommand.php
More file actions
127 lines (102 loc) · 3.68 KB
/
Copy pathPurgeCommand.php
File metadata and controls
127 lines (102 loc) · 3.68 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/*
* This file is part of the MassiveSearchBundle
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Massive\Bundle\SearchBundle\Command;
use Massive\Bundle\SearchBundle\Search\SearchManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
/**
* Command to purge search indexes.
*
* @final
*/
class PurgeCommand extends Command
{
protected static $defaultName = 'massive:search:purge';
/**
* @var SearchManagerInterface
*/
private $searchManager;
/**
* @var QuestionHelper
*/
private $questionHelper;
public function __construct(
SearchManagerInterface $searchManager,
?QuestionHelper $questionHelper = null
) {
parent::__construct(self::$defaultName);
$this->searchManager = $searchManager;
$this->questionHelper = $questionHelper ?: new QuestionHelper();
}
public function configure()
{
$this->setDescription('Purge one, many or all indexes.');
$this->setHelp(
<<<'EOT'
Purge one, many or all indexes:
$ %command.full_name%
Execute without any arguments in order to see the active indexes, use the
<comment>--index=<foo></comment> option in order to specify specific indexes or
<comment>--all</comment> to purge all indexes
EOT
);
$this->addOption('index', 'i', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Specify index to remove');
$this->addOption('all', null, InputOption::VALUE_NONE, 'Purge ALL indexes.');
$this->addOption('force', null, InputOption::VALUE_NONE, 'Do not ask for confirmation.');
}
public function execute(InputInterface $input, OutputInterface $output): int
{
$indexes = $input->getOption('index');
$all = $input->getOption('all');
$force = $input->getOption('force');
$allIndexes = $this->searchManager->getIndexNames();
if (!$all && empty($indexes)) {
return $this->listIndexes($output, $allIndexes);
}
if ($all) {
$indexes = $this->searchManager->getIndexNames();
}
$unknownIndexes = \array_diff($indexes, $allIndexes);
if ($unknownIndexes) {
throw new \InvalidArgumentException(\sprintf(
'Unknown indexes "%s", known indexes: "%s"',
\implode('", "', $unknownIndexes),
\implode('", "', $allIndexes)
));
}
foreach ($indexes as $indexName) {
$question = new ConfirmationQuestion(\sprintf(
'Are you sure you want to purge index "%s"? ',
$indexName
), false);
if (true === $force || $this->questionHelper->ask($input, $output, $question)) {
$this->searchManager->purge($indexName);
}
}
return 0;
}
private function listIndexes(OutputInterface $output, array $indexNames): int
{
if (!$indexNames) {
$output->writeln('No indexes currently exist.');
return 0;
}
$output->writeln('<info>Specify the option: </>--index=<index_name><info> where </><index_name><info> is one of the following:</>');
$output->write(\PHP_EOL);
foreach ($indexNames as $indexName) {
$output->writeln(' ' . $indexName);
}
return 0;
}
}