Skip to content

Commit 4f54059

Browse files
committed
refactor: standardize event classes and improve README documentation
- Standardize WorkflowCompletedEvent and WorkflowFailedEvent to use `final readonly class` with constructor promotion, matching all other event classes. Added helper methods (getWorkflowId, getWorkflowName, getErrorMessage) for consistency. - README improvements: - Remove broken /docs link that pointed to non-existent directory - Document SimpleWorkflow helper with usage examples - Document getInstances() filter options (state, definition_name, created_after, created_before, limit, offset) - Document getProgress() and getStatusSummary() on WorkflowInstance - Add built-in actions table (LogAction, ConditionAction, etc.) - Add WorkflowState helper methods section (isActive, color, icon, etc.) - Clarify PHP 8.3+ attributes are metadata annotations, not yet auto-parsed by the engine at runtime https://claude.ai/code/session_013CdFJtmtMmSqBZFwdrmicG
1 parent 4218973 commit 4f54059

File tree

3 files changed

+111
-19
lines changed

3 files changed

+111
-19
lines changed

README.md

Lines changed: 84 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ $workflow = WorkflowBuilder::quick()->documentApproval();
129129

130130
### PHP 8.3+ Attributes
131131

132-
Use native PHP attributes to configure actions with retry, timeout, and conditions:
132+
> **Note:** Attributes are currently metadata annotations for documentation and tooling. They are not yet auto-parsed by the engine at runtime — use the builder API (`timeout:`, `retryAttempts:`, `when()`) or step config to apply these behaviors. Attribute-driven execution is planned for a future release.
133+
134+
Use native PHP attributes to annotate actions with retry, timeout, and conditions:
133135

134136
#### Retry Logic
135137
```php
@@ -214,14 +216,57 @@ $workflow = WorkflowBuilder::create('conditional-flow')
214216
### Workflow Lifecycle Management
215217

216218
```php
217-
// Start, pause, resume, cancel
219+
// Start, resume, cancel
218220
$instanceId = $engine->start('my-workflow', $definition->toArray(), ['key' => 'value']);
219221
$instance = $engine->getInstance($instanceId);
222+
$engine->resume($instanceId);
220223
$engine->cancel($instanceId, 'No longer needed');
221224

222-
// Query instances
223-
$instances = $engine->getInstances(['state' => 'running']);
224-
$status = $engine->getStatus('my-workflow');
225+
// Track progress
226+
$progress = $instance->getProgress(); // 0.0 to 100.0
227+
$summary = $instance->getStatusSummary();
228+
229+
// Query instances with filters
230+
$instances = $engine->getInstances([
231+
'state' => 'running',
232+
'definition_name' => 'order-processing',
233+
'created_after' => new \DateTime('-7 days'),
234+
'limit' => 50,
235+
'offset' => 0,
236+
]);
237+
```
238+
239+
### SimpleWorkflow Helper
240+
241+
For quick workflow execution without manual engine setup:
242+
243+
```php
244+
use SolutionForest\WorkflowEngine\Support\SimpleWorkflow;
245+
246+
$simple = new SimpleWorkflow($storageAdapter);
247+
248+
// Run actions sequentially
249+
$instanceId = $simple->sequential('user-onboarding', [
250+
SendWelcomeEmailAction::class,
251+
CreateUserProfileAction::class,
252+
AssignDefaultRoleAction::class,
253+
], ['user_id' => 123]);
254+
255+
// Run a single action as a workflow
256+
$instanceId = $simple->runAction(SendEmailAction::class, [
257+
'to' => 'user@example.com',
258+
'subject' => 'Welcome!',
259+
]);
260+
261+
// Execute from a builder
262+
$builder = WorkflowBuilder::create('custom-flow')
263+
->addStep('validate', ValidateAction::class)
264+
->addStep('process', ProcessAction::class);
265+
$instanceId = $simple->executeBuilder($builder, $context);
266+
267+
// Check status
268+
$status = $simple->getStatus($instanceId);
269+
// Returns: id, state, current_step, progress, completed_steps, failed_steps, error_message, ...
225270
```
226271

227272
## 🏗️ Architecture
@@ -280,6 +325,37 @@ State transitions are validated at runtime — invalid transitions throw `Invali
280325
| `Exceptions\` | WorkflowException, InvalidWorkflowDefinitionException, InvalidWorkflowStateException, ActionNotFoundException, StepExecutionException, WorkflowInstanceNotFoundException |
281326
| `Support\` | NullLogger, NullEventDispatcher, SimpleWorkflow, Uuid, Timeout, ConditionEvaluator, Arr |
282327

328+
### Built-in Actions
329+
330+
Six ready-to-use actions are included:
331+
332+
| Action | Purpose | Config Keys |
333+
|--------|---------|-------------|
334+
| **LogAction** | Log messages with placeholder replacement (`{user.name}`) | `message`, `level` (debug/info/warning/error) |
335+
| **EmailAction** | Mock email sending with template support | `to`, `subject`, `body`, `template` |
336+
| **HttpAction** | HTTP requests with `{{ variable }}` template variables | `url`, `method`, `headers`, `body` |
337+
| **DelayAction** | Pause execution for a specified duration | `seconds`, `minutes`, `hours` |
338+
| **ConditionAction** | Evaluate boolean expressions and branch (`on_true`/`on_false`) | `condition`, `on_true`, `on_false` |
339+
| **BaseAction** | Abstract base class for custom actions ||
340+
341+
### WorkflowState Helpers
342+
343+
The `WorkflowState` enum provides utility methods for UI and logic:
344+
345+
```php
346+
$state = $instance->getState();
347+
348+
$state->isActive(); // true for PENDING, RUNNING, WAITING, PAUSED
349+
$state->isFinished(); // true for COMPLETED, FAILED, CANCELLED
350+
$state->isSuccessful(); // true for COMPLETED
351+
$state->isError(); // true for FAILED
352+
$state->label(); // "Running"
353+
$state->description(); // "The workflow is actively executing steps..."
354+
$state->color(); // "blue" (gray, blue, yellow, orange, green, red, purple)
355+
$state->icon(); // "▶️"
356+
$state->canTransitionTo(WorkflowState::COMPLETED); // bool
357+
$state->getValidTransitions(); // [WorkflowState::WAITING, ...]
358+
```
283359

284360
## 🔧 Configuration
285361

@@ -436,7 +512,6 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file
436512

437513
## 🔗 Links
438514

439-
- [Documentation](https://github.com/solution-forest/workflow-engine-core/docs)
440-
- [Issues](https://github.com/solution-forest/workflow-engine-core/issues)
441-
- [Changelog](https://github.com/solution-forest/workflow-engine-core/blob/main/CHANGELOG.md)
442-
- [Laravel Integration](https://github.com/solution-forest/workflow-engine-laravel)
515+
- [Issues](https://github.com/solutionforest/workflow-engine-core/issues)
516+
- [Changelog](https://github.com/solutionforest/workflow-engine-core/blob/main/CHANGELOG.md)
517+
- [Laravel Integration](https://github.com/solutionforest/workflow-engine-laravel)

src/Events/WorkflowCompletedEvent.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@
44

55
use SolutionForest\WorkflowEngine\Core\WorkflowInstance;
66

7-
class WorkflowCompletedEvent
7+
final readonly class WorkflowCompletedEvent
88
{
9-
public WorkflowInstance $instance;
9+
public function __construct(
10+
public WorkflowInstance $instance,
11+
) {}
1012

11-
public function __construct(WorkflowInstance $instance)
13+
public function getWorkflowId(): string
1214
{
13-
$this->instance = $instance;
15+
return $this->instance->getId();
16+
}
17+
18+
public function getWorkflowName(): string
19+
{
20+
return $this->instance->getDefinition()->getName();
1421
}
1522
}

src/Events/WorkflowFailedEvent.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,25 @@
44

55
use SolutionForest\WorkflowEngine\Core\WorkflowInstance;
66

7-
class WorkflowFailedEvent
7+
final readonly class WorkflowFailedEvent
88
{
9-
public WorkflowInstance $instance;
9+
public function __construct(
10+
public WorkflowInstance $instance,
11+
public \Exception $exception,
12+
) {}
1013

11-
public \Exception $exception;
14+
public function getWorkflowId(): string
15+
{
16+
return $this->instance->getId();
17+
}
18+
19+
public function getWorkflowName(): string
20+
{
21+
return $this->instance->getDefinition()->getName();
22+
}
1223

13-
public function __construct(WorkflowInstance $instance, \Exception $exception)
24+
public function getErrorMessage(): string
1425
{
15-
$this->instance = $instance;
16-
$this->exception = $exception;
26+
return $this->exception->getMessage();
1727
}
1828
}

0 commit comments

Comments
 (0)