Completed
Push — master ( 2afe63...db6e9c )
by Christian
02:24
created

Run::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace uuf6429\ElderBrother\Console\Command;
4
5
use Symfony\Component\Console\Helper\ProgressBar;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use uuf6429\ElderBrother\Exception\RecoverableException;
10
11
class Run extends CommandAbstract
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16
    protected function configure()
17
    {
18
        $this
19
            ->setName('run')
20
            ->setDescription('Runs configured actions for an event.')
21
            ->setHelp('This command runs all actions defined in configuration for the specified event.')
22
            ->addOption('event', 'e', InputOption::VALUE_REQUIRED, 'The event whose actions will be run.')
23
            ->addOption('no-progress', null, InputOption::VALUE_NONE, 'Disables progress bar.')
24
        ;
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    protected function execute(InputInterface $input, OutputInterface $output)
31
    {
32
        if ($output->isDebug()) {
33
            $output->write(['Running from: <info>', PROJECT_ROOT, '</info>']);
34
            $output->writeln('');
35
        }
36
37
        if (extension_loaded('xdebug')) {
38
            $output->writeln(
39
                sprintf(
40
                    '<bg=yellow;fg=black;>%s</>',
41
                    'Xdebug is enabled; performance will likely be affected.'
42
                )
43
            );
44
        }
45
46
        $event = $input->getOption('event');
47
        $actions = $this->config->get($event);
48
49
        if (!empty($actions)) {
50
            // See https://github.com/symfony/symfony/pull/10356 for multiple bars
51
            $progress = !$input->hasOption('no-progress')
52
                ? new ProgressBar($output, count($actions))
53
                : null;
54
            if ($progress) {
55
                $progress->start();
56
                $output->write("\n");
57
            }
58
59
            foreach ($actions as $action) {
60
                if ($progress) {
61
                    $output->write("\033[1A");
62
                    $progress->setMessage('Running "' . $action->getName() . '".');
63
                    $progress->advance();
64
                    $output->write("\n");
65
                } else {
66
                    $output->writeln('Running <info>"' . $action->getName() . '"</info>...');
67
                }
68
69
                try {
70
                    $action->execute($input, $output);
71
                } catch (\Exception $ex) {
72
                    $this->getApplication()->renderException($ex, $output);
73
                    if (!($ex instanceof RecoverableException)) {
74
                        return 1;
75
                    }
76
                }
77
            }
78
79
            if ($progress) {
80
                $progress->finish();
81
            }
82
            $output->writeln('Done.');
83
        }
84
85
        return 0;
86
    }
87
}
88