|
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
|
|
|
class ProgressBarProcessNotifier implements EventSubscriberInterface |
|
15
|
|
|
{ |
|
16
|
|
|
/** @var ProgressBar */ |
|
17
|
|
|
private $progressBar; |
|
18
|
|
|
|
|
19
|
|
|
private $currentStep; |
|
20
|
|
|
|
|
21
|
9 |
|
public function __construct(ProgressBar $progressBar) |
|
22
|
|
|
{ |
|
23
|
9 |
|
$this->progressBar = $progressBar; |
|
24
|
9 |
|
$this->progressBar->setOutput(new NullOutput()); |
|
25
|
9 |
|
} |
|
26
|
|
|
|
|
27
|
1 |
|
public static function getSubscribedEvents() |
|
28
|
|
|
{ |
|
29
|
|
|
return [ |
|
30
|
1 |
|
ConsoleEvents::COMMAND => ['onCommand', 255], |
|
31
|
|
|
]; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
1 |
|
public function onCommand(ConsoleEvent $event) |
|
35
|
|
|
{ |
|
36
|
1 |
|
$this->progressBar->setOutput($event->getOutput()); |
|
37
|
1 |
|
} |
|
38
|
|
|
|
|
39
|
5 |
|
public function onStartProcess(ProcessState $state, StepInterface $step) |
|
40
|
|
|
{ |
|
41
|
5 |
|
if (!$step instanceof IterableStepInterface) { |
|
42
|
1 |
|
return; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
4 |
|
$count = $step->count($state); |
|
46
|
|
|
|
|
47
|
4 |
|
if (!$count || !$state->getOptions()['progress_bar']) { |
|
48
|
1 |
|
return; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
3 |
|
$this->progressBar->create($count, get_class($step)); |
|
52
|
3 |
|
$this->currentStep = $step; |
|
53
|
3 |
|
} |
|
54
|
|
|
|
|
55
|
2 |
|
public function onUpdateProcess(ProcessState $state, StepInterface $step) |
|
|
|
|
|
|
56
|
|
|
{ |
|
57
|
2 |
|
if (!$this->currentStep) { |
|
58
|
1 |
|
return; |
|
59
|
|
|
} |
|
60
|
1 |
|
$this->progressBar->setProgress($this->currentStep->getProgress($state)); |
|
61
|
1 |
|
} |
|
62
|
|
|
|
|
63
|
2 |
|
public function onEndProcess(ProcessState $state, StepInterface $step) |
|
|
|
|
|
|
64
|
|
|
{ |
|
65
|
2 |
|
if (!$this->currentStep) { |
|
66
|
1 |
|
return; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
$this->currentStep = null; |
|
70
|
1 |
|
$this->progressBar->finish(); |
|
71
|
1 |
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.