Application::__construct()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
nc 3
cc 3
eloc 19
nop 0
1
<?php
2
3
namespace uuf6429\ElderBrother\Console;
4
5
use Psr\Log;
6
use Symfony\Component\Console\Application as ConsoleApplication;
7
use Symfony\Component\Console\Command\Command as SfyCommand;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Logger\ConsoleLogger;
10
use Symfony\Component\Console\Output\ConsoleOutput;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use uuf6429\ElderBrother\Config;
13
14
class Application extends ConsoleApplication
15
{
16
    /**
17
     * @var ConsoleOutput
18
     */
19
    private $output;
20
    /**
21
     * @var ConsoleLogger
22
     */
23
    private $logger;
24
    /**
25
     * @var Config\ConfigInterface
26
     */
27
    private $config;
28
29
    public function __construct()
30
    {
31
        error_reporting(-1);
32
33
        parent::__construct('Elder Brother', '1.0.0');
34
35
        $this->output = new ConsoleOutput();
36
        $this->logger = new ConsoleLogger($this->output);
37
        $this->config = new Config\Config();
38
39
        $this->logger->debug('Loading configuration...');
40
        foreach ([
41
            PROJECT_ROOT . '.brother.php',
42
            PROJECT_ROOT . '.brother.local.php',
43
        ] as $configFile) {
44
            if (file_exists($configFile)) {
45
                $this->logger->debug('Loading config file: ' . $configFile);
46
                $this->config->loadFromFile($configFile, $this->logger);
47
            } else {
48
                $this->logger->debug('Config file does not exist: ' . $configFile);
49
            }
50
        }
51
52
        $this->add(new Command\Run());
53
        $this->add(new Command\Install());
54
        $this->add(new Command\Uninstall());
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     *
60
     * @throws \RuntimeException
61
     */
62
    public function run(InputInterface $input = null, OutputInterface $output = null)
63
    {
64
        if ($output) {
65
            throw new \RuntimeException('Output cannot be set.');
66
        }
67
68
        return parent::run($input, $this->output);
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function add(SfyCommand $command)
75
    {
76
        if ($command instanceof Config\ConfigAwareInterface) {
77
            $command->setConfig($this->config);
78
        }
79
80
        if ($command instanceof Log\LoggerAwareInterface) {
81
            $command->setLogger($this->logger);
82
        }
83
84
        return parent::add($command);
85
    }
86
}
87