1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GBProd\ElasticsearchDataProviderBundle\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Command to run providing |
13
|
|
|
* |
14
|
|
|
* @author gbprod <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class ProvideCommand extends ContainerAwareCommand |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* {@inheritdoc} |
20
|
|
|
*/ |
21
|
1 |
|
protected function configure() |
22
|
|
|
{ |
23
|
1 |
|
$this |
24
|
1 |
|
->setName('elasticsearch:provide') |
25
|
1 |
|
->setDescription('Provide data to Elasticsearch') |
26
|
1 |
|
->addArgument( |
27
|
1 |
|
'index', |
28
|
1 |
|
InputArgument::OPTIONAL, |
29
|
|
|
'Index to provide' |
30
|
1 |
|
) |
31
|
1 |
|
->addArgument( |
32
|
1 |
|
'type', |
33
|
1 |
|
InputArgument::OPTIONAL, |
34
|
|
|
'Type to provide' |
35
|
1 |
|
) |
36
|
1 |
|
->addOption( |
37
|
1 |
|
'client', |
38
|
1 |
|
null, |
39
|
1 |
|
InputOption::VALUE_REQUIRED, |
40
|
1 |
|
'Client to use (if not default)', |
41
|
|
|
'default' |
42
|
1 |
|
) |
43
|
|
|
; |
44
|
1 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
1 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
50
|
|
|
{ |
51
|
1 |
|
$handler = $this->getContainer() |
52
|
1 |
|
->get('gbprod.elasticsearch_dataprovider.handler') |
53
|
1 |
|
; |
54
|
|
|
|
55
|
1 |
|
$client = $this->getClient($input->getOption('client')); |
56
|
|
|
|
57
|
1 |
|
$index = $input->getArgument('index'); |
58
|
1 |
|
$type = $input->getArgument('type'); |
59
|
|
|
|
60
|
1 |
|
$output->writeln(sprintf( |
61
|
1 |
|
'<info>Providing <comment>%s/%s</comment> for client <comment>%s</comment>...</info>', |
62
|
1 |
|
$index ?: '*', |
63
|
1 |
|
$type ?: '*', |
64
|
1 |
|
$input->getOption('client') |
65
|
1 |
|
)); |
66
|
|
|
|
67
|
1 |
|
$handler->handle($client, $index, $type); |
68
|
1 |
|
} |
69
|
|
|
|
70
|
1 |
|
private function getClient($clientName) |
71
|
|
|
{ |
72
|
1 |
|
$client = $this->getContainer() |
73
|
1 |
|
->get(sprintf( |
74
|
1 |
|
'm6web_elasticsearch.client.%s', |
75
|
|
|
$clientName |
76
|
1 |
|
)) |
77
|
1 |
|
; |
78
|
|
|
|
79
|
1 |
|
if (!$client) { |
80
|
|
|
throw new \InvalidArgumentException(sprintf( |
81
|
|
|
'No client "%s" found', |
82
|
|
|
$clientName |
83
|
|
|
)); |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
return $client; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|