1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the FOSElasticaBundle package. |
4
|
|
|
* |
5
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace FOS\ElasticaBundle\Command; |
12
|
|
|
|
13
|
|
|
use FOS\ElasticaBundle\Index\IndexManager; |
14
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
16
|
|
|
use Symfony\Component\Console\Input\InputOption; |
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Create command |
21
|
|
|
* |
22
|
|
|
* @author Oleg Andreyev <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class CreateCommand extends ContainerAwareCommand |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var IndexManager |
28
|
|
|
*/ |
29
|
|
|
private $indexManager; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var MappingBuilder |
33
|
|
|
*/ |
34
|
|
|
private $mappingBuilder; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var ConfigManager |
38
|
|
|
*/ |
39
|
|
|
private $configManager; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Alias processor |
43
|
|
|
* |
44
|
|
|
* @var AliasProcessor |
45
|
|
|
*/ |
46
|
|
|
private $aliasProcessor; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritdoc} |
50
|
|
|
*/ |
51
|
3 |
|
protected function configure() |
52
|
|
|
{ |
53
|
|
|
$this |
54
|
3 |
|
->setName('fos:elastica:create') |
55
|
3 |
|
->addOption('index', null, InputOption::VALUE_OPTIONAL, 'Index that needs to be created') |
56
|
3 |
|
->setDescription('Creating empty index with mapping') |
57
|
|
|
; |
58
|
3 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
3 |
|
protected function initialize(InputInterface $input, OutputInterface $output) |
64
|
|
|
{ |
65
|
3 |
|
$this->indexManager = $this->getContainer()->get('fos_elastica.index_manager'); |
66
|
3 |
|
$this->mappingBuilder = $this->getContainer()->get('fos_elastica.mapping_builder'); |
67
|
3 |
|
$this->configManager = $this->getContainer()->get('fos_elastica.config_manager'); |
68
|
3 |
|
$this->aliasProcessor = $this->getContainer()->get('fos_elastica.alias_processor'); |
69
|
3 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
3 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
75
|
|
|
{ |
76
|
3 |
|
$indexName = $input->getOption('index'); |
77
|
3 |
|
$indexes = null === $indexName ? array_keys($this->indexManager->getAllIndexes()) : array($indexName); |
78
|
|
|
|
79
|
3 |
|
foreach ($indexes as $indexName) { |
80
|
3 |
|
$output->writeln(sprintf('<info>Creating</info> <comment>%s</comment>', $indexName)); |
81
|
|
|
|
82
|
3 |
|
$indexConfig = $this->configManager->getIndexConfiguration($indexName); |
83
|
3 |
|
$index = $this->indexManager->getIndex($indexName); |
84
|
3 |
|
if ($indexConfig->isUseAlias()) { |
85
|
1 |
|
$this->aliasProcessor->setRootName($indexConfig, $index); |
86
|
|
|
} |
87
|
3 |
|
$mapping = $this->mappingBuilder->buildIndexMapping($indexConfig); |
88
|
3 |
|
$index->create($mapping, false); |
89
|
|
|
} |
90
|
3 |
|
} |
91
|
|
|
} |
92
|
|
|
|