1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GBProd\ElasticsearchDataProviderBundle\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
6
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
7
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
8
|
|
|
use GBProd\ElasticsearchDataProviderBundle\Event\HasStartedHandling; |
9
|
|
|
use GBProd\ElasticsearchDataProviderBundle\Event\HasStartedProviding; |
10
|
|
|
use GBProd\ElasticsearchDataProviderBundle\Event\HasFinishedProviding; |
11
|
|
|
use GBProd\ElasticsearchDataProviderBundle\Event\HasIndexedDocument; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Progress bar for providing |
15
|
|
|
* |
16
|
|
|
* @author gbprod <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class ProvidingProgressBar |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var OutputInterface |
22
|
|
|
*/ |
23
|
|
|
private $output; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var ProgressBar |
27
|
|
|
*/ |
28
|
|
|
private $progressBar; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param EventDispatcherInterface $dispatcher |
32
|
|
|
* @param OutputInterface $output |
33
|
|
|
*/ |
34
|
1 |
|
public function __construct( |
35
|
|
|
EventDispatcherInterface $dispatcher, |
36
|
|
|
OutputInterface $output |
37
|
|
|
) { |
38
|
1 |
|
$this->output = $output; |
39
|
|
|
|
40
|
1 |
|
$dispatcher->addListener( |
41
|
1 |
|
'elasticsearch.has_started_handling', |
42
|
1 |
|
[$this, 'onStartedHandling'] |
43
|
1 |
|
); |
44
|
|
|
|
45
|
1 |
|
$dispatcher->addListener( |
46
|
1 |
|
'elasticsearch.has_started_providing', |
47
|
1 |
|
[$this, 'onStartedProviding'] |
48
|
1 |
|
); |
49
|
|
|
|
50
|
1 |
|
$dispatcher->addListener( |
51
|
1 |
|
'elasticsearch.has_finished_providing', |
52
|
1 |
|
[$this, 'onFinishedProviding'] |
53
|
1 |
|
); |
54
|
|
|
|
55
|
1 |
|
$dispatcher->addListener( |
56
|
1 |
|
'elasticsearch.has_indexed_document', |
57
|
1 |
|
[$this, 'onIndexedDocument'] |
58
|
1 |
|
); |
59
|
1 |
|
} |
60
|
|
|
|
61
|
|
|
public function onStartedHandling(HasStartedHandling $event) |
62
|
|
|
{ |
63
|
|
|
$this->output->writeln(sprintf( |
64
|
|
|
'<info>Start running <comment>%d</comment> providers</info>', |
65
|
|
|
count($event->getEntries()) |
66
|
|
|
)); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function onStartedProviding(HasStartedProviding $event) |
70
|
|
|
{ |
71
|
|
|
$this->output->writeln(sprintf( |
72
|
|
|
'<info>Start running <comment>%s</comment> provider</info>', |
73
|
|
|
get_class($event->getEntry()->getProvider()) |
74
|
|
|
)); |
75
|
|
|
|
76
|
|
|
$count = $event->getEntry()->getProvider()->count(); |
77
|
|
|
if (null !== $count) { |
78
|
|
|
$this->progressBar = new ProgressBar($this->output, $count); |
79
|
|
|
$this->progressBar->setFormat( |
80
|
|
|
' %current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% %memory:6s%' |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function onFinishedProviding(HasFinishedProviding $event) |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
$progressBar->finish(); |
|
|
|
|
88
|
|
|
$this->progressBar = null; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function onIndexedDocument(HasIndexedDocument $event) |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
if ($this->progressBar) { |
94
|
|
|
$this->progressBar->advance(); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.