Passed
Pull Request — master (#1068)
by butschster
23:53 queued 14:16
created

ScaffolderBootloader   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 96.97%

Importance

Changes 0
Metric Value
wmc 4
eloc 55
c 0
b 0
f 0
dl 0
loc 110
rs 10
ccs 64
cts 66
cp 0.9697

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A init() 0 82 2
A addDeclaration() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Scaffolder\Bootloader;
6
7
use Cocur\Slugify\Slugify;
8
use Cocur\Slugify\SlugifyInterface;
9
use ReflectionClass;
10
use ReflectionException;
11
use Spiral\Boot\Bootloader\Bootloader;
12
use Spiral\Boot\DirectoriesInterface;
13
use Spiral\Boot\KernelInterface;
14
use Spiral\Config\ConfiguratorInterface;
15
use Spiral\Config\Patch\Append;
16
use Spiral\Console\Bootloader\ConsoleBootloader;
17
use Spiral\Scaffolder\Command;
18
use Spiral\Scaffolder\Config\ScaffolderConfig;
19
use Spiral\Scaffolder\Declaration;
20
21
class ScaffolderBootloader extends Bootloader
22
{
23
    protected const BINDINGS = [
24
        SlugifyInterface::class => Slugify::class,
25
    ];
26
27 370
    public function __construct(
28
        private readonly ConfiguratorInterface $config,
29
        private readonly KernelInterface $kernel,
30
    ) {
31 370
    }
32
33 370
    public function init(ConsoleBootloader $console, DirectoriesInterface $dir): void
34
    {
35 370
        $console->addCommand(Command\InfoCommand::class);
36 370
        $console->addCommand(Command\BootloaderCommand::class);
37 370
        $console->addCommand(Command\CommandCommand::class);
38 370
        $console->addCommand(Command\ConfigCommand::class);
39 370
        $console->addCommand(Command\ControllerCommand::class);
40 370
        $console->addCommand(Command\JobHandlerCommand::class);
41 370
        $console->addCommand(Command\MiddlewareCommand::class);
42 370
        $console->addCommand(Command\FilterCommand::class);
43
44
        try {
45 370
            $defaultNamespace = (new ReflectionClass($this->kernel))->getNamespaceName();
46
        } catch (ReflectionException) {
47
            $defaultNamespace = '';
48
        }
49
50 370
        $this->config->setDefaults(ScaffolderConfig::CONFIG, [
51
            /*
52
             * This is set of comment lines to be applied to every scaffolded file, you can use env() function
53
             * to make it developer specific or set one universal pattern per project.
54
             */
55 370
            'header' => [],
56
57
            /*
58
             * Base directory for generated classes, class will be automatically localed into sub directory
59
             * using given namespace.
60
             */
61 370
            'directory' => $dir->get('app') . 'src/',
62
63
            /*
64
             * Default namespace to be applied for every generated class. By default uses Kernel namespace
65
             *
66
             * Example: 'namespace' => 'MyApplication'
67
             * Controllers: MyApplication\Controllers\SampleController
68
             */
69 370
            'namespace' => $defaultNamespace,
70
71 370
            'declarations' => [],
72
73
            /*
74
             * This is set of default settings to be used for your scaffolding commands.
75
             */
76 370
            'defaults' => [
77 370
                'declarations' => [
78 370
                    Declaration\BootloaderDeclaration::TYPE => [
79 370
                        'namespace' => 'Bootloader',
80 370
                        'postfix' => 'Bootloader',
81 370
                        'class' => Declaration\BootloaderDeclaration::class,
82 370
                    ],
83 370
                    Declaration\ConfigDeclaration::TYPE => [
84 370
                        'namespace' => 'Config',
85 370
                        'postfix' => 'Config',
86 370
                        'class' => Declaration\ConfigDeclaration::class,
87 370
                        'options' => [
88 370
                            'directory' => $dir->get('config'),
89 370
                        ],
90 370
                    ],
91 370
                    Declaration\ControllerDeclaration::TYPE => [
92 370
                        'namespace' => 'Controller',
93 370
                        'postfix' => 'Controller',
94 370
                        'class' => Declaration\ControllerDeclaration::class,
95 370
                    ],
96 370
                    Declaration\FilterDeclaration::TYPE => [
97 370
                        'namespace' => 'Filter',
98 370
                        'postfix' => 'Filter',
99 370
                        'class' => Declaration\FilterDeclaration::class,
100 370
                    ],
101 370
                    Declaration\MiddlewareDeclaration::TYPE => [
102 370
                        'namespace' => 'Middleware',
103 370
                        'postfix' => 'Middleware',
104 370
                        'class' => Declaration\MiddlewareDeclaration::class,
105 370
                    ],
106 370
                    Declaration\CommandDeclaration::TYPE => [
107 370
                        'namespace' => 'Command',
108 370
                        'postfix' => 'Command',
109 370
                        'class' => Declaration\CommandDeclaration::class,
110 370
                    ],
111 370
                    Declaration\JobHandlerDeclaration::TYPE => [
112 370
                        'namespace' => 'Job',
113 370
                        'postfix' => 'Job',
114 370
                        'class' => Declaration\JobHandlerDeclaration::class,
115 370
                    ],
116 370
                ],
117 370
            ],
118 370
        ]);
119
    }
120
121
    /**
122
     * Register a new Scaffolder declaration.
123
     *
124
     * @param non-empty-string $name
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
125
     */
126 3
    public function addDeclaration(string $name, array $declaration): void
127
    {
128 3
        $this->config->modify(
129 3
            ScaffolderConfig::CONFIG,
130 3
            new Append('defaults.declarations', $name, $declaration),
131 3
        );
132
    }
133
}
134