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\Input\InputOption; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use uuf6429\ElderBrother\Config; |
11
|
|
|
use uuf6429\ElderBrother\Exception\RecoverableException; |
12
|
|
|
|
13
|
|
|
class Run extends Command |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var Config |
17
|
|
|
*/ |
18
|
|
|
protected $config; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param Config $config |
22
|
|
|
*/ |
23
|
|
|
public function __construct(Config $config) |
24
|
|
|
{ |
25
|
|
|
parent::__construct(null); |
26
|
|
|
|
27
|
|
|
$this->config = $config; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
protected function configure() |
34
|
|
|
{ |
35
|
|
|
$this |
36
|
|
|
->setName('run') |
37
|
|
|
->setDescription('Runs configured actions for an event.') |
38
|
|
|
->setHelp('This command runs all actions defined in configuration for the specified event.') |
39
|
|
|
->addOption('event', 'e', InputOption::VALUE_REQUIRED, 'The event whose actions will be run.') |
40
|
|
|
->addOption('no-progress', null, InputOption::VALUE_NONE, 'Disables progress bar.') |
41
|
|
|
; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
48
|
|
|
{ |
49
|
|
|
if ($output->isDebug()) { |
50
|
|
|
$output->write(['Running from: <info>', PROJECT_ROOT, '</info>']); |
51
|
|
|
$output->writeln(''); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (extension_loaded('xdebug')) { |
55
|
|
|
$output->writeln( |
56
|
|
|
sprintf( |
57
|
|
|
'<bg=yellow;fg=black;>%s</>', |
58
|
|
|
'Xdebug is enabled; performance will likely be affected.' |
59
|
|
|
) |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$event = $input->getOption('event'); |
64
|
|
|
$withProgress = !$input->hasOption('no-progress'); |
65
|
|
|
$actions = $this->config->get($event); |
66
|
|
|
|
67
|
|
|
if (!empty($actions)) { |
68
|
|
|
if ($withProgress) { |
69
|
|
|
// See https://github.com/symfony/symfony/pull/10356 for multiple bars |
70
|
|
|
$progress = new ProgressBar($output, count($actions)); |
71
|
|
|
$progress->start(); |
72
|
|
|
$output->write("\n"); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
foreach ($actions as $action) { |
76
|
|
|
if ($withProgress) { |
77
|
|
|
$output->write("\033[1A"); |
78
|
|
|
$progress->setMessage('Running "' . $action->getName() . '".'); |
|
|
|
|
79
|
|
|
$progress->advance(); |
80
|
|
|
$output->write("\n"); |
81
|
|
|
} else { |
82
|
|
|
$output->writeln('Running <info>"' . $action->getName() . '"</info>...'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
try { |
86
|
|
|
$action->execute($input, $output); |
87
|
|
|
} catch (\Exception $ex) { |
88
|
|
|
$this->getApplication()->renderException($ex, $output); |
89
|
|
|
if (!($ex instanceof RecoverableException)) { |
90
|
|
|
return 1; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if ($withProgress) { |
96
|
|
|
$progress->finish(); |
97
|
|
|
} |
98
|
|
|
$output->writeln('Done.'); |
99
|
|
|
} else { |
100
|
|
|
$output->writeln( |
101
|
|
|
sprintf( |
102
|
|
|
'<bg=yellow;fg=black;>%s</>', |
103
|
|
|
'No actions have been set up yet!' |
104
|
|
|
) |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return 0; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: