Passed
Branch master (a4c265)
by Anton
02:03
created

CommandBootloader::configureExtensions()   B

Complexity

Conditions 8
Paths 128

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 28
rs 8.2111
c 0
b 0
f 0
cc 8
nc 128
nop 2
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
9
namespace Spiral\Bootloader;
10
11
use Cycle\ORM\ORMInterface;
12
use Psr\Container\ContainerInterface;
13
use Spiral\Boot\Bootloader\Bootloader;
14
use Spiral\Boot\Bootloader\DependedInterface;
15
use Spiral\Command\Cycle;
16
use Spiral\Command\Database;
17
use Spiral\Command\Encrypter;
18
use Spiral\Command\GRPC;
19
use Spiral\Command\Migrate;
20
use Spiral\Command\Translator;
21
use Spiral\Command\Views;
22
use Spiral\Console;
23
use Spiral\Console\Sequence\RuntimeDirectory;
24
use Spiral\Core\Container;
25
use Spiral\Database\DatabaseProviderInterface;
26
use Spiral\Encrypter\EncryptionInterface;
27
use Spiral\Files\FilesInterface;
28
use Spiral\GRPC\InvokerInterface;
29
use Spiral\Migrations\Migrator;
30
use Spiral\Translator\Config\TranslatorConfig;
31
use Spiral\Translator\TranslatorInterface;
32
use Spiral\Views\ViewsInterface;
33
use Symfony\Component\Console\Output\OutputInterface;
34
35
/**
36
 * Register framework directories in tokenizer in order to locate default commands.
37
 */
38
final class CommandBootloader extends Bootloader implements DependedInterface
39
{
40
    /**
41
     * @param ConsoleBootloader $console
42
     * @param Container         $container
43
     */
44
    public function boot(ConsoleBootloader $console, Container $container)
45
    {
46
        $console->addCommand(Console\Command\ConfigureCommand::class);
47
        $console->addCommand(Console\Command\UpdateCommand::class);
48
49
        $console->addConfigureSequence(
50
            [RuntimeDirectory::class, 'ensure'],
51
            '<fg=magenta>[runtime]</fg=magenta> <fg=cyan>verify `runtime` directory access</fg=cyan>'
52
        );
53
54
        $this->configureExtensions($console, $container);
55
    }
56
57
    /**
58
     * @return array
59
     */
60
    public function defineDependencies(): array
61
    {
62
        return [
63
            ConsoleBootloader::class
64
        ];
65
    }
66
67
    /**
68
     * @param ConsoleBootloader $console
69
     * @param Container         $container
70
     */
71
    private function configureExtensions(ConsoleBootloader $console, Container $container): void
72
    {
73
        if ($container->has(DatabaseProviderInterface::class)) {
74
            $this->configureDatabase($console);
75
        }
76
77
        if ($container->has(ORMInterface::class)) {
78
            $this->configureCycle($console, $container);
79
        }
80
81
        if ($container->has(TranslatorInterface::class)) {
82
            $this->configureTranslator($console);
83
        }
84
85
        if ($container->has(ViewsInterface::class)) {
86
            $this->configureViews($console);
87
        }
88
89
        if ($container->has(Migrator::class)) {
90
            $this->configureMigrations($console);
91
        }
92
93
        if ($container->has(InvokerInterface::class)) {
94
            $this->configureGRPC($console);
95
        }
96
97
        if ($container->has(EncryptionInterface::class)) {
98
            $this->configureEncrypter($console);
99
        }
100
    }
101
102
    /**
103
     * @param ConsoleBootloader $console
104
     */
105
    private function configureDatabase(ConsoleBootloader $console)
106
    {
107
        $console->addCommand(Database\ListCommand::class);
108
        $console->addCommand(Database\TableCommand::class);
109
    }
110
111
    /**
112
     * @param ConsoleBootloader  $console
113
     * @param ContainerInterface $container
114
     */
115
    private function configureCycle(ConsoleBootloader $console, ContainerInterface $container)
116
    {
117
        $console->addCommand(Cycle\UpdateCommand::class);
118
119
        $console->addUpdateSequence(
120
            'cycle:update',
121
            '<fg=magenta>[cycle]</fg=magenta> <fg=cyan>update Cycle schema...</fg=cyan>'
122
        );
123
124
        $console->addCommand(Cycle\SyncCommand::class);
125
126
        if ($container->has(Migrator::class)) {
127
            $console->addCommand(Cycle\MigrateCommand::class);
128
        }
129
    }
130
131
    /**
132
     * @param ConsoleBootloader $console
133
     **/
134
    private function configureTranslator(ConsoleBootloader $console)
135
    {
136
        $console->addCommand(Translator\IndexCommand::class);
137
        $console->addCommand(Translator\ExportCommand::class);
138
        $console->addCommand(Translator\ResetCommand::class);
139
140
        $console->addConfigureSequence(
141
            function (FilesInterface $files, TranslatorConfig $config, OutputInterface $output) {
0 ignored issues
show
Bug introduced by
function(...) { /* ... */ } of type callable is incompatible with the type array|string expected by parameter $sequence of Spiral\Bootloader\Consol...:addConfigureSequence(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

141
            /** @scrutinizer ignore-type */ function (FilesInterface $files, TranslatorConfig $config, OutputInterface $output) {
Loading history...
142
                $files->ensureDirectory($config->getLocaleDirectory($config->getDefaultLocale()));
143
                $output->writeln("<info>The default locale directory has been ensured.</info>");
144
            },
145
            '<fg=magenta>[i18n]</fg=magenta> <fg=cyan>ensure default locale directory...</fg=cyan>'
146
        );
147
148
        $console->addConfigureSequence(
149
            'i18n:index',
150
            '<fg=magenta>[i18n]</fg=magenta> <fg=cyan>scan translator function and [[values]] usage...</fg=cyan>'
151
        );
152
    }
153
154
    /**
155
     * @param ConsoleBootloader $console
156
     */
157
    private function configureViews(ConsoleBootloader $console)
158
    {
159
        $console->addCommand(Views\ResetCommand::class);
160
        $console->addCommand(Views\CompileCommand::class);
161
162
        $console->addConfigureSequence(
163
            'views:compile',
164
            '<fg=magenta>[views]</fg=magenta> <fg=cyan>warm up view cache...</fg=cyan>'
165
        );
166
    }
167
168
    /**
169
     * @param ConsoleBootloader $console
170
     */
171
    private function configureMigrations(ConsoleBootloader $console)
172
    {
173
        $console->addCommand(Migrate\InitCommand::class);
174
        $console->addCommand(Migrate\StatusCommand::class);
175
        $console->addCommand(Migrate\MigrateCommand::class);
176
        $console->addCommand(Migrate\RollbackCommand::class);
177
        $console->addCommand(Migrate\ReplayCommand::class);
178
    }
179
180
    /**
181
     * @param ConsoleBootloader $console
182
     */
183
    private function configureGRPC(ConsoleBootloader $console)
184
    {
185
        $console->addCommand(GRPC\GenerateCommand::class);
186
        $console->addCommand(GRPC\ListCommand::class);
187
    }
188
189
    /**
190
     * @param ConsoleBootloader $console
191
     */
192
    private function configureEncrypter(ConsoleBootloader $console)
193
    {
194
        $console->addCommand(Encrypter\KeyCommand::class);
195
    }
196
}