1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GBProd\ElasticaProviderBundle\Command; |
4
|
|
|
|
5
|
|
|
use Elastica\Client; |
6
|
|
|
use GBProd\ElasticaProviderBundle\Provider\Handler; |
7
|
|
|
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; |
8
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
13
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Command to run providing |
17
|
|
|
* |
18
|
|
|
* @author gbprod <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class ProvideCommand extends ContainerAwareCommand |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var Handler |
24
|
|
|
*/ |
25
|
|
|
private $handler; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var EventDispatcherInterface |
29
|
|
|
*/ |
30
|
|
|
private $eventDispatcher; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param Handler $handler |
34
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
35
|
|
|
*/ |
36
|
5 |
|
public function __construct( |
37
|
|
|
Handler $handler, |
38
|
|
|
EventDispatcherInterface $eventDispatcher |
39
|
|
|
) { |
40
|
5 |
|
parent::__construct(); |
41
|
5 |
|
$this->handler = $handler; |
42
|
5 |
|
$this->eventDispatcher = $eventDispatcher; |
43
|
5 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
5 |
|
protected function configure() |
49
|
|
|
{ |
50
|
5 |
|
$this |
51
|
5 |
|
->setName('elasticsearch:provide') |
52
|
5 |
|
->setDescription('Provide data to Elasticsearch') |
53
|
5 |
|
->addArgument('index', InputArgument::OPTIONAL, 'Index to provide') |
54
|
5 |
|
->addArgument('type', InputArgument::OPTIONAL, 'Type to provide') |
55
|
5 |
|
->addOption('client', null, InputOption::VALUE_REQUIRED, 'Client to use (if not default)') |
56
|
5 |
|
->addOption('alias', null, InputOption::VALUE_REQUIRED, 'Alias to use instead of index name for providers (index is required to use this option)') |
|
|
|
|
57
|
|
|
; |
58
|
5 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
4 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
64
|
|
|
{ |
65
|
4 |
|
$client = $this->getClient($input->getOption('client')); |
66
|
|
|
|
67
|
3 |
|
$index = $input->getArgument('index'); |
68
|
3 |
|
$type = $input->getArgument('type'); |
69
|
3 |
|
$alias = (null !== $index) ? $input->getOption('alias') : null; // alias usage only if index is set |
70
|
|
|
|
71
|
3 |
|
$output->writeln(sprintf( |
72
|
3 |
|
'<info>Providing <comment>%s/%s</comment> for client <comment>%s</comment></info>', |
73
|
3 |
|
$index ?: '*', |
74
|
3 |
|
$type ?: '*', |
75
|
3 |
|
$input->getOption('client') |
76
|
3 |
|
)); |
77
|
|
|
|
78
|
3 |
|
$this->initializeProgress($output); |
79
|
|
|
|
80
|
3 |
|
$this->handler->handle($client, $index, $type, $alias); |
81
|
3 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $clientName |
85
|
|
|
* |
86
|
|
|
* @return Client |
87
|
|
|
*/ |
88
|
4 |
|
private function getClient($clientName) |
89
|
|
|
{ |
90
|
4 |
|
$clientName = $clientName ?: 'gbprod.elastica_provider.default_client'; |
91
|
|
|
|
92
|
4 |
|
$client = $this->getContainer() |
93
|
4 |
|
->get($clientName, ContainerInterface::NULL_ON_INVALID_REFERENCE) |
94
|
4 |
|
; |
95
|
|
|
|
96
|
4 |
|
if (!$client) { |
97
|
1 |
|
throw new \InvalidArgumentException(sprintf( |
98
|
1 |
|
'No client "%s" found', |
99
|
|
|
$clientName |
100
|
1 |
|
)); |
101
|
|
|
} |
102
|
|
|
|
103
|
3 |
|
return $client; |
104
|
|
|
} |
105
|
|
|
|
106
|
3 |
|
private function initializeProgress(OutputInterface $output) |
107
|
|
|
{ |
108
|
3 |
|
new ProvidingProgressBar($this->eventDispatcher, $output); |
109
|
3 |
|
} |
110
|
|
|
} |
111
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.