| 1 |
<?php |
| 2 |
|
| 3 |
namespace AATXT\App\Core; |
| 4 |
|
| 5 |
use AATXT\App\CLI\AutoAltTextCommand; |
| 6 |
use AATXT\App\Admin\BulkActions\BulkActionHandler; |
| 7 |
use AATXT\App\Admin\BulkActions\GenerateAltTextBulkAction; |
| 8 |
use AATXT\App\Admin\MediaLibrary; |
| 9 |
use AATXT\App\Infrastructure\Database\ErrorLogSchema; |
| 10 |
use AATXT\App\Services\AltTextService; |
| 11 |
use AATXT\App\Services\ContentAltTextEnricher; |
| 12 |
use AATXT\Vendor\DI\Container as DIContainer; |
| 13 |
|
| 14 |
/** |
| 15 |
* Bootstrap class for the Auto Alt Text plugin. |
| 16 |
* |
| 17 |
* This class orchestrates the plugin initialization by: |
| 18 |
* - Setting up the dependency injection container |
| 19 |
* - Registering bulk actions |
| 20 |
* - Initializing the hooks registrar |
| 21 |
* |
| 22 |
* Replaces the Singleton pattern in the old Setup class. |
| 23 |
*/ |
| 24 |
final class PluginBootstrap |
| 25 |
{ |
| 26 |
/** |
| 27 |
* Dependency injection container |
| 28 |
* |
| 29 |
* @var DIContainer |
| 30 |
*/ |
| 31 |
private $container; |
| 32 |
|
| 33 |
/** |
| 34 |
* Path to the main plugin file |
| 35 |
* |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
private $pluginFile; |
| 39 |
|
| 40 |
/** |
| 41 |
* Constructor |
| 42 |
* |
| 43 |
* @param DIContainer $container The DI container |
| 44 |
* @param string $pluginFile Path to the main plugin file |
| 45 |
*/ |
| 46 |
public function __construct(DIContainer $container, string $pluginFile) |
| 47 |
{ |
| 48 |
$this->container = $container; |
| 49 |
$this->pluginFile = $pluginFile; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Boot the plugin. |
| 54 |
* |
| 55 |
* Initializes all plugin components and registers WordPress hooks. |
| 56 |
* |
| 57 |
* @return void |
| 58 |
*/ |
| 59 |
public function boot(): void |
| 60 |
{ |
| 61 |
// Get services from container |
| 62 |
$altTextService = $this->container->get(AltTextService::class); |
| 63 |
$mediaLibrary = $this->container->get(MediaLibrary::class); |
| 64 |
$contentAltTextEnricher = $this->container->get(ContentAltTextEnricher::class); |
| 65 |
$schema = $this->container->get(ErrorLogSchema::class); |
| 66 |
|
| 67 |
if (defined('WP_CLI') && WP_CLI) { |
| 68 |
\WP_CLI::add_command('auto-alt-text', new AutoAltTextCommand($altTextService)); |
| 69 |
} |
| 70 |
|
| 71 |
// Create plugin lifecycle handler |
| 72 |
$lifecycle = new PluginLifecycle($schema); |
| 73 |
|
| 74 |
// Create and configure bulk action handler |
| 75 |
$bulkActionHandler = $this->createBulkActionHandler($altTextService); |
| 76 |
|
| 77 |
// Create hooks registrar and register all hooks |
| 78 |
$hooksRegistrar = new HooksRegistrar( |
| 79 |
$altTextService, |
| 80 |
$bulkActionHandler, |
| 81 |
$lifecycle, |
| 82 |
$mediaLibrary, |
| 83 |
$contentAltTextEnricher, |
| 84 |
$this->pluginFile |
| 85 |
); |
| 86 |
|
| 87 |
$hooksRegistrar->register(); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Create and configure the bulk action handler. |
| 92 |
* |
| 93 |
* @param AltTextService $altTextService The alt text service |
| 94 |
* @return BulkActionHandler Configured bulk action handler |
| 95 |
*/ |
| 96 |
private function createBulkActionHandler(AltTextService $altTextService): BulkActionHandler |
| 97 |
{ |
| 98 |
$handler = new BulkActionHandler(); |
| 99 |
|
| 100 |
// Register the generate alt text bulk action |
| 101 |
$generateAltTextAction = new GenerateAltTextBulkAction($altTextService); |
| 102 |
$handler->register($generateAltTextAction); |
| 103 |
|
| 104 |
return $handler; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* Static factory method for easy plugin initialization. |
| 109 |
* |
| 110 |
* Usage in main plugin file: |
| 111 |
* ```php |
| 112 |
* PluginBootstrap::init(__FILE__); |
| 113 |
* ``` |
| 114 |
* |
| 115 |
* @param string $pluginFile Path to the main plugin file |
| 116 |
* @return void |
| 117 |
*/ |
| 118 |
public static function init(string $pluginFile): void |
| 119 |
{ |
| 120 |
$container = Container::make(); |
| 121 |
$bootstrap = new self($container, $pluginFile); |
| 122 |
$bootstrap->boot(); |
| 123 |
} |
| 124 |
} |
| 125 |
|