Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b2cbe22

Browse filesBrowse files
committed
[Workflow] Added a context to Workflow::apply()
1 parent c7fe1b6 commit b2cbe22
Copy full SHA for b2cbe22

File tree

13 files changed

+176
-13
lines changed
Filter options

13 files changed

+176
-13
lines changed

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ private function addWorkflowSection(ArrayNodeDefinition $rootNode)
267267
->fixXmlConfig('argument')
268268
->children()
269269
->enumNode('type')
270-
->values(array('multiple_state', 'single_state'))
270+
->values(array('multiple_state', 'single_state', 'method'))
271271
->end()
272272
->arrayNode('arguments')
273273
->beforeNormalization()

‎src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,8 @@ private function registerWorkflowConfiguration(array $config, ContainerBuilder $
594594
$definitionDefinition->addTag('workflow.definition', array(
595595
'name' => $name,
596596
'type' => $type,
597-
'marking_store' => isset($workflow['marking_store']['type']) ? $workflow['marking_store']['type'] : null,
597+
'marking_store' => $workflow['marking_store']['type'] ?? null,
598+
'single_state' => 'method' === ($workflow['marking_store']['type'] ?? null) && ($workflow['marking_store']['arguments'][0] ?? false),
598599
));
599600

600601
// Create MarkingStore

‎src/Symfony/Bundle/FrameworkBundle/Resources/config/workflow.xml

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/workflow.xml
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
<service id="workflow.marking_store.multiple_state" class="Symfony\Component\Workflow\MarkingStore\MultipleStateMarkingStore" abstract="true" />
2424
<service id="workflow.marking_store.single_state" class="Symfony\Component\Workflow\MarkingStore\SingleStateMarkingStore" abstract="true" />
25+
<service id="workflow.marking_store.method" class="Symfony\Component\Workflow\MarkingStore\MethodMarkingStore" abstract="true" />
2526

2627
<service id="workflow.registry" class="Symfony\Component\Workflow\Registry" />
2728
<service id="Symfony\Component\Workflow\Registry" alias="workflow.registry" />

‎src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ public function testWorkflows()
215215
$workflowDefinition->getArgument(0),
216216
'Places are passed to the workflow definition'
217217
);
218-
$this->assertSame(array('workflow.definition' => array(array('name' => 'article', 'type' => 'workflow', 'marking_store' => 'multiple_state'))), $workflowDefinition->getTags());
218+
$this->assertSame(array('workflow.definition' => array(array('name' => 'article', 'type' => 'workflow', 'marking_store' => 'multiple_state', 'single_state' => false))), $workflowDefinition->getTags());
219219
$this->assertCount(4, $workflowDefinition->getArgument(1));
220220
$this->assertSame('draft', $workflowDefinition->getArgument(2));
221221

@@ -237,7 +237,7 @@ public function testWorkflows()
237237
$stateMachineDefinition->getArgument(0),
238238
'Places are passed to the state machine definition'
239239
);
240-
$this->assertSame(array('workflow.definition' => array(array('name' => 'pull_request', 'type' => 'state_machine', 'marking_store' => 'single_state'))), $stateMachineDefinition->getTags());
240+
$this->assertSame(array('workflow.definition' => array(array('name' => 'pull_request', 'type' => 'state_machine', 'marking_store' => 'single_state', 'single_state' => false))), $stateMachineDefinition->getTags());
241241
$this->assertCount(9, $stateMachineDefinition->getArgument(1));
242242
$this->assertSame('start', $stateMachineDefinition->getArgument(2));
243243

‎src/Symfony/Component/Workflow/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Workflow/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

77
* Trigger `entered` event for subject entering in the Workflow for the first time
8+
* Added a context to `Workflow::apply()`. The `MethodMarkingStore` could be used to leverage this feature.
89

910
4.1.0
1011
-----

‎src/Symfony/Component/Workflow/DependencyInjection/ValidateWorkflowsPass.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Workflow/DependencyInjection/ValidateWorkflowsPass.php
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ private function createValidator($tag)
5959
return new WorkflowValidator(true);
6060
}
6161

62-
return new WorkflowValidator();
62+
if ('multiple_state' === $tag['marking_store']) {
63+
return new WorkflowValidator(false);
64+
}
65+
66+
return new WorkflowValidator($tag['single_state'] ?? false);
6367
}
6468
}

‎src/Symfony/Component/Workflow/MarkingStore/MarkingStoreInterface.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Workflow/MarkingStore/MarkingStoreInterface.php
+2-3Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ public function getMarking($subject);
3636
/**
3737
* Sets a Marking to a subject.
3838
*
39-
* @param object $subject A subject
40-
* @param Marking $marking A marking
39+
* @param object $subject A subject
4140
*/
42-
public function setMarking($subject, Marking $marking);
41+
public function setMarking($subject, Marking $marking, array $context = array());
4342
}
+83Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Workflow\MarkingStore;
13+
14+
use Symfony\Component\Workflow\Exception\LogicException;
15+
use Symfony\Component\Workflow\Marking;
16+
17+
/**
18+
* MethodMarkingStore stores the marking with a subject's method.
19+
*
20+
* This store deals with a "single state" or "multiple state" Marking.
21+
*
22+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
23+
*/
24+
class MethodMarkingStore implements MarkingStoreInterface
25+
{
26+
private $singleState;
27+
private $property;
28+
29+
/**
30+
* @param string $property Used to determine methods to call
31+
* The `getMarking` method will use `$subject->getProperty()`
32+
* The `setMarking` method will use `$subject->setProperty(string|array $places, array $context = array())`
33+
*/
34+
public function __construct(bool $singleState = false, string $property = 'marking')
35+
{
36+
$this->singleState = $singleState;
37+
$this->property = $property;
38+
}
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public function getMarking($subject)
44+
{
45+
$method = 'get'.ucfirst($this->property);
46+
47+
if (!method_exists($subject, $method)) {
48+
throw new LogicException(sprintf('The method "%s::%s()" does not exists.', \get_class($subject), $method));
49+
}
50+
51+
$marking = $subject->{$method}();
52+
53+
if (!$marking) {
54+
return new Marking();
55+
}
56+
57+
if ($this->singleState) {
58+
$marking = array($marking => 1);
59+
}
60+
61+
return new Marking($marking);
62+
}
63+
64+
/**
65+
* {@inheritdoc}
66+
*/
67+
public function setMarking($subject, Marking $marking, array $context = array())
68+
{
69+
$marking = $marking->getPlaces();
70+
71+
if ($this->singleState) {
72+
$marking = key($marking);
73+
}
74+
75+
$method = 'set'.ucfirst($this->property);
76+
77+
if (!method_exists($subject, $method)) {
78+
throw new LogicException(sprintf('The method "%s::%s()" does not exists.', \get_class($subject), $method));
79+
}
80+
81+
$subject->{$method}($marking, $context);
82+
}
83+
}

‎src/Symfony/Component/Workflow/MarkingStore/MultipleStateMarkingStore.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Workflow/MarkingStore/MultipleStateMarkingStore.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function getMarking($subject)
4646
/**
4747
* {@inheritdoc}
4848
*/
49-
public function setMarking($subject, Marking $marking)
49+
public function setMarking($subject, Marking $marking, array $context = array())
5050
{
5151
$this->propertyAccessor->setValue($subject, $this->property, $marking->getPlaces());
5252
}

‎src/Symfony/Component/Workflow/MarkingStore/SingleStateMarkingStore.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Workflow/MarkingStore/SingleStateMarkingStore.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function getMarking($subject)
5151
/**
5252
* {@inheritdoc}
5353
*/
54-
public function setMarking($subject, Marking $marking)
54+
public function setMarking($subject, Marking $marking, array $context = array())
5555
{
5656
$this->propertyAccessor->setValue($subject, $this->property, key($marking->getPlaces()));
5757
}
+74Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
namespace Symfony\Component\Workflow\Tests\MarkingStore;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Symfony\Component\Workflow\Marking;
7+
use Symfony\Component\Workflow\MarkingStore\MethodMarkingStore;
8+
9+
class MethodMarkingStoreTest extends TestCase
10+
{
11+
public function testGetSetMarkingWithMultipleState()
12+
{
13+
$subject = new Subject();
14+
15+
$markingStore = new MethodMarkingStore(false);
16+
17+
$marking = $markingStore->getMarking($subject);
18+
19+
$this->assertInstanceOf(Marking::class, $marking);
20+
$this->assertCount(0, $marking->getPlaces());
21+
22+
$marking->mark('first_place');
23+
24+
$markingStore->setMarking($subject, $marking);
25+
26+
$this->assertSame(array('first_place' => 1), $subject->getMarking());
27+
28+
$marking2 = $markingStore->getMarking($subject);
29+
30+
$this->assertEquals($marking, $marking2);
31+
}
32+
33+
public function testGetSetMarkingWithSingleState()
34+
{
35+
$subject = new Subject();
36+
37+
$markingStore = new MethodMarkingStore(true);
38+
39+
$marking = $markingStore->getMarking($subject);
40+
41+
$this->assertInstanceOf(Marking::class, $marking);
42+
$this->assertCount(0, $marking->getPlaces());
43+
44+
$marking->mark('first_place');
45+
46+
$markingStore->setMarking($subject, $marking);
47+
48+
$this->assertSame('first_place', $subject->getMarking());
49+
50+
$marking2 = $markingStore->getMarking($subject);
51+
52+
$this->assertEquals($marking, $marking2);
53+
}
54+
}
55+
56+
final class Subject
57+
{
58+
private $marking;
59+
60+
public function __construct($marking = null)
61+
{
62+
$this->marking = $marking;
63+
}
64+
65+
public function getMarking()
66+
{
67+
return $this->marking;
68+
}
69+
70+
public function setMarking($marking)
71+
{
72+
$this->marking = $marking;
73+
}
74+
}

‎src/Symfony/Component/Workflow/Workflow.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Workflow/Workflow.php
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public function buildTransitionBlockerList($subject, string $transitionName): Tr
143143
/**
144144
* {@inheritdoc}
145145
*/
146-
public function apply($subject, $transitionName)
146+
public function apply($subject, $transitionName, array $context = array())
147147
{
148148
$marking = $this->getMarking($subject);
149149

@@ -172,7 +172,7 @@ public function apply($subject, $transitionName)
172172

173173
$this->enter($subject, $transition, $marking);
174174

175-
$this->markingStore->setMarking($subject, $marking);
175+
$this->markingStore->setMarking($subject, $marking, $context);
176176

177177
$this->entered($subject, $transition, $marking);
178178

‎src/Symfony/Component/Workflow/WorkflowInterface.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Workflow/WorkflowInterface.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function buildTransitionBlockerList($subject, string $transitionName): Tr
5858
*
5959
* @throws LogicException If the transition is not applicable
6060
*/
61-
public function apply($subject, $transitionName);
61+
public function apply($subject, $transitionName, array $context = array());
6262

6363
/**
6464
* Returns all enabled transitions.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.