|
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
|
4 |
|
public function __construct( |
|
33
|
|
|
Handler $handler, |
|
34
|
|
|
EventDispatcherInterface $eventDispatcher |
|
35
|
|
|
) |
|
36
|
|
|
{ |
|
37
|
4 |
|
parent::__construct(); |
|
38
|
4 |
|
$this->handler = $handler; |
|
39
|
4 |
|
$this->eventDispatcher = $eventDispatcher; |
|
40
|
4 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritdoc} |
|
44
|
|
|
*/ |
|
45
|
4 |
|
protected function configure() |
|
46
|
|
|
{ |
|
47
|
4 |
|
$this |
|
48
|
4 |
|
->setName('elasticsearch:provide') |
|
49
|
4 |
|
->setDescription('Provide data to Elasticsearch') |
|
50
|
4 |
|
->addArgument('index', InputArgument::OPTIONAL, 'Index to provide') |
|
51
|
4 |
|
->addArgument('type', InputArgument::OPTIONAL, 'Type to provide') |
|
52
|
4 |
|
->addOption('client', null, InputOption::VALUE_REQUIRED, 'Client to use (if not default)') |
|
53
|
4 |
|
->addOption('alias', null, InputOption::VALUE_REQUIRED, 'Alias to use instead of index name for providers (index is required to use this option)') |
|
|
|
|
|
|
54
|
|
|
; |
|
55
|
4 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
4 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
61
|
|
|
{ |
|
62
|
4 |
|
$client = $this->getClient($input->getOption('client')); |
|
63
|
|
|
|
|
64
|
3 |
|
$index = $input->getArgument('index'); |
|
65
|
3 |
|
$type = $input->getArgument('type'); |
|
66
|
3 |
|
$alias = (null !== $index) ? $input->getOption('alias') : null; // alias usage only if index is set |
|
67
|
|
|
|
|
68
|
3 |
|
$output->writeln(sprintf( |
|
69
|
3 |
|
'<info>Providing <comment>%s/%s</comment> for client <comment>%s</comment></info>', |
|
70
|
3 |
|
$index ?: '*', |
|
71
|
3 |
|
$type ?: '*', |
|
72
|
3 |
|
$input->getOption('client') |
|
73
|
3 |
|
)); |
|
74
|
|
|
|
|
75
|
3 |
|
$this->initializeProgress($output); |
|
76
|
|
|
|
|
77
|
3 |
|
$this->handler->handle($client, $index, $type, $alias); |
|
78
|
3 |
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param string $clientName |
|
82
|
|
|
* |
|
83
|
|
|
* @return Client |
|
84
|
|
|
*/ |
|
85
|
4 |
|
private function getClient($clientName) |
|
86
|
|
|
{ |
|
87
|
4 |
|
$clientName = $clientName ?: 'gbprod.elastica_provider.default_client'; |
|
88
|
|
|
|
|
89
|
|
|
/** @var Client $client */ |
|
90
|
4 |
|
$client = $this->getContainer()->get($clientName, ContainerInterface::NULL_ON_INVALID_REFERENCE); |
|
91
|
|
|
|
|
92
|
4 |
|
if (!$client) { |
|
93
|
1 |
|
throw new \InvalidArgumentException(sprintf( |
|
94
|
1 |
|
'No client "%s" found', |
|
95
|
|
|
$clientName |
|
96
|
1 |
|
)); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
3 |
|
return $client; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
3 |
|
private function initializeProgress(OutputInterface $output) |
|
103
|
|
|
{ |
|
104
|
3 |
|
new ProvidingProgressBar($this->eventDispatcher, $output); |
|
105
|
3 |
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
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.