1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spiral\Scaffolder\Declaration; |
6
|
|
|
|
7
|
|
|
use Nette\PhpGenerator\Literal; |
8
|
|
|
use Spiral\Boot\Bootloader\Bootloader; |
9
|
|
|
use Spiral\Boot\BootloadManager\Methods; |
10
|
|
|
use Spiral\Boot\KernelInterface; |
11
|
|
|
use Spiral\Bootloader\DomainBootloader; |
12
|
|
|
use Spiral\Core\CoreInterface; |
13
|
|
|
use Spiral\Scaffolder\Config\ScaffolderConfig; |
14
|
|
|
|
15
|
|
|
class BootloaderDeclaration extends AbstractDeclaration implements HasInstructions |
16
|
|
|
{ |
17
|
|
|
public const TYPE = 'bootloader'; |
18
|
3 |
|
|
19
|
|
|
public function __construct( |
20
|
|
|
private readonly KernelInterface $kernel, |
21
|
|
|
ScaffolderConfig $config, |
22
|
|
|
string $name, |
23
|
|
|
?string $comment = null, |
24
|
|
|
?string $namespace = null, |
25
|
3 |
|
private readonly bool $isDomain = false, |
26
|
|
|
) { |
27
|
|
|
parent::__construct($config, $name, $comment, $namespace); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
3 |
|
* Declare constants and boot method. |
32
|
|
|
*/ |
33
|
3 |
|
public function declare(): void |
34
|
|
|
{ |
35
|
3 |
|
$extends = $this->isDomain ? DomainBootloader::class : Bootloader::class; |
36
|
3 |
|
|
37
|
|
|
$this->namespace->addUse($extends); |
38
|
3 |
|
$this->class->setExtends($extends); |
39
|
|
|
|
40
|
3 |
|
$this->class->setFinal(); |
41
|
3 |
|
|
42
|
3 |
|
$this->class->addConstant('BINDINGS', [])->setProtected(); |
43
|
|
|
$this->class->addConstant('SINGLETONS', [])->setProtected(); |
44
|
3 |
|
$this->class->addConstant('DEPENDENCIES', [])->setProtected(); |
45
|
1 |
|
|
46
|
1 |
|
if ($this->isDomain) { |
47
|
1 |
|
$this->class->addConstant('INTERCEPTORS', [])->setProtected(); |
48
|
1 |
|
$this->namespace->addUse(CoreInterface::class); |
49
|
1 |
|
$this->class->getConstant('SINGLETONS')->setValue([ |
50
|
|
|
new Literal('CoreInterface::class => [self::class, \'domainCore\']'), |
51
|
|
|
]); |
52
|
3 |
|
} |
53
|
3 |
|
|
54
|
|
|
$this->class->addMethod(Methods::INIT->value)->setReturnType('void'); |
55
|
|
|
$this->class->addMethod(Methods::BOOT->value)->setReturnType('void'); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getInstructions(): array |
59
|
|
|
{ |
60
|
|
|
$kernelClass = (new \ReflectionClass($this->kernel))->getName(); |
61
|
|
|
|
62
|
|
|
return [ |
63
|
|
|
\sprintf('Don\'t forget to add your bootloader to the bootloader\'s list in \'<comment>%s</comment>\' class', $kernelClass), |
64
|
|
|
'Read more about bootloaders in the documentation: https://spiral.dev/docs/framework-bootloaders', |
65
|
|
|
]; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|