|
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\KernelInterface; |
|
13
|
|
|
use Spiral\Config\ConfiguratorInterface; |
|
14
|
|
|
use Spiral\Config\Patch\Append; |
|
15
|
|
|
use Spiral\Console\Bootloader\ConsoleBootloader; |
|
16
|
|
|
use Spiral\Scaffolder\Command; |
|
17
|
|
|
use Spiral\Scaffolder\Config\ScaffolderConfig; |
|
18
|
|
|
use Spiral\Scaffolder\Declaration; |
|
19
|
|
|
|
|
20
|
|
|
class ScaffolderBootloader extends Bootloader |
|
21
|
|
|
{ |
|
22
|
|
|
protected const BINDINGS = [ |
|
23
|
|
|
SlugifyInterface::class => Slugify::class, |
|
24
|
|
|
]; |
|
25
|
|
|
|
|
26
|
263 |
|
public function __construct( |
|
27
|
|
|
private readonly ConfiguratorInterface $config, |
|
28
|
|
|
private readonly KernelInterface $kernel |
|
29
|
|
|
) { |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
263 |
|
public function init(ConsoleBootloader $console): void |
|
33
|
|
|
{ |
|
34
|
263 |
|
$console->addCommand(Command\BootloaderCommand::class); |
|
35
|
263 |
|
$console->addCommand(Command\CommandCommand::class); |
|
36
|
263 |
|
$console->addCommand(Command\ConfigCommand::class); |
|
37
|
263 |
|
$console->addCommand(Command\ControllerCommand::class); |
|
38
|
263 |
|
$console->addCommand(Command\JobHandlerCommand::class); |
|
39
|
263 |
|
$console->addCommand(Command\MiddlewareCommand::class); |
|
40
|
|
|
|
|
41
|
|
|
try { |
|
42
|
263 |
|
$defaultNamespace = (new ReflectionClass($this->kernel))->getNamespaceName(); |
|
43
|
|
|
} catch (ReflectionException) { |
|
44
|
|
|
$defaultNamespace = ''; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
263 |
|
$this->config->setDefaults(ScaffolderConfig::CONFIG, [ |
|
48
|
|
|
/* |
|
49
|
|
|
* This is set of comment lines to be applied to every scaffolded file, you can use env() function |
|
50
|
|
|
* to make it developer specific or set one universal pattern per project. |
|
51
|
|
|
*/ |
|
52
|
263 |
|
'header' => [], |
|
53
|
|
|
|
|
54
|
|
|
/* |
|
55
|
|
|
* Base directory for generated classes, class will be automatically localed into sub directory |
|
56
|
|
|
* using given namespace. |
|
57
|
|
|
*/ |
|
58
|
263 |
|
'directory' => directory('app') . 'src/', |
|
59
|
|
|
|
|
60
|
|
|
/* |
|
61
|
|
|
* Default namespace to be applied for every generated class. By default uses Kernel namespace |
|
62
|
|
|
* |
|
63
|
|
|
* Example: 'namespace' => 'MyApplication' |
|
64
|
|
|
* Controllers: MyApplication\Controllers\SampleController |
|
65
|
|
|
*/ |
|
66
|
|
|
'namespace' => $defaultNamespace, |
|
67
|
|
|
|
|
68
|
|
|
/* |
|
69
|
|
|
* This is set of default settings to be used for your scaffolding commands. |
|
70
|
|
|
*/ |
|
71
|
|
|
'declarations' => [ |
|
72
|
263 |
|
Declaration\BootloaderDeclaration::TYPE => [ |
|
73
|
|
|
'namespace' => 'Bootloader', |
|
74
|
|
|
'postfix' => 'Bootloader', |
|
75
|
|
|
'class' => Declaration\BootloaderDeclaration::class, |
|
76
|
|
|
], |
|
77
|
|
|
Declaration\ConfigDeclaration::TYPE => [ |
|
78
|
263 |
|
'namespace' => 'Config', |
|
79
|
|
|
'postfix' => 'Config', |
|
80
|
|
|
'class' => Declaration\ConfigDeclaration::class, |
|
81
|
|
|
'options' => [ |
|
82
|
263 |
|
'directory' => directory('config'), |
|
83
|
|
|
], |
|
84
|
|
|
], |
|
85
|
|
|
Declaration\ControllerDeclaration::TYPE => [ |
|
86
|
|
|
'namespace' => 'Controller', |
|
87
|
|
|
'postfix' => 'Controller', |
|
88
|
|
|
'class' => Declaration\ControllerDeclaration::class, |
|
89
|
|
|
], |
|
90
|
|
|
Declaration\MiddlewareDeclaration::TYPE => [ |
|
91
|
|
|
'namespace' => 'Middleware', |
|
92
|
|
|
'postfix' => '', |
|
93
|
|
|
'class' => Declaration\MiddlewareDeclaration::class, |
|
94
|
|
|
], |
|
95
|
|
|
Declaration\CommandDeclaration::TYPE => [ |
|
96
|
|
|
'namespace' => 'Command', |
|
97
|
|
|
'postfix' => 'Command', |
|
98
|
|
|
'class' => Declaration\CommandDeclaration::class, |
|
99
|
|
|
], |
|
100
|
|
|
Declaration\JobHandlerDeclaration::TYPE => [ |
|
101
|
|
|
'namespace' => 'Job', |
|
102
|
|
|
'postfix' => 'Job', |
|
103
|
|
|
'class' => Declaration\JobHandlerDeclaration::class, |
|
104
|
|
|
], |
|
105
|
|
|
], |
|
106
|
|
|
]); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Register new Scaffolder declaration. |
|
111
|
|
|
*/ |
|
112
|
3 |
|
public function addDeclaration(string $name, array $declaration): void |
|
113
|
|
|
{ |
|
114
|
3 |
|
$this->config->modify( |
|
115
|
|
|
ScaffolderConfig::CONFIG, |
|
116
|
3 |
|
new Append('declarations', $name, $declaration) |
|
117
|
|
|
); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|