1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSElasticaBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace FOS\ElasticaBundle\Command; |
13
|
|
|
|
14
|
|
|
use Elastica\Exception\ExceptionInterface; |
15
|
|
|
use Elastica\Request; |
16
|
|
|
use FOS\ElasticaBundle\Elastica\Client; |
17
|
|
|
use FOS\ElasticaBundle\Index\IndexManager; |
18
|
|
|
use Symfony\Component\Console\Command\Command; |
19
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
20
|
|
|
use Symfony\Component\Console\Input\InputOption; |
21
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @author Jan Schädlich <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class DeleteCommand extends Command |
27
|
|
|
{ |
28
|
|
|
protected static $defaultName = 'fos:elastica:delete'; |
29
|
|
|
|
30
|
|
|
private $client; |
31
|
|
|
private $indexManager; |
32
|
|
|
|
33
|
6 |
|
public function __construct( |
34
|
|
|
Client $client, |
35
|
|
|
IndexManager $indexManager |
36
|
|
|
) { |
37
|
6 |
|
parent::__construct(); |
38
|
|
|
|
39
|
6 |
|
$this->client = $client; |
40
|
6 |
|
$this->indexManager = $indexManager; |
41
|
6 |
|
} |
42
|
|
|
|
43
|
6 |
|
protected function configure() |
44
|
|
|
{ |
45
|
|
|
$this |
46
|
6 |
|
->addOption('index', null, InputOption::VALUE_OPTIONAL, 'Index that needs to be deleted') |
47
|
6 |
|
->setDescription('Deleting an index') |
48
|
|
|
; |
49
|
6 |
|
} |
50
|
|
|
|
51
|
2 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
52
|
|
|
{ |
53
|
2 |
|
$indexName = $input->getOption('index'); |
54
|
2 |
|
$indexes = null === $indexName ? array_keys($this->indexManager->getAllIndexes()) : [$indexName]; |
55
|
|
|
|
56
|
2 |
|
foreach ($indexes as $indexName) { |
57
|
2 |
|
$output->writeln( |
58
|
2 |
|
sprintf('<info>Deleting</info> <comment>%s</comment> ', $indexName) |
59
|
|
|
); |
60
|
2 |
|
$index = $this->indexManager->getIndex($indexName); |
61
|
2 |
|
if (!$index->exists()) { |
62
|
|
|
$output->writeln( |
63
|
|
|
sprintf('<error>%s does not exist and can\'t be deleted</error>', $indexName) |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
continue; |
67
|
|
|
} |
68
|
|
|
|
69
|
2 |
|
$this->deleteIndex($index->getName()); |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
return 0; |
73
|
|
|
} |
74
|
|
|
|
75
|
2 |
View Code Duplication |
private function deleteIndex(string $indexName): void |
76
|
|
|
{ |
77
|
|
|
try { |
78
|
2 |
|
$path = sprintf('%s', $indexName); |
79
|
2 |
|
$this->client->request($path, Request::DELETE); |
80
|
|
|
} catch (ExceptionInterface $deleteOldIndexException) { |
81
|
|
|
throw new \RuntimeException(sprintf( |
82
|
|
|
'Failed to delete index %s with message: %s', |
83
|
|
|
$indexName, |
84
|
|
|
$deleteOldIndexException->getMessage() |
85
|
|
|
), 0, $deleteOldIndexException); |
86
|
|
|
} |
87
|
2 |
|
} |
88
|
|
|
} |
89
|
|
|
|