1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GBProd\ElasticaExtraBundle\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
6
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Command to list indices |
12
|
|
|
* |
13
|
|
|
* @author gbprod <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class ListIndexCommand extends ElasticaAwareCommand |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* {@inheritdoc} |
19
|
|
|
*/ |
20
|
2 |
|
protected function configure() |
21
|
|
|
{ |
22
|
2 |
|
$this |
23
|
2 |
|
->setName('elasticsearch:index:list') |
24
|
2 |
|
->setDescription('Index list') |
25
|
2 |
|
->addOption('pattern', '', InputOption::VALUE_REQUIRED, 'Index name filter pattern', null) |
26
|
2 |
|
->addOption('client', null, InputOption::VALUE_REQUIRED, 'Client to use (if not default)', null) |
27
|
|
|
; |
28
|
2 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
2 |
View Code Duplication |
protected function execute(InputInterface $input, OutputInterface $output) |
|
|
|
|
34
|
|
|
{ |
35
|
2 |
|
$client = $this->getClient($input->getOption('client')); |
36
|
|
|
|
37
|
2 |
|
$uri = sprintf('_cat/indices/%s?h=i', $input->getOption('pattern')); |
38
|
2 |
|
$response = $client->request($uri); |
39
|
|
|
|
40
|
2 |
|
$indices = $this->extractIndices($response->getData()['message']); |
41
|
|
|
|
42
|
2 |
|
foreach ($indices as $index) { |
43
|
2 |
|
$output->writeln($index); |
44
|
2 |
|
} |
45
|
2 |
|
} |
46
|
|
|
|
47
|
2 |
|
private function extractIndices($data) |
48
|
|
|
{ |
49
|
2 |
|
$lines = preg_split('#\R#', $data); |
50
|
|
|
|
51
|
2 |
|
return array_map('trim', $lines); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.