Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 858bf31

Browse files
committed
[Workflow] Added a context to Workflow::apply()
1 parent 8ce6f5e commit 858bf31

File tree

9 files changed

+157
-9
lines changed

9 files changed

+157
-9
lines changed

src/Symfony/Component/Workflow/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.3.0
5+
-----
6+
7+
* [Workflow] Added a context to `Workflow::apply()`. The `MethodMarkingStore` could be used to leverage this feature.
8+
49
4.1.0
510
-----
611

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,6 @@ private function createValidator($tag)
5959
return new WorkflowValidator(true);
6060
}
6161

62-
return new WorkflowValidator();
62+
return new WorkflowValidator($tag['single_state'] ?? false);
6363
}
6464
}

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

Lines 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
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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\Marking;
15+
16+
/**
17+
* MethodMarkingStore stores the marking with a subject's method.
18+
*
19+
* This store deals with a "single state" or "multiple state" Marking.
20+
*
21+
* @author Grégoire Pineau <[email protected]>
22+
*/
23+
class MethodMarkingStore implements MarkingStoreInterface
24+
{
25+
private $property;
26+
private $singleState;
27+
28+
/**
29+
* @param string $property Used to determine methods to call
30+
* The `getMarking` method will use `$subject->getProperty()`
31+
* The `setMarking` method will use `$subject->setProperty(string|array $places, array $context = array())`
32+
*/
33+
public function __construct(string $property = 'marking', bool $singleState = false)
34+
{
35+
$this->property = $property;
36+
$this->singleState = $singleState;
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
public function getMarking($subject)
43+
{
44+
$marking = $subject->{'get'.ucfirst($this->property)}();
45+
46+
if (!$marking) {
47+
return new Marking();
48+
}
49+
50+
if ($this->singleState) {
51+
$marking = array($marking => 1);
52+
}
53+
54+
return new Marking($marking);
55+
}
56+
57+
/**
58+
* {@inheritdoc}
59+
*/
60+
public function setMarking($subject, Marking $marking, array $context = array())
61+
{
62+
$marking = $marking->getPlaces();
63+
64+
if ($this->singleState) {
65+
$marking = key($marking);
66+
}
67+
68+
$subject->{'set'.ucfirst($this->property)}($marking, $context);
69+
}
70+
}

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

Lines 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

Lines 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
}
Lines 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('marking', 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('marking', 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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public function buildTransitionBlockerList($subject, string $transitionName): Tr
133133
/**
134134
* {@inheritdoc}
135135
*/
136-
public function apply($subject, $transitionName)
136+
public function apply($subject, $transitionName, array $context = array())
137137
{
138138
$marking = $this->getMarking($subject);
139139

@@ -162,7 +162,7 @@ public function apply($subject, $transitionName)
162162

163163
$this->enter($subject, $transition, $marking);
164164

165-
$this->markingStore->setMarking($subject, $marking);
165+
$this->markingStore->setMarking($subject, $marking, $context);
166166

167167
$this->entered($subject, $transition, $marking);
168168

src/Symfony/Component/Workflow/WorkflowInterface.php

Lines 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)