Passed
Push — master ( ed42e4...b3b06a )
by Kirill
04:44
created

ConsoleConfig::getCommands()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Console\Config;
13
14
use Spiral\Console\Exception\ConfigException;
15
use Spiral\Console\Sequence\CallableSequence;
16
use Spiral\Console\Sequence\CommandSequence;
17
use Spiral\Console\SequenceInterface;
18
use Spiral\Core\InjectableConfig;
19
20
final class ConsoleConfig extends InjectableConfig
21
{
22
    public const CONFIG = 'console';
23
24
    /**
25
     * @var array
26
     */
27
    protected $config = [
28
        'name'      => null,
29
        'version'   => null,
30
        'commands'  => [],
31
        'configure' => [],
32
        'update'    => []
33
    ];
34
35
    /**
36
     * @return string
37
     */
38
    public function getName(): string
39
    {
40
        return $this->config['name'] ?? 'Spiral Framework';
41
    }
42
43
    /**
44
     * @return string
45
     */
46
    public function getVersion(): string
47
    {
48
        return $this->config['version'] ?? 'UNKNOWN';
49
    }
50
51
    /**
52
     * User defined set of commands (to be used when auto-location is off).
53
     *
54
     * @return array
55
     */
56
    public function getCommands(): array
57
    {
58
        if (!array_key_exists('commands', $this->config)) {
59
            //Legacy config support
60
            return [];
61
        }
62
63
        return $this->config['commands'];
64
    }
65
66
    /**
67
     * Get list of configure sequences.
68
     *
69
     * @return \Generator|SequenceInterface[]
70
     *
71
     * @throws ConfigException
72
     */
73
    public function configureSequence(): \Generator
74
    {
75
        $sequence = $this->config['configure'] ?? $this->config['configureSequence'] ?? [];
76
        foreach ($sequence as $item) {
77
            yield $this->parseSequence($item);
78
        }
79
    }
80
81
    /**
82
     * Get list of all update sequences.
83
     *
84
     * @return \Generator|SequenceInterface[]
85
     *
86
     * @throws ConfigException
87
     */
88
    public function updateSequence(): \Generator
89
    {
90
        $sequence = $this->config['update'] ?? $this->config['updateSequence'] ?? [];
91
        foreach ($sequence as $item) {
92
            yield $this->parseSequence($item);
93
        }
94
    }
95
96
    /**
97
     * @param mixed $item
98
     * @return SequenceInterface
99
     *
100
     * @throws ConfigException
101
     */
102
    protected function parseSequence($item): SequenceInterface
103
    {
104
        if ($item instanceof SequenceInterface) {
105
            return $item;
106
        }
107
108
        if (is_string($item)) {
109
            return new CallableSequence($item);
110
        }
111
112
        if (is_array($item) && isset($item['command'])) {
113
            return new CommandSequence(
114
                $item['command'],
115
                $item['options'] ?? [],
116
                $item['header'] ?? '',
117
                $item['footer'] ?? ''
118
            );
119
        }
120
121
        if (is_array($item) && isset($item['invoke'])) {
122
            return new CallableSequence(
123
                $item['invoke'],
124
                $item['parameters'] ?? [],
125
                $item['header'] ?? '',
126
                $item['footer'] ?? ''
127
            );
128
        }
129
130
        throw new ConfigException(sprintf(
131
            'Unable to parse sequence `%s`.',
132
            json_encode($item)
133
        ));
134
    }
135
}
136