Bootstrap & Lifecycle
The Bootstrap class handles plugin initialization and cleanup during the plugin lifecycle.
Basic Structure
<?php
namespace Plugins\MyPlugin;
use Psr\Log\LoggerInterface;
class Bootstrap
{
public function __construct(
private readonly LoggerInterface $logger,
) {}
/**
* Called when plugin is enabled
*/
public function initialize(): void
{
$this->logger->info('MyPlugin initialized', [
'plugin' => 'my-plugin',
'version' => '1.0.0'
]);
// Initialization logic here
// DO NOT initialize settings - use migrations instead
}
/**
* Called when plugin is disabled
*/
public function cleanup(): void
{
$this->logger->info('MyPlugin cleanup', [
'plugin' => 'my-plugin'
]);
// Cleanup logic here
// DO NOT delete user data!
// DO NOT drop database tables!
}
}Dependency Injection
Bootstrap supports full dependency injection:
What to Do in initialize()
Good practices:
Log initialization
Validate runtime requirements (PHP extensions, etc.)
Register runtime hooks (if needed)
Perform initial data validation
Trigger initial sync with external services (if needed)
Bad practices:
❌ Create database tables (use migrations instead)
❌ Initialize settings (use migrations instead)
❌ Heavy computations (keep it fast)
❌ Make external API calls (unless absolutely necessary)
❌ Throw exceptions unless truly critical
What to Do in cleanup()
Good practices:
Log cleanup
Cancel scheduled jobs (if using external schedulers)
Close external connections
Clear temporary caches
Notify external services of deactivation
Bad practices:
❌ Delete user data
❌ Drop database tables
❌ Delete plugin settings
❌ Remove migrations
❌ Any destructive operations
Important: Cleanup is NOT uninstall! Users may re-enable the plugin later and expect their data intact.
Error Handling
Bootstrap Registration
Ensure Bootstrap is public in services.yaml:
Plugin Lifecycle
The plugin goes through these states:
DISCOVERED: Plugin found by scanner
REGISTERED: Plugin manifest validated and registered
ENABLED: Plugin active, Bootstrap->initialize() called
DISABLED: Plugin inactive, Bootstrap->cleanup() called
UPDATE_PENDING: Plugin has an available update
FAULTED: Plugin encountered error during initialization
Related Guides
Plugin Manifest - Configure bootstrap_class
Dependencies - Service injection
Entities & Database - Use migrations for setup
Last updated