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