Passed
Push — master ( 8926af...beb5fc )
by Anton
02:16
created

ConsoleBootloader   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 33
dl 0
loc 114
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A sequence() 0 19 3
A boot() 0 12 1
A addUpdateSequence() 0 9 1
A addCommand() 0 5 1
A addConfigureSequence() 0 9 1
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
declare(strict_types=1);
9
10
namespace Spiral\Bootloader;
11
12
use Spiral\Boot\Bootloader\Bootloader;
13
use Spiral\Boot\KernelInterface;
14
use Spiral\Command\CleanCommand;
15
use Spiral\Command\PublishCommand;
16
use Spiral\Config\ConfiguratorInterface;
17
use Spiral\Config\Patch\Append;
18
use Spiral\Console\CommandLocator;
19
use Spiral\Console\Console;
20
use Spiral\Console\ConsoleDispatcher;
21
use Spiral\Console\LocatorInterface;
22
use Spiral\Console\Sequence\CallableSequence;
23
use Spiral\Console\Sequence\CommandSequence;
24
use Spiral\Core\Container\SingletonInterface;
25
26
/**
27
 * Bootloads console and provides ability to register custom bootload commands.
28
 */
29
final class ConsoleBootloader extends Bootloader implements SingletonInterface
30
{
31
    const DEPENDENCIES = [
32
        TokenizerBootloader::class,
33
    ];
34
35
    const SINGLETONS = [
36
        Console::class          => Console::class,
37
        LocatorInterface::class => CommandLocator::class
38
    ];
39
40
    /** @var ConfiguratorInterface */
41
    private $config;
42
43
    /**
44
     * @param ConfiguratorInterface $config
45
     */
46
    public function __construct(ConfiguratorInterface $config)
47
    {
48
        $this->config = $config;
49
    }
50
51
    /**
52
     * @param KernelInterface   $kernel
53
     * @param ConsoleDispatcher $console
54
     */
55
    public function boot(KernelInterface $kernel, ConsoleDispatcher $console)
56
    {
57
        $kernel->addDispatcher($console);
58
59
        $this->config->setDefaults('console', [
60
            'commands'  => [],
61
            'configure' => [],
62
            'update'    => []
63
        ]);
64
65
        $this->addCommand(CleanCommand::class);
66
        $this->addCommand(PublishCommand::class);
67
    }
68
69
    /**
70
     * @param string $command
71
     */
72
    public function addCommand(string $command)
73
    {
74
        $this->config->modify(
75
            'console',
76
            new Append('commands', null, $command)
77
        );
78
    }
79
80
    /**
81
     * @param array|string $sequence
82
     * @param string       $header
83
     * @param string       $footer
84
     * @param array        $options
85
     */
86
    public function addConfigureSequence(
87
        $sequence,
88
        string $header,
89
        string $footer = '',
90
        array $options = []
91
    ) {
92
        $this->config->modify(
93
            'console',
94
            $this->sequence('configure', $sequence, $header, $footer, $options)
95
        );
96
    }
97
98
    /**
99
     * @param array|string $sequence
100
     * @param string       $header
101
     * @param string       $footer
102
     * @param array        $options
103
     */
104
    public function addUpdateSequence(
105
        $sequence,
106
        string $header,
107
        string $footer = '',
108
        array $options = []
109
    ) {
110
        $this->config->modify(
111
            'console',
112
            $this->sequence('update', $sequence, $header, $footer, $options)
113
        );
114
    }
115
116
    /**
117
     * @param string $target
118
     * @param mixed  $sequence
119
     * @param string $header
120
     * @param string $footer
121
     * @param array  $options
122
     * @return Append
123
     */
124
    private function sequence(
125
        string $target,
126
        $sequence,
127
        string $header,
128
        string $footer,
129
        array $options
130
    ): Append {
131
        if (is_array($sequence) || $sequence instanceof \Closure) {
132
            return new Append(
133
                $target,
134
                null,
135
                new CallableSequence($sequence, $options, $header, $footer)
136
            );
137
        }
138
139
        return new Append(
140
            $target,
141
            null,
142
            new CommandSequence($sequence, $options, $header, $footer)
143
        );
144
    }
145
}
146