项目作者: MateusGabi

项目描述 :
Using workflows to dev your apps
高级语言: JavaScript
项目地址: git://github.com/MateusGabi/Workflow-Strategy.git
创建时间: 2017-12-19T11:16:22Z
项目社区:https://github.com/MateusGabi/Workflow-Strategy

开源协议:MIT License

下载


Worflow Strategy

Using workflows to dev your apps

Hello bro 🖖,

Workflow Strategy (WS) is not a framework, it’s an methodology. WS has three classes: Strategy, Workflow and WorkflowArtifact. Of Course you can copy-and-paste our files.

This classes are generics and you shall create concret-class that inherit WS Classes (See examples).

Simple Example

A workflow encapsulates an object of interest. For example, if you have a sales process (SalesWorkflow), you have a single object of type Sale that will be manipulated. Sale class must extends WorkflowArtifact.

  1. class SalesWorkflow extends Workflow {
  2. // array of user roles
  3. protected $places = (...);
  4. // see examples
  5. protected $transitions = (...);
  6. function authorize($action) : boolean {
  7. return true;
  8. }
  9. // handlers methods..
  10. }
  1. class Sale extends WorkflowArtifact {
  2. protected $foo;
  3. protected $bar;
  4. public function __construct($foo, $bar) {
  5. //!important
  6. parent::__construct();
  7. $this->foo = $foo;
  8. $this->bar = $bar;
  9. }
  10. }

In order to keep the business rules encapsulated in a single object, there is Strategy object. Strategy represents a solution to a particular problem. This solution is a workflow and an artifact.

  1. class SaleStrategy extends Strategy {
  2. protected $workflow;
  3. function __construct() {
  4. $this->workflow = new SalesWorflow();
  5. }
  6. public function foo($bar) {
  7. $sale = new Sale($bar);
  8. $this->workflow->setArtifact($sale);
  9. // goes ahead (next status)
  10. $this->workflow->next();
  11. // goes back (previous status)
  12. $this->workflow->previous();
  13. // force ending (end status)
  14. $this->workflow->finish();
  15. }
  16. }

Artifacts change state as the process progresses, for this, we have handlers methods. When an WorkflowArtifact change status, an handler ixecuted.

  1. class SaleWorkflow extends Workflow {
  2. //...
  3. protected $transitions = [
  4. 'old_status' => [
  5. 'from' => 'a',
  6. 'to' => 'b'
  7. ],
  8. 'new_status' => [
  9. 'from' => 'b',
  10. 'to' => 'c'
  11. ]
  12. ];
  13. function onChangeFromOldStatusToNewStatus() {
  14. //...
  15. }
  16. }

License

MIT