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
|
|
|
use Symfony\Component\DependencyInjection\Container; |
11
|
|
|
use GBProd\ElasticsearchDataProviderBundle\Event\HasStartedHandling; |
12
|
|
|
use GBProd\ElasticsearchDataProviderBundle\Event\HasStartedProviding; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Command to run providing |
16
|
|
|
* |
17
|
|
|
* @author gbprod <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class ProvideCommand extends ContainerAwareCommand |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
1 |
|
protected function configure() |
25
|
|
|
{ |
26
|
1 |
|
$this |
27
|
1 |
|
->setName('elasticsearch:provide') |
28
|
1 |
|
->setDescription('Provide data to Elasticsearch') |
29
|
1 |
|
->addArgument( |
30
|
1 |
|
'index', |
31
|
1 |
|
InputArgument::OPTIONAL, |
32
|
|
|
'Index to provide' |
33
|
1 |
|
) |
34
|
1 |
|
->addArgument( |
35
|
1 |
|
'type', |
36
|
1 |
|
InputArgument::OPTIONAL, |
37
|
|
|
'Type to provide' |
38
|
1 |
|
) |
39
|
1 |
|
->addOption( |
40
|
1 |
|
'client', |
41
|
1 |
|
null, |
42
|
1 |
|
InputOption::VALUE_REQUIRED, |
43
|
1 |
|
'Client to use (if not default)', |
44
|
|
|
'default' |
45
|
1 |
|
) |
46
|
|
|
; |
47
|
1 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
*/ |
52
|
1 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
53
|
|
|
{ |
54
|
1 |
|
$handler = $this->getContainer() |
55
|
1 |
|
->get('gbprod.elasticsearch_dataprovider.handler') |
56
|
1 |
|
; |
57
|
|
|
|
58
|
1 |
|
$client = $this->getClient($input->getOption('client')); |
59
|
|
|
|
60
|
1 |
|
$index = $input->getArgument('index'); |
61
|
1 |
|
$type = $input->getArgument('type'); |
62
|
|
|
|
63
|
1 |
|
$output->writeln(sprintf( |
64
|
1 |
|
'<info>Providing <comment>%s/%s</comment> for client <comment>%s</comment>...</info>', |
65
|
1 |
|
$index ?: '*', |
66
|
1 |
|
$type ?: '*', |
67
|
1 |
|
$input->getOption('client') |
68
|
1 |
|
)); |
69
|
|
|
|
70
|
1 |
|
$this->initializeProgress($output); |
71
|
|
|
|
72
|
1 |
|
$handler->handle($client, $index, $type); |
73
|
1 |
|
} |
74
|
|
|
|
75
|
1 |
|
private function getClient($clientName) |
76
|
|
|
{ |
77
|
1 |
|
$client = $this->getContainer() |
78
|
1 |
|
->get(sprintf( |
79
|
1 |
|
'm6web_elasticsearch.client.%s', |
80
|
|
|
$clientName |
81
|
1 |
|
)) |
82
|
1 |
|
; |
83
|
|
|
|
84
|
1 |
|
if (!$client) { |
85
|
|
|
throw new \InvalidArgumentException(sprintf( |
86
|
|
|
'No client "%s" found', |
87
|
|
|
$clientName |
88
|
|
|
)); |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
return $client; |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
private function initializeProgress(OutputInterface $output) |
95
|
|
|
{ |
96
|
1 |
|
$dispatcher = $this->getContainer()->get('event_dispatcher'); |
97
|
|
|
|
98
|
1 |
|
$dispatcher->addListener( |
99
|
1 |
|
'elasticsearch.has_started_handling', |
100
|
|
|
function (HasStartedHandling $event) use ($output) { |
101
|
|
|
$output->writeln(sprintf( |
102
|
|
|
'<info>Start running <comment>%d</comment> providers</info>', |
103
|
|
|
count($event->getEntries()) |
104
|
|
|
)); |
105
|
|
|
} |
106
|
1 |
|
); |
107
|
|
|
|
108
|
1 |
|
$dispatcher->addListener( |
109
|
1 |
|
'elasticsearch.has_started_providing', |
110
|
|
|
function (HasStartedProviding $event) use ($output) { |
111
|
|
|
$output->writeln(sprintf( |
112
|
|
|
'<info>Start running <comment>%s</comment> provider</info>', |
113
|
|
|
get_class($event->getEntry()->getProvider()) |
114
|
|
|
)); |
115
|
|
|
} |
116
|
1 |
|
); |
117
|
|
|
|
118
|
1 |
|
$dispatcher->addListener( |
119
|
1 |
|
'elasticsearch.has_indexed_document', |
120
|
|
|
function (HasIndexingDocument $event) use ($output) { |
121
|
|
|
$output->writeln(sprintf( |
122
|
|
|
'<info>Indexing <comment>%s</comment> document</info>', |
123
|
|
|
get_class($event->getEntry()->getId()) |
124
|
|
|
)); |
125
|
|
|
} |
126
|
1 |
|
); |
127
|
1 |
|
} |
128
|
|
|
} |
129
|
|
|
|