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

Skip to content

Commit 15d04c1

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

File tree

10 files changed

+161
-9
lines changed

10 files changed

+161
-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($places, $context)`
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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Symfony\Component\Workflow\Tests\Fixtures;
4+
5+
final class Subject
6+
{
7+
private $marking;
8+
9+
public function __construct($marking = null)
10+
{
11+
$this->marking = $marking;
12+
}
13+
14+
public function getMarking()
15+
{
16+
return $this->marking;
17+
}
18+
19+
public function setMarking($marking)
20+
{
21+
$this->marking = $marking;
22+
}
23+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
use Symfony\Component\Workflow\Tests\Fixtures\Subject;
9+
10+
class MethodMarkingStoreTest extends TestCase
11+
{
12+
public function testGetSetMarkingWithMultipleState()
13+
{
14+
$subject = new Subject();
15+
16+
$markingStore = new MethodMarkingStore('marking', false);
17+
18+
$marking = $markingStore->getMarking($subject);
19+
20+
$this->assertInstanceOf(Marking::class, $marking);
21+
$this->assertCount(0, $marking->getPlaces());
22+
23+
$marking->mark('first_place');
24+
25+
$markingStore->setMarking($subject, $marking);
26+
27+
$this->assertSame(array('first_place' => 1), $subject->getMarking());
28+
29+
$marking2 = $markingStore->getMarking($subject);
30+
31+
$this->assertEquals($marking, $marking2);
32+
}
33+
34+
public function testGetSetMarkingWithSingleState()
35+
{
36+
$subject = new Subject();
37+
38+
$markingStore = new MethodMarkingStore('marking', true);
39+
40+
$marking = $markingStore->getMarking($subject);
41+
42+
$this->assertInstanceOf(Marking::class, $marking);
43+
$this->assertCount(0, $marking->getPlaces());
44+
45+
$marking->mark('first_place');
46+
47+
$markingStore->setMarking($subject, $marking);
48+
49+
$this->assertSame('first_place', $subject->getMarking());
50+
51+
$marking2 = $markingStore->getMarking($subject);
52+
53+
$this->assertEquals($marking, $marking2);
54+
}
55+
}

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)