Passed
Push — master ( ba8454...6a45f4 )
by Alexander
04:08 queued 01:51
created

Application::addOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Console;
6
7
use Exception;
8
use Psr\EventDispatcher\EventDispatcherInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Component\Console\Style\StyleInterface;
12
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as SymfonyEventDispatcherInterface;
13
use Yiisoft\FriendlyException\FriendlyExceptionInterface;
14
use Yiisoft\Yii\Console\Event\ApplicationShutdown;
15
use Yiisoft\Yii\Console\Event\ApplicationStartup;
16
17
class Application extends \Symfony\Component\Console\Application
18
{
19
    public const VERSION = '3.0.0-dev';
20
21
    /** @psalm-suppress PropertyNotSetInConstructor */
22
    private EventDispatcherInterface $dispatcher;
23
24 2
    public function __construct(string $name = 'Yii Console', string $version = self::VERSION)
25
    {
26 2
        parent::__construct($name, $version);
27 2
    }
28
29 2
    public function setDispatcher(SymfonyEventDispatcherInterface $dispatcher): void
30
    {
31 2
        $this->dispatcher = $dispatcher;
32 2
        parent::setDispatcher($dispatcher);
33 2
    }
34
35
    public function getDispatcher(): EventDispatcherInterface
36
    {
37
        return $this->dispatcher;
38
    }
39
40
    public function start(): void
41
    {
42
        $this->dispatcher->dispatch(new ApplicationStartup());
43
    }
44
45
    public function shutdown(int $exitCode): void
46
    {
47
        $this->dispatcher->dispatch(new ApplicationShutdown($exitCode));
48
    }
49
50
    public function doRenderException(Exception $e, OutputInterface $output): void
51
    {
52
        // Friendly Exception support
53
        if ($e instanceof FriendlyExceptionInterface) {
54
            if ($output instanceof StyleInterface) {
55
                $output->title($e->getName());
56
                $output->note((string) $e->getSolution());
57
                $output->newLine();
58
            } else {
59
                $output->writeln([
60
                    '<fg=red>' . $e->getName() . '</>',
61
                    '<fg=yellow>' . (string) $e->getSolution() . '</>',
62
                    '',
63
                ]);
64
            }
65
        }
66
    }
67
68 2
    public function addOptions(InputOption $options): void
69
    {
70 2
        $this->getDefinition()->addOption($options);
71 2
    }
72
}
73