Skip to content

Commit

Permalink
all WorkflowEvent events fired now include the start status, the end
Browse files Browse the repository at this point in the history
status and the transitions
  • Loading branch information
raoul2000 committed Oct 3, 2015
1 parent e8111c4 commit 0dd5897
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 206 deletions.
10 changes: 6 additions & 4 deletions CHANGE.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#version 0.0.20
- remove unnecessary use in SimpleWorkflowBehavior
- test against StatusInterface and not anymore Status
- remove unnecessary `use` statement in `SimpleWorkflowBehavior`
- test against `StatusInterface` and not anymore `Status`
- update doc
- minor change in the `WorkflowFileSource->parseStatusId()` method
- minor change in the `WorkflowFileSource->parseStatusId()`
- fix : restore model errors after `getNextStatuses()`

**important notice**
Expand All @@ -18,7 +18,9 @@ $post->initStatus();
```
Note that calling `initStatus()` is usually never done by the developer but by the behavior itself when specific `ActiveRecords` event are fired.

- remove custom validation error message in `WorkflowValidator->init()` (not needed)
- remove custom validation error message in `WorkflowValidator->init()` (not needed)
- when a model changes status, all `WorkflowEvent` events fired now include the **start** status, the **end** status and the **transitions**. This change has been applied
to all Event sequences in `raoul2000\workflow\events`.

#version 0.0.19
- fix GraphmlLoader to not only process ShapeNode elements, and use node label in yEd as Status Id.
Expand Down
15 changes: 10 additions & 5 deletions guide/concept-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,21 @@ class Post extends \yii\db\ActiveRecord
}
```


## Event Object

All events fired are instances of the `raoul2000\workflow\events\WorkflowEvent` class. It provides all the method needed to get informations
All events fired are instances of the `raoul2000\workflow\events\WorkflowEvent` class which provides all the method needed to get informations
on the event that just occured.

TBC
- `getStartStatus()` : returns the Status instance that the model is leaving. If the WorkflowEvent is fired because the model *enters* into a workflow, this method returns `null`.
- `getEndStatus()` : returns the Status instance that the model is reaching. If the WorkflowEvent is fired because the model *leaves* a workflow, this method returns `null`.
- `getTransition()` : returns the Transition instance that the model is performing. Note that if the WorkflowEvent is fired because the model *enters* or *leaves* the
workflow, this method returns `null`.

Remember that a `WorkflowEvent` object is passed to all attached handlers(see next chapter).

## Event Handler

A event handler is used to implement specific process on any of the events in the event sequence. Installing an event handler is
A event handler is used to implement a specific process on any of the events in the event sequence. Installing an event handler is
a standard operation described in the [Yii2 Definitive Guide](http://www.yiiframework.com/doc-2.0/guide-concept-events.html#attaching-event-handlers).

In the example below, we are attaching an handler for the event that is fired when a Post instance goes from the status *draft* to the
Expand All @@ -174,7 +178,7 @@ class Post extends \yii\db\ActiveRecord
[$this, 'sendMail']
);
}

// $event is an instance of raoul2000\workflow\events\WorkflowEvent
public function sendMail($event)
{
MailingService::sendMailToCorrector(
Expand All @@ -183,6 +187,7 @@ class Post extends \yii\db\ActiveRecord
);
}
```


## before vs after

Expand Down
89 changes: 29 additions & 60 deletions src/events/BasicEventSequence.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,29 @@ class BasicEventSequence extends Object implements IEventSequence
*/
public function createEnterWorkflowSequence($initalStatus, $sender)
{
$config = [
'end' => $initalStatus,
'sender' => $sender
];
return [
'before' => [
new WorkflowEvent(
WorkflowEvent::beforeEnterWorkflow($initalStatus->getWorkflowId()),
[
'end' => $initalStatus,
'sender' => $sender
]
$config
),
new WorkflowEvent(
WorkflowEvent::beforeEnterStatus($initalStatus->getId()),
[
'end' => $initalStatus,
'sender' => $sender
]
$config
)
],
'after' => [
new WorkflowEvent(
WorkflowEvent::afterEnterWorkflow($initalStatus->getWorkflowId()),
[
'end' => $initalStatus,
'sender' => $sender
]
$config
),
new WorkflowEvent(
WorkflowEvent::afterEnterStatus($initalStatus->getId()),
[
'end' => $initalStatus,
'sender' => $sender
]
$config
)
]
];
Expand All @@ -73,37 +65,30 @@ public function createEnterWorkflowSequence($initalStatus, $sender)
*/
public function createLeaveWorkflowSequence($finalStatus, $sender)
{
$config = [
'start' => $finalStatus,
'sender' => $sender
];

return [
'before' => [
new WorkflowEvent(
WorkflowEvent::beforeLeaveStatus($finalStatus->getId()),
[
'start' => $finalStatus,
'sender' => $sender
]
$config
),
new WorkflowEvent(
WorkflowEvent::beforeLeaveWorkflow($finalStatus->getWorkflowId()),
[
'start' => $finalStatus,
'sender' => $sender
]
$config
)
],
'after' => [
new WorkflowEvent(
WorkflowEvent::afterLeaveStatus($finalStatus->getId()),
[
'start' => $finalStatus,
'sender' => $sender
]
$config
),
new WorkflowEvent(
WorkflowEvent::afterLeaveWorkflow($finalStatus->getWorkflowId()),
[
'start' => $finalStatus,
'sender' => $sender
]
$config
)
]
];
Expand All @@ -124,55 +109,39 @@ public function createLeaveWorkflowSequence($finalStatus, $sender)
*/
public function createChangeStatusSequence($transition, $sender)
{
$config = [
'start' => $transition->getStartStatus(),
'end' => $transition->getEndStatus(),
'transition' => $transition,
'sender' => $sender
];
return [
'before' => [
new WorkflowEvent(
WorkflowEvent::beforeLeaveStatus($transition->getStartStatus()->getId()),
[
'start' => $transition->getStartStatus(),
'sender' => $sender
]
$config
),
new WorkflowEvent(
WorkflowEvent::beforeChangeStatus($transition->getStartStatus()->getId(), $transition->getEndStatus()->getId()),
[
'start' => $transition->getStartStatus(),
'end' => $transition->getEndStatus(),
'transition' => $transition,
'sender' => $sender
]
$config
),
new WorkflowEvent(
WorkflowEvent::beforeEnterStatus($transition->getEndStatus()->getId()),
[
'end' => $transition->getEndStatus(),
'sender' => $sender
]
$config
)
],
'after' => [
new WorkflowEvent(
WorkflowEvent::afterLeaveStatus($transition->getStartStatus()->getId()),
[
'start' => $transition->getStartStatus(),
'sender' => $sender
]
$config
),
new WorkflowEvent(
WorkflowEvent::afterChangeStatus($transition->getStartStatus()->getId(), $transition->getEndStatus()->getId()),
[
'start' => $transition->getStartStatus(),
'end' => $transition->getEndStatus(),
'transition' => $transition,
'sender' => $sender
]
$config
),
new WorkflowEvent(
WorkflowEvent::afterEnterStatus($transition->getEndStatus()->getId()),
[
'end' => $transition->getEndStatus(),
'sender' => $sender
]
$config
)
]
];
Expand Down
Loading

0 comments on commit 0dd5897

Please sign in to comment.