|
| 1 | +.. index:: |
| 2 | + single: Workflow; Workflows as State Machines |
| 3 | + |
| 4 | +Workflows as State Machines |
| 5 | +=========================== |
| 6 | + |
| 7 | +The workflow component is modelled after a *Workflow net* which is a subclass |
| 8 | +of a `Petri net`_. By adding further restrictions you can get a state machine. |
| 9 | +The most important one being that a state machine cannot be in more than |
| 10 | +one place simultaneously. It is also worth noting that a workflow does not |
| 11 | +commonly have cyclic path in the definition graph, but it is common for a state |
| 12 | +machine. |
| 13 | + |
| 14 | +Example of a State Machine |
| 15 | +-------------------------- |
| 16 | + |
| 17 | +A pull request starts in an intial "start" state, a state for e.g. running |
| 18 | +tests on Travis. When this is finished, the pull request is in the "review" |
| 19 | +state, where contributors can require changes, reject or accept the |
| 20 | +pull request. At any time, you can also "update" the pull request, which |
| 21 | +will result in another Travis run. |
| 22 | + |
| 23 | +.. image:: /_images/components/workflow/pull_request.png |
| 24 | + |
| 25 | +Below is the configuration for the pull request state machine. |
| 26 | + |
| 27 | +.. configuration-block:: |
| 28 | + |
| 29 | + .. code-block:: yaml |
| 30 | +
|
| 31 | + # app/config/config.yml |
| 32 | + framework: |
| 33 | + workflows: |
| 34 | + pull_request: |
| 35 | + type: 'state_machine' |
| 36 | + supports: |
| 37 | + - AppBundle\Entity\PullRequest |
| 38 | + places: |
| 39 | + - start |
| 40 | + - coding |
| 41 | + - travis |
| 42 | + - review |
| 43 | + - merged |
| 44 | + - closed |
| 45 | + transitions: |
| 46 | + submit: |
| 47 | + from: start |
| 48 | + to: travis |
| 49 | + update: |
| 50 | + from: [coding, travis, review] |
| 51 | + to: travis |
| 52 | + wait_for_reivew: |
| 53 | + from: travis |
| 54 | + to: review |
| 55 | + request_change: |
| 56 | + from: review |
| 57 | + to: coding |
| 58 | + accept: |
| 59 | + from: review |
| 60 | + to: merged |
| 61 | + reject: |
| 62 | + from: review |
| 63 | + to: closed |
| 64 | + reopen: |
| 65 | + from: closed |
| 66 | + to: review |
| 67 | +
|
| 68 | + .. code-block:: xml |
| 69 | +
|
| 70 | + <!-- app/config/config.xml --> |
| 71 | + <?xml version="1.0" encoding="utf-8" ?> |
| 72 | + <container xmlns="http://symfony.com/schema/dic/services" |
| 73 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 74 | + xmlns:framework="http://symfony.com/schema/dic/symfony" |
| 75 | + xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd |
| 76 | + http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd" |
| 77 | + > |
| 78 | +
|
| 79 | + <framework:config> |
| 80 | + <framework:workflow name="pull_request" type="state_machine"> |
| 81 | + <framework:marking-store type="single_state"/> |
| 82 | +
|
| 83 | + <framework:support>AppBundle\Entity\PullRequest</framework:support> |
| 84 | +
|
| 85 | + <framework:place>start</framework:place> |
| 86 | + <framework:place>coding</framework:place> |
| 87 | + <framework:place>travis</framework:place> |
| 88 | + <framework:place>review</framework:place> |
| 89 | + <framework:place>merged</framework:place> |
| 90 | + <framework:place>closed</framework:place> |
| 91 | +
|
| 92 | + <framework:transition name="submit"> |
| 93 | + <framework:from>start</framework:from> |
| 94 | +
|
| 95 | + <framework:to>travis</framework:to> |
| 96 | + </framework:transition> |
| 97 | +
|
| 98 | + <framework:transition name="update"> |
| 99 | + <framework:from>coding</framework:from> |
| 100 | + <framework:from>travis</framework:from> |
| 101 | + <framework:from>review</framework:from> |
| 102 | +
|
| 103 | + <framework:to>travis</framework:to> |
| 104 | + </framework:transition> |
| 105 | +
|
| 106 | + <framework:transition name="wait_for_review"> |
| 107 | + <framework:from>travis</framework:from> |
| 108 | +
|
| 109 | + <framework:to>review</framework:to> |
| 110 | + </framework:transition> |
| 111 | +
|
| 112 | + <framework:transition name="request_change"> |
| 113 | + <framework:from>review</framework:from> |
| 114 | +
|
| 115 | + <framework:to>coding</framework:to> |
| 116 | + </framework:transition> |
| 117 | +
|
| 118 | + <framework:transition name="accept"> |
| 119 | + <framework:from>review</framework:from> |
| 120 | +
|
| 121 | + <framework:to>merged</framework:to> |
| 122 | + </framework:transition> |
| 123 | +
|
| 124 | + <framework:transition name="reject"> |
| 125 | + <framework:from>review</framework:from> |
| 126 | +
|
| 127 | + <framework:to>closed</framework:to> |
| 128 | + </framework:transition> |
| 129 | +
|
| 130 | + <framework:transition name="reopen"> |
| 131 | + <framework:from>closed</framework:from> |
| 132 | +
|
| 133 | + <framework:to>review</framework:to> |
| 134 | + </framework:transition> |
| 135 | +
|
| 136 | + </framework:workflow> |
| 137 | +
|
| 138 | + </framework:config> |
| 139 | + </container> |
| 140 | +
|
| 141 | + .. code-block:: php |
| 142 | +
|
| 143 | + // app/config/config.php |
| 144 | +
|
| 145 | + $container->loadFromExtension('framework', array( |
| 146 | + // ... |
| 147 | + 'workflows' => array( |
| 148 | + 'pull_request' => array( |
| 149 | + 'type' => 'state_machine', |
| 150 | + 'supports' => array('AppBundle\Entity\PullRequest'), |
| 151 | + 'places' => array( |
| 152 | + 'start', |
| 153 | + 'coding', |
| 154 | + 'travis', |
| 155 | + 'review', |
| 156 | + 'merged', |
| 157 | + 'closed', |
| 158 | + ), |
| 159 | + 'transitions' => array( |
| 160 | + 'start'=> array( |
| 161 | + 'form' => 'start', |
| 162 | + 'to' => 'travis', |
| 163 | + ), |
| 164 | + 'update'=> array( |
| 165 | + 'form' => array('coding','travis','review'), |
| 166 | + 'to' => 'travis', |
| 167 | + ), |
| 168 | + 'wait_for_reivew'=> array( |
| 169 | + 'form' => 'travis', |
| 170 | + 'to' => 'review', |
| 171 | + ), |
| 172 | + 'request_change'=> array( |
| 173 | + 'form' => 'review', |
| 174 | + 'to' => 'coding', |
| 175 | + ), |
| 176 | + 'accept'=> array( |
| 177 | + 'form' => 'review', |
| 178 | + 'to' => 'merged', |
| 179 | + ), |
| 180 | + 'reject'=> array( |
| 181 | + 'form' => 'review', |
| 182 | + 'to' => 'closed', |
| 183 | + ), |
| 184 | + 'reopen'=> array( |
| 185 | + 'form' => 'start', |
| 186 | + 'to' => 'review', |
| 187 | + ), |
| 188 | + ), |
| 189 | + ), |
| 190 | + ), |
| 191 | + )); |
| 192 | +
|
| 193 | +You can now use this state machine by getting the ``state_machine.pull_request`` service:: |
| 194 | + |
| 195 | + $stateMachine = $this->container->get('state_machine.pull_request'); |
| 196 | + |
| 197 | +.. _Petri net: https://en.wikipedia.org/wiki/Petri_net |
0 commit comments