Passed
Push — master ( 4165df...b83769 )
by Théo
01:49
created

Application::getDefaultCommands()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 20
rs 9.4285
c 2
b 0
f 0
cc 3
eloc 11
nc 4
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the box project.
7
 *
8
 * (c) Kevin Herrera <[email protected]>
9
 *     Théo Fidry <[email protected]>
10
 *
11
 * This source file is subject to the MIT license that is bundled
12
 * with this source code in the file LICENSE.
13
 */
14
15
namespace KevinGH\Box;
16
17
use ErrorException;
18
use KevinGH\Amend;
19
use Symfony\Component\Console\Application as SymfonyApplication;
20
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
21
use Symfony\Component\Console\Helper\HelperSet;
22
use Symfony\Component\Console\Input\InputInterface;
23
use Symfony\Component\Console\Output\ConsoleOutput;
24
use Symfony\Component\Console\Output\OutputInterface;
25
26
final class Application extends SymfonyApplication
27
{
28
    private const LOGO = <<<'ASCII'
29
30
    ____            
31
   / __ )____  _  __
32
  / __  / __ \| |/_/
33
 / /_/ / /_/ />  <  
34
/_____/\____/_/|_|  
35
                    
36
37
38
ASCII;
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function __construct(string $name = 'Box', string $version = '@git-version@')
44
    {
45
        // convert errors to exceptions
46
        set_error_handler(
47
            function ($code, $message, $file, $line): void {
48
                if (error_reporting() & $code) {
49
                    throw new ErrorException($message, 0, $code, $file, $line);
50
                }
51
                // @codeCoverageIgnoreStart
52
            }
53
        // @codeCoverageIgnoreEnd
54
        );
55
56
        parent::__construct($name, $version);
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     */
62
    public function getLongVersion()
63
    {
64
        if (('@'.'git-version@') !== $this->getVersion()) {
65
            return sprintf(
66
                '<info>%s</info> version <comment>%s</comment> build <comment>%s</comment>',
67
                $this->getName(),
68
                $this->getVersion(),
69
                '@git-commit@'
70
            );
71
        }
72
73
        return '<info>'.$this->getName().'</info> (repo)';
74
    }
75
76
    /**
77
     * @override
78
     */
79
    public function run(InputInterface $input = null, OutputInterface $output = null)
80
    {
81
        $output = $output ?: new ConsoleOutput();
82
83
        $output->getFormatter()->setStyle(
84
            'error',
85
            new OutputFormatterStyle('red')
86
        );
87
88
        $output->getFormatter()->setStyle(
89
            'question',
90
            new OutputFormatterStyle('cyan')
91
        );
92
93
        return parent::run($input, $output);
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function getHelp(): string
100
    {
101
        return self::LOGO.parent::getHelp();
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    protected function getDefaultCommands(): array
108
    {
109
        $commands = parent::getDefaultCommands();
110
111
        if (extension_loaded('phar')) {
112
            $commands[] = new Command\Build();
113
            $commands[] = new Command\Info();
114
        }
115
116
        $commands[] = new Command\Validate();
117
        $commands[] = new Command\Verify();
118
119
        if (('@'.'git-version@') !== $this->getVersion()) {
120
            $command = new Amend\Command('update');
121
            $command->setManifestUri('@manifest_url@');
122
123
            $commands[] = $command;
124
        }
125
126
        return $commands;
127
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132
    protected function getDefaultHelperSet(): HelperSet
133
    {
134
        $helperSet = parent::getDefaultHelperSet();
135
        $helperSet->set(new Helper\ConfigurationHelper());
136
137
        if (('@'.'git-version@') !== $this->getVersion()) {
138
            $helperSet->set(new Amend\Helper());
139
        }
140
141
        return $helperSet;
142
    }
143
}
144