Passed
Push — master ( 242a44...c9fc0d )
by butschster
17:36 queued 09:25
created

ScaffolderBootloader::addDeclaration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Spiral Framework. Scaffolder
5
 *
6
 * @license MIT
7
 * @author  Valentin V (vvval)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Scaffolder\Bootloader;
13
14
use Cocur\Slugify\Slugify;
15
use Cocur\Slugify\SlugifyInterface;
16
use ReflectionClass;
17
use ReflectionException;
18
use Spiral\Boot\Bootloader\Bootloader;
19
use Spiral\Boot\KernelInterface;
20
use Spiral\Config\ConfiguratorInterface;
21
use Spiral\Config\Patch\Append;
22
use Spiral\Console\Bootloader\ConsoleBootloader;
23
use Spiral\Scaffolder\Command;
24
use Spiral\Scaffolder\Config\ScaffolderConfig;
25
use Spiral\Scaffolder\Declaration;
26
27
class ScaffolderBootloader extends Bootloader
28
{
29
    protected const BINDINGS = [
30
        SlugifyInterface::class => Slugify::class,
31
    ];
32
33
    /** @var ConfiguratorInterface */
34
    private $config;
35
36
    /** @var KernelInterface */
37
    private $kernel;
38
39
    /**
40
     * ScaffolderBootloader constructor.
41
     */
42
    public function __construct(ConfiguratorInterface $config, KernelInterface $kernel)
43
    {
44
        $this->config = $config;
45
        $this->kernel = $kernel;
46
    }
47
48
    public function boot(ConsoleBootloader $console): void
49
    {
50
        $console->addCommand(Command\Database\EntityCommand::class, true);
51
        $console->addCommand(Command\Database\RepositoryCommand::class, true);
52
        $console->addCommand(Command\BootloaderCommand::class);
53
        $console->addCommand(Command\CommandCommand::class);
54
        $console->addCommand(Command\ConfigCommand::class);
55
        $console->addCommand(Command\ControllerCommand::class);
56
        $console->addCommand(Command\FilterCommand::class);
57
        $console->addCommand(Command\JobHandlerCommand::class);
58
        $console->addCommand(Command\MiddlewareCommand::class);
59
        $console->addCommand(Command\MigrationCommand::class, true);
60
61
        try {
62
            $defaultNamespace = (new ReflectionClass($this->kernel))->getNamespaceName();
63
        } catch (ReflectionException $e) {
64
            $defaultNamespace = '';
65
        }
66
67
        $this->config->setDefaults(ScaffolderConfig::CONFIG, [
68
            /*
69
             * This is set of comment lines to be applied to every scaffolded file, you can use env() function
70
             * to make it developer specific or set one universal pattern per project.
71
             */
72
            'header'       => [
73
                '{project-name}',
74
                '',
75
                '@author {author-name}',
76
            ],
77
78
            /*
79
             * Base directory for generated classes, class will be automatically localed into sub directory
80
             * using given namespace.
81
             */
82
            'directory'    => directory('app') . 'src/',
83
84
            /*
85
             * Default namespace to be applied for every generated class. By default uses Kernel namespace
86
             *
87
             * Example: 'namespace' => 'MyApplication'
88
             * Controllers: MyApplication\Controllers\SampleController
89
             */
90
            'namespace'    => $defaultNamespace,
91
92
            /*
93
             * This is set of default settings to be used for your scaffolding commands.
94
             */
95
            'declarations' => [
96
                'bootloader' => [
97
                    'namespace' => 'Bootloader',
98
                    'postfix'   => 'Bootloader',
99
                    'class'     => Declaration\BootloaderDeclaration::class,
100
                ],
101
                'config'     => [
102
                    'namespace' => 'Config',
103
                    'postfix'   => 'Config',
104
                    'class'     => Declaration\ConfigDeclaration::class,
105
                    'options'   => [
106
                        'directory' => directory('config'),
107
                    ],
108
                ],
109
                'controller' => [
110
                    'namespace' => 'Controller',
111
                    'postfix'   => 'Controller',
112
                    'class'     => Declaration\ControllerDeclaration::class,
113
                ],
114
                'middleware' => [
115
                    'namespace' => 'Middleware',
116
                    'postfix'   => '',
117
                    'class'     => Declaration\MiddlewareDeclaration::class,
118
                ],
119
                'command'    => [
120
                    'namespace' => 'Command',
121
                    'postfix'   => 'Command',
122
                    'class'     => Declaration\CommandDeclaration::class,
123
                ],
124
                'jobHandler' => [
125
                    'namespace' => 'Job',
126
                    'postfix'   => 'Job',
127
                    'class'     => Declaration\JobHandlerDeclaration::class,
128
                ],
129
                'migration'  => [
130
                    'namespace' => '',
131
                    'postfix'   => 'Migration',
132
                    'class'     => Declaration\MigrationDeclaration::class,
133
                ],
134
                'filter'     => [
135
                    'namespace' => 'Request',
136
                    'postfix'   => 'Request',
137
                    'class'     => Declaration\FilterDeclaration::class,
138
                    'options'   => [
139
                        //Set of default filters and validate rules for various types
140
                        'mapping' => [
141
                            'int'     => [
142
                                'source'    => 'data',
143
                                'setter'    => 'intval',
144
                                'validates' => ['notEmpty', 'integer'],
145
                            ],
146
                            'integer' => [
147
                                'source'    => 'data',
148
                                'setter'    => 'intval',
149
                                'validates' => ['notEmpty', 'integer'],
150
                            ],
151
                            'float'   => [
152
                                'source'    => 'data',
153
                                'setter'    => 'floatval',
154
                                'validates' => ['notEmpty', 'float'],
155
                            ],
156
                            'double'  => [
157
                                'source'    => 'data',
158
                                'setter'    => 'floatval',
159
                                'validates' => ['notEmpty', 'float'],
160
                            ],
161
                            'string'  => [
162
                                'source'    => 'data',
163
                                'setter'    => 'strval',
164
                                'validates' => ['notEmpty', 'string'],
165
                            ],
166
                            'bool'    => [
167
                                'source'    => 'data',
168
                                'setter'    => 'boolval',
169
                                'validates' => ['notEmpty', 'boolean'],
170
                            ],
171
                            'boolean' => [
172
                                'source'    => 'data',
173
                                'setter'    => 'boolval',
174
                                'validates' => ['notEmpty', 'boolean'],
175
                            ],
176
                            'email'   => [
177
                                'source'    => 'data',
178
                                'setter'    => 'strval',
179
                                'validates' => ['notEmpty', 'string', 'email'],
180
                            ],
181
                            'file'    => [
182
                                'source'    => 'file',
183
                                'validates' => ['file::uploaded'],
184
                            ],
185
                            'image'   => [
186
                                'source'    => 'file',
187
                                'validates' => ['image::uploaded', 'image::valid'],
188
                            ],
189
                            null      => [
190
                                'source'    => 'data',
191
                                'setter'    => 'strval',
192
                                'validates' => ['notEmpty', 'string'],
193
                            ],
194
                        ],
195
                    ],
196
                ],
197
                'entity'     => [
198
                    'namespace' => 'Database',
199
                    'postfix'   => '',
200
                    'options'   => [
201
                        'annotated' => Declaration\Database\Entity\AnnotatedDeclaration::class,
202
                    ],
203
                ],
204
                'repository' => [
205
                    'namespace' => 'Repository',
206
                    'postfix'   => 'Repository',
207
                    'class'     => Declaration\Database\RepositoryDeclaration::class,
208
                ],
209
            ],
210
        ]);
211
    }
212
213
    /**
214
     * Register new Scaffolder declaration.
215
     */
216
    public function addDeclaration(string $name, array $declaration): void
217
    {
218
        $this->config->modify(ScaffolderConfig::CONFIG, new Append('declarations', $name, $declaration));
219
    }
220
}
221