1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace uuf6429\ElderBrother\Console\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Command\Command; |
6
|
|
|
use Symfony\Component\Console\Helper\ProgressBar; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
9
|
|
|
use uuf6429\ElderBrother\Config; |
10
|
|
|
use uuf6429\ElderBrother\Exception\RecoverableException; |
11
|
|
|
|
12
|
|
|
class Run extends Command |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var Config |
16
|
|
|
*/ |
17
|
|
|
protected $config; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param Config $config |
21
|
|
|
*/ |
22
|
|
|
public function __construct(Config $config) |
23
|
|
|
{ |
24
|
|
|
parent::__construct(null); |
25
|
|
|
|
26
|
|
|
$this->config = $config; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
|
|
protected function configure() |
33
|
|
|
{ |
34
|
|
|
$this |
35
|
|
|
->setName('run') |
36
|
|
|
->setDescription('Runs configured actions.') |
37
|
|
|
->setHelp('This command runs all actions defined in configuration.') |
38
|
|
|
; |
39
|
|
|
// TODO add argument to specify which even to trigger |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
46
|
|
|
{ |
47
|
|
|
if ($output->isDebug()) { |
48
|
|
|
$output->writeln('<info>Running from:</info> ' . PROJECT_ROOT); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if (extension_loaded('xdebug')) { |
52
|
|
|
$output->writeln( |
53
|
|
|
sprintf( |
54
|
|
|
'<bg=yellow;fg=black;>%s</>', |
55
|
|
|
'Xdebug is enabled; performance will likely be affected.' |
56
|
|
|
) |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$event = ''; // TODO get from param |
|
|
|
|
61
|
|
|
|
62
|
|
|
$actions = $this->config->get($event); |
63
|
|
|
|
64
|
|
|
if (!empty($actions)) { |
65
|
|
|
// See https://github.com/symfony/symfony/pull/10356 for multiple bars |
66
|
|
|
$progress = new ProgressBar($output, count($actions)); |
67
|
|
|
$progress->start(); |
68
|
|
|
$output->write("\n"); |
69
|
|
|
|
70
|
|
|
foreach ($actions as $action) { |
71
|
|
|
$output->write("\033[1A"); |
72
|
|
|
$progress->setMessage('Running "' . $action->getName() . '".'); |
73
|
|
|
$progress->advance(); |
74
|
|
|
$output->write("\n"); |
75
|
|
|
|
76
|
|
|
try { |
77
|
|
|
$action->execute($input, $output); |
78
|
|
|
} catch (RecoverableException $ex) { |
79
|
|
|
$this->getApplication()->renderException($ex, $output); // TODO customize this for recoverable exceptions |
|
|
|
|
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$progress->finish(); |
84
|
|
|
$output->writeln(['', 'FINISHED']); |
85
|
|
|
} else { |
86
|
|
|
$output->writeln('<info>No actions have been set up yet!</info>'); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|