|
4 | 4 |
|
5 | 5 | namespace Spiral\Boot\Attribute; |
6 | 6 |
|
7 | | -use Spiral\Attributes\NamedArgumentConstructor; |
8 | | - |
9 | | -#[\Attribute(\Attribute::TARGET_CLASS), NamedArgumentConstructor] |
| 7 | +/** |
| 8 | + * Attribute to configure bootloader behavior. |
| 9 | + * |
| 10 | + * This attribute allows defining configuration for bootloaders, including |
| 11 | + * constructor arguments, enabling/disabling based on environment variables, |
| 12 | + * and controlling how configuration overrides work. |
| 13 | + * |
| 14 | + * Example usage: |
| 15 | + * ```php |
| 16 | + * // Basic configuration with constructor arguments |
| 17 | + * #[BootloadConfig(args: ['defaultConnection' => 'sqlite'])] |
| 18 | + * class DatabaseBootloader extends Bootloader |
| 19 | + * { |
| 20 | + * public function __construct( |
| 21 | + * private readonly string $defaultConnection |
| 22 | + * ) {} |
| 23 | + * |
| 24 | + * // ... |
| 25 | + * } |
| 26 | + * |
| 27 | + * // Conditionally enable based on environment |
| 28 | + * #[BootloadConfig( |
| 29 | + * allowEnv: ['APP_ENV' => ['local', 'development']], |
| 30 | + * denyEnv: ['TESTING' => [true, 1, 'true']] |
| 31 | + * )] |
| 32 | + * class DevToolsBootloader extends Bootloader |
| 33 | + * { |
| 34 | + * // Only loaded in local or development environments |
| 35 | + * // And not when TESTING is true |
| 36 | + * } |
| 37 | + * |
| 38 | + * // Prevent runtime configuration from overriding attribute configuration |
| 39 | + * #[BootloadConfig(args: ['debug' => true], override: false)] |
| 40 | + * class DebugBootloader extends Bootloader |
| 41 | + * { |
| 42 | + * // The 'debug' argument will always be true, even if different |
| 43 | + * // configuration is provided at runtime |
| 44 | + * } |
| 45 | + * ``` |
| 46 | + * |
| 47 | + * When a bootloader has both runtime configuration and a BootloadConfig attribute, |
| 48 | + * the override parameter controls which configuration takes precedence. |
| 49 | + */ |
| 50 | +#[\Attribute(\Attribute::TARGET_CLASS)] |
10 | 51 | class BootloadConfig |
11 | 52 | { |
| 53 | + /** |
| 54 | + * @param array $args Arguments to pass to the bootloader's constructor. |
| 55 | + * @param bool $enabled Whether this bootloader is enabled. |
| 56 | + * @param array $allowEnv Environment conditions that must be satisfied for the bootloader to be enabled. |
| 57 | + * Format: ['ENV_VAR' => ['allowed_value1', 'allowed_value2']] |
| 58 | + * @param array $denyEnv Environment conditions that must not be satisfied for the bootloader to be enabled. |
| 59 | + * Format: ['ENV_VAR' => ['denied_value1', 'denied_value2']] |
| 60 | + * @param bool $override Whether runtime configuration should override the attribute configuration. |
| 61 | + * If false, attribute configuration takes precedence. |
| 62 | + */ |
12 | 63 | public function __construct( |
13 | 64 | public array $args = [], |
14 | 65 | public bool $enabled = true, |
|
0 commit comments