Passed
Push — master ( c9a7c7...1fb46e )
by Jakub
01:37
created

Application::createCommandLoader()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 8
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Zalas\Toolbox\Cli;
4
5
use Psr\Container\ContainerInterface;
6
use Symfony\Component\Console\Application as CliApplication;
7
use Symfony\Component\Console\CommandLoader\CommandLoaderInterface;
8
use Symfony\Component\Console\CommandLoader\ContainerCommandLoader;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Zalas\Toolbox\Cli\Command\InstallCommand;
13
use Zalas\Toolbox\Cli\Command\ListCommand;
14
use Zalas\Toolbox\Cli\Command\TestCommand;
15
16
final class Application extends CliApplication
17
{
18
    private $serviceContainer;
19
20 16
    public function __construct(string $version, ServiceContainer $serviceContainer)
21
    {
22 16
        parent::__construct('toolbox', $version);
23
24 16
        $this->serviceContainer = $serviceContainer;
25
26 16
        $this->setCommandLoader($this->createCommandLoader($serviceContainer));
27
    }
28
29 2
    public function doRun(InputInterface $input, OutputInterface $output)
30
    {
31 2
        $this->serviceContainer->set(InputInterface::class, $input);
32 2
        $this->serviceContainer->set(OutputInterface::class, $output);
33
34 2
        return parent::doRun($input, $output);
35
    }
36
37 9
    protected function getDefaultInputDefinition()
38
    {
39 9
        $definition = parent::getDefaultInputDefinition();
40 9
        $definition->addOption(new InputOption('tools', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Path(s) to the list of tools. Can also be set with TOOLBOX_JSON environment variable.', $this->toolsJsonDefault()));
41
42 9
        return $definition;
43
    }
44
45 9
    private function toolsJsonDefault(): array
46
    {
47 9
        return \getenv('TOOLBOX_JSON')
48 1
            ? \array_map('trim', \explode(',', \getenv('TOOLBOX_JSON')))
49 9
            : [__DIR__.'/../../resources/pre-installation.json', __DIR__.'/../../resources/tools.json'];
50
    }
51
52 16
    private function createCommandLoader(ContainerInterface $container): CommandLoaderInterface
53
    {
54 16
        return new ContainerCommandLoader(
55 16
            $container,
56
            [
57 16
                InstallCommand::NAME => InstallCommand::class,
58
                ListCommand::NAME => ListCommand::class,
59
                TestCommand::NAME => TestCommand::class,
60
            ]
61
        );
62
    }
63
}
64