|
1 | 1 | # Advanced Features |
2 | 2 |
|
| 3 | +## PHP 8.3+ Attributes |
| 4 | + |
| 5 | +Use declarative attributes to configure your workflow actions: |
| 6 | + |
| 7 | +### WorkflowStep Attribute |
| 8 | + |
| 9 | +Define step metadata directly on your action classes: |
| 10 | + |
| 11 | +```php |
| 12 | +<?php |
| 13 | + |
| 14 | +namespace App\Actions; |
| 15 | + |
| 16 | +use SolutionForest\WorkflowEngine\Attributes\WorkflowStep; |
| 17 | +use SolutionForest\WorkflowEngine\Contracts\WorkflowAction; |
| 18 | + |
| 19 | +#[WorkflowStep( |
| 20 | + id: 'send_email', |
| 21 | + name: 'Send Welcome Email', |
| 22 | + description: 'Sends a welcome email to new users', |
| 23 | + config: ['template' => 'welcome', 'async' => true], |
| 24 | + required: true, |
| 25 | + order: 1 |
| 26 | +)] |
| 27 | +class SendWelcomeEmailAction implements WorkflowAction |
| 28 | +{ |
| 29 | + // Action implementation... |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +### Timeout Attribute |
| 34 | + |
| 35 | +Set execution timeouts for your actions: |
| 36 | + |
| 37 | +```php |
| 38 | +use SolutionForest\WorkflowEngine\Attributes\Timeout; |
| 39 | + |
| 40 | +// Single time unit |
| 41 | +#[Timeout(seconds: 30)] |
| 42 | +#[Timeout(minutes: 5)] |
| 43 | +#[Timeout(hours: 1)] |
| 44 | + |
| 45 | +// Combined time units |
| 46 | +#[Timeout(minutes: 5, seconds: 30)] // 5 minutes 30 seconds |
| 47 | +class LongRunningAction implements WorkflowAction |
| 48 | +{ |
| 49 | + // Action will timeout after specified duration |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +### Retry Attribute |
| 54 | + |
| 55 | +Configure automatic retry behavior: |
| 56 | + |
| 57 | +```php |
| 58 | +use SolutionForest\WorkflowEngine\Attributes\Retry; |
| 59 | + |
| 60 | +// Basic retry configuration |
| 61 | +#[Retry(attempts: 3)] |
| 62 | + |
| 63 | +// With backoff strategy |
| 64 | +#[Retry(attempts: 5, backoff: 'exponential')] |
| 65 | +#[Retry(attempts: 3, backoff: 'linear', delay: 1000)] |
| 66 | + |
| 67 | +// Maximum delay cap |
| 68 | +#[Retry(attempts: 5, backoff: 'exponential', delay: 1000, maxDelay: 30000)] |
| 69 | +class UnreliableApiAction implements WorkflowAction |
| 70 | +{ |
| 71 | + // Will automatically retry on failure |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +Available backoff strategies: |
| 76 | +- `linear` - Fixed delay between retries |
| 77 | +- `exponential` - Exponentially increasing delay |
| 78 | +- `fixed` - Same delay for all retries |
| 79 | + |
| 80 | +### Condition Attribute |
| 81 | + |
| 82 | +Add conditional execution rules: |
| 83 | + |
| 84 | +```php |
| 85 | +use SolutionForest\WorkflowEngine\Attributes\Condition; |
| 86 | + |
| 87 | +// Single condition |
| 88 | +#[Condition('user.email is not null')] |
| 89 | + |
| 90 | +// Multiple conditions (repeatable attribute) |
| 91 | +#[Condition('user.email is not null')] |
| 92 | +#[Condition('order.amount > 100')] |
| 93 | +#[Condition('user.premium = true', operator: 'or')] |
| 94 | +class ConditionalAction implements WorkflowAction |
| 95 | +{ |
| 96 | + // Only executes if conditions are met |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +Condition expressions support: |
| 101 | +- Property access: `user.email`, `order.amount` |
| 102 | +- Comparisons: `>`, `<`, `>=`, `<=`, `=`, `!=` |
| 103 | +- Null checks: `is null`, `is not null` |
| 104 | +- Operators: `and`, `or` |
| 105 | + |
| 106 | +### Combining Attributes |
| 107 | + |
| 108 | +You can use multiple attributes together: |
| 109 | + |
| 110 | +```php |
| 111 | +#[WorkflowStep( |
| 112 | + id: 'process_payment', |
| 113 | + name: 'Process Payment', |
| 114 | + description: 'Processes customer payment' |
| 115 | +)] |
| 116 | +#[Timeout(minutes: 2)] |
| 117 | +#[Retry(attempts: 3, backoff: 'exponential')] |
| 118 | +#[Condition('order.amount > 0')] |
| 119 | +#[Condition('payment.method is not null')] |
| 120 | +class ProcessPaymentAction implements WorkflowAction |
| 121 | +{ |
| 122 | + // Robust payment processing with timeout, retries, and conditions |
| 123 | +} |
| 124 | +``` |
| 125 | + |
3 | 126 | ## Error Handling and Retries |
4 | 127 |
|
5 | 128 | ### Automatic Retries |
|
0 commit comments