1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Spiral\Boot\Attribute; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Attribute for marking methods that should be called during the boot phase. |
9
|
|
|
* |
10
|
|
|
* Methods marked with this attribute will be called during the bootloader's |
11
|
|
|
* boot phase, after all initialization methods have been called. |
12
|
|
|
* The boot phase is where you typically configure services, register event listeners, |
13
|
|
|
* or perform other setup tasks. |
14
|
|
|
* |
15
|
|
|
* The priority parameter determines the order in which boot methods are called. |
16
|
|
|
* Higher priority values are executed first. |
17
|
|
|
* |
18
|
|
|
* Example usage: |
19
|
|
|
* ```php |
20
|
|
|
* class MyBootloader extends Bootloader |
21
|
|
|
* { |
22
|
|
|
* // Called during boot phase with default priority (0) |
23
|
|
|
* #[BootMethod] |
24
|
|
|
* public function configureRoutes(RouterInterface $router): void |
25
|
|
|
* { |
26
|
|
|
* $router->addRoute('home', '/'); |
27
|
|
|
* } |
28
|
|
|
* |
29
|
|
|
* // Called during boot phase with high priority (10) |
30
|
|
|
* #[BootMethod(priority: 10)] |
31
|
|
|
* public function configureDatabase(DatabaseInterface $db): void |
32
|
|
|
* { |
33
|
|
|
* $db->setDefaultConnection('default'); |
34
|
|
|
* } |
35
|
|
|
* |
36
|
|
|
* // Called during boot phase with low priority (-10) |
37
|
|
|
* #[BootMethod(priority: -10)] |
38
|
|
|
* public function registerEventListeners(EventDispatcherInterface $dispatcher): void |
39
|
|
|
* { |
40
|
|
|
* $dispatcher->addListener(ApplicationStarted::class, fn() => $this->onStart()); |
41
|
|
|
* } |
42
|
|
|
* } |
43
|
|
|
* ``` |
44
|
|
|
* |
45
|
|
|
* Boot methods are executed after all bootloaders' init methods have been called. |
46
|
|
|
* |
47
|
|
|
* @see InitMethod For methods to be called during initialization phase |
48
|
|
|
*/ |
49
|
|
|
#[\Attribute(\Attribute::TARGET_METHOD)] |
50
|
|
|
final class BootMethod |
51
|
|
|
{ |
52
|
|
|
/** |
53
|
|
|
* @param int $priority The priority of this boot method. Higher values are executed first. |
54
|
|
|
*/ |
55
|
4 |
|
public function __construct( |
56
|
|
|
public readonly int $priority = 0, |
57
|
4 |
|
) {} |
58
|
|
|
} |
59
|
|
|
|