1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Darkilliant\ProcessBundle\ProcessNotifier; |
4
|
|
|
|
5
|
|
|
use Darkilliant\ProcessBundle\Console\ProgressBar; |
6
|
|
|
use Darkilliant\ProcessBundle\State\ProcessState; |
7
|
|
|
use Darkilliant\ProcessBundle\Step\IterableStepInterface; |
8
|
|
|
use Darkilliant\ProcessBundle\Step\StepInterface; |
9
|
|
|
use Symfony\Component\Console\ConsoleEvents; |
10
|
|
|
use Symfony\Component\Console\Event\ConsoleEvent; |
11
|
|
|
use Symfony\Component\Console\Output\NullOutput; |
12
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @internal |
16
|
|
|
* Class ProgressBarProcessNotifier |
17
|
|
|
*/ |
18
|
|
|
class ProgressBarProcessNotifier implements EventSubscriberInterface |
19
|
|
|
{ |
20
|
|
|
/** @var ProgressBar */ |
21
|
|
|
private $progressBar; |
22
|
|
|
|
23
|
9 |
|
public function __construct(ProgressBar $progressBar) |
24
|
|
|
{ |
25
|
9 |
|
$this->progressBar = $progressBar; |
26
|
9 |
|
$this->progressBar->setOutput(new NullOutput()); |
27
|
9 |
|
} |
28
|
|
|
|
29
|
1 |
|
public static function getSubscribedEvents() |
30
|
|
|
{ |
31
|
|
|
return [ |
32
|
1 |
|
ConsoleEvents::COMMAND => ['onCommand', 255], |
33
|
|
|
]; |
34
|
|
|
} |
35
|
|
|
|
36
|
1 |
|
public function onCommand(ConsoleEvent $event) |
37
|
|
|
{ |
38
|
1 |
|
$this->progressBar->setOutput($event->getOutput()); |
39
|
1 |
|
} |
40
|
|
|
|
41
|
5 |
|
public function onStartProcess(ProcessState $state, StepInterface $step) |
42
|
|
|
{ |
43
|
5 |
|
if (!$step instanceof IterableStepInterface) { |
44
|
1 |
|
return null; |
45
|
|
|
} |
46
|
|
|
|
47
|
4 |
|
if (!$state->getOptions()['progress_bar']) { |
48
|
1 |
|
return null; |
49
|
|
|
} |
50
|
|
|
|
51
|
3 |
|
$count = $step->count($state); |
52
|
|
|
|
53
|
3 |
|
if (!$count) { |
54
|
|
|
return null; |
55
|
|
|
} |
56
|
|
|
|
57
|
3 |
|
$this->progressBar->create($count, get_class($step)); |
58
|
3 |
|
} |
59
|
|
|
|
60
|
2 |
|
public function onUpdateProcess(ProcessState $state, StepInterface $step) |
61
|
|
|
{ |
62
|
2 |
|
if (!$state->getOptions()['progress_bar']) { |
63
|
1 |
|
return; |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
$this->progressBar->setProgress($step->getProgress($state)); |
|
|
|
|
67
|
1 |
|
} |
68
|
|
|
|
69
|
2 |
|
public function onEndProcess(ProcessState $state, StepInterface $step) |
|
|
|
|
70
|
|
|
{ |
71
|
2 |
|
if (!$state->getOptions()['progress_bar']) { |
72
|
1 |
|
return; |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
$this->progressBar->finish(); |
76
|
1 |
|
} |
77
|
|
|
} |
78
|
|
|
|