-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBootstrap.php
More file actions
125 lines (109 loc) · 4.05 KB
/
Copy pathBootstrap.php
File metadata and controls
125 lines (109 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
namespace Plugins\HelloWorld;
use App\Core\Service\Plugin\PluginSettingService;
use Psr\Log\LoggerInterface;
/**
* Bootstrap class for Hello World plugin initialization.
*
* The Bootstrap class is invoked when the plugin is enabled and provides
* a centralized place for:
* - Initializing default settings
* - Registering custom services
* - Setting up event listeners programmatically
* - Performing one-time setup tasks
* - Logging plugin startup
*
* This is optional but recommended for plugins that need custom initialization logic.
* If no bootstrap is needed, this class can be omitted.
*
* The bootstrap class is specified in plugin.json:
* "bootstrap_class": "Plugins\\HelloWorld\\Bootstrap"
*/
class Bootstrap
{
public function __construct(
private readonly PluginSettingService $pluginSettingService,
private readonly LoggerInterface $logger,
) {}
/**
* Initialize the plugin.
*
* This method is called once when the plugin is enabled.
* Use it for:
* - Setting default configuration values
* - Verifying plugin requirements
* - Performing initial data setup
* - Registering custom handlers
*
* @return void
*/
public function initialize(): void
{
$this->logger->info('Hello World plugin: Bootstrap initialization started');
try {
// Note: Default settings are initialized via migration (Version20251214000002.php)
// This ensures settings are properly versioned and tracked with the database schema
// Perform any other setup tasks
$this->setupPluginDefaults();
$this->logger->info('Hello World plugin: Bootstrap initialization completed successfully');
} catch (\Exception $e) {
$this->logger->error('Hello World plugin: Bootstrap initialization failed', [
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
]);
throw $e;
}
}
/**
* Setup plugin-specific defaults.
*
* Add any additional initialization logic here:
* - Create default data in plugin tables
* - Verify external dependencies
* - Register custom handlers
* - Setup caching
*
* @return void
*/
private function setupPluginDefaults(): void
{
// Example: Log plugin version and configuration
$debugMode = (bool) $this->pluginSettingService->get('hello-world', 'debug_mode', false);
if ($debugMode) {
$this->logger->debug('Hello World plugin: Debug mode is enabled');
// In debug mode, log all current settings
$allSettings = [
'greeting_message' => $this->pluginSettingService->get('hello-world', 'greeting_message'),
'enable_widget' => $this->pluginSettingService->get('hello-world', 'enable_widget'),
'enable_statistics' => $this->pluginSettingService->get('hello-world', 'enable_statistics'),
'notification_level' => $this->pluginSettingService->get('hello-world', 'notification_level'),
];
$this->logger->debug('Hello World plugin: Current settings', $allSettings);
}
// Here you could:
// - Verify database tables exist
// - Create initial statistics records
// - Register custom event listeners
// - Setup external API connections
// - Initialize caches
}
/**
* Cleanup method called when plugin is disabled (optional).
*
* Use this for cleanup tasks like:
* - Clearing caches
* - Closing connections
* - Removing temporary data
*
* Note: This is optional and only called if the method exists.
*
* @return void
*/
public function cleanup(): void
{
$this->logger->info('Hello World plugin: Bootstrap cleanup started');
// Perform cleanup tasks
// Don't delete user data or settings - just cleanup temporary resources
$this->logger->info('Hello World plugin: Bootstrap cleanup completed');
}
}