Skip to content

Commit 3cd0a0d

Browse files
committed
[Workflow] Add support for storing the marking in a property
1 parent 510e51f commit 3cd0a0d

File tree

5 files changed

+222
-22
lines changed

5 files changed

+222
-22
lines changed

src/Symfony/Component/Workflow/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Add `with-metadata` option to the `workflow:dump` command to include places,
88
transitions and workflow's metadata into dumped graph
9+
* Add support for storing marking in a property
910

1011
6.2
1112
---

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

Lines changed: 76 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,45 +15,41 @@
1515
use Symfony\Component\Workflow\Marking;
1616

1717
/**
18-
* MethodMarkingStore stores the marking with a subject's method.
18+
* MethodMarkingStore stores the marking with a subject's public method if
19+
* exist, then to a property.
1920
*
2021
* This store deals with a "single state" or "multiple state" Marking.
2122
*
2223
* "single state" Marking means a subject can be in one and only one state at
23-
* the same time. Use it with state machine.
24+
* the same time. Use it with state machine. It uses a string to store the
25+
* marking.
2426
*
2527
* "multiple state" Marking means a subject can be in many states at the same
26-
* time. Use it with workflow.
28+
* time. Use it with workflow. It uses an array of strings to store the marking.
2729
*
2830
* @author Grégoire Pineau <lyrixx@lyrixx.info>
2931
*/
3032
final class MethodMarkingStore implements MarkingStoreInterface
3133
{
32-
private bool $singleState;
33-
private string $property;
34+
private array $getters = [];
35+
private array $setters = [];
3436

3537
/**
3638
* @param string $property Used to determine methods to call
3739
* The `getMarking` method will use `$subject->getProperty()`
3840
* The `setMarking` method will use `$subject->setProperty(string|array $places, array $context = array())`
3941
*/
40-
public function __construct(bool $singleState = false, string $property = 'marking')
41-
{
42-
$this->singleState = $singleState;
43-
$this->property = $property;
42+
public function __construct(
43+
private bool $singleState = false,
44+
private string $property = 'marking',
45+
) {
4446
}
4547

4648
public function getMarking(object $subject): Marking
4749
{
48-
$method = 'get'.ucfirst($this->property);
49-
50-
if (!method_exists($subject, $method)) {
51-
throw new LogicException(sprintf('The method "%s::%s()" does not exist.', get_debug_type($subject), $method));
52-
}
53-
5450
$marking = null;
5551
try {
56-
$marking = $subject->{$method}();
52+
$marking = ($this->getGetter($subject))();
5753
} catch (\Error $e) {
5854
$unInitializedPropertyMessage = sprintf('Typed property %s::$%s must not be accessed before initialization', get_debug_type($subject), $this->property);
5955
if ($e->getMessage() !== $unInitializedPropertyMessage) {
@@ -68,7 +64,7 @@ public function getMarking(object $subject): Marking
6864
if ($this->singleState) {
6965
$marking = [(string) $marking => 1];
7066
} elseif (!\is_array($marking)) {
71-
throw new LogicException(sprintf('The method "%s::%s()" did not return an array and the Workflow\'s Marking store is instantiated with $singleState=false.', get_debug_type($subject), $method));
67+
throw new LogicException(sprintf('The marking stored in "%s::%s" is not an array and the Workflow\'s Marking store is instantiated with $singleState=false.', get_debug_type($subject), $this->property));
7268
}
7369

7470
return new Marking($marking);
@@ -82,12 +78,72 @@ public function setMarking(object $subject, Marking $marking, array $context = [
8278
$marking = key($marking);
8379
}
8480

85-
$method = 'set'.ucfirst($this->property);
81+
($this->getSetter($subject))($marking, $context);
82+
}
83+
84+
private function getGetter(object $subject): callable
85+
{
86+
$propertyName = $this->property;
87+
$methodName = 'get'.ucfirst($propertyName);
8688

89+
return match ($this->getters[$subject::class] ??= $this->getType($subject, $propertyName, $methodName)) {
90+
MarkingStoreMethod::METHOD => static fn () => $subject->{$methodName}(),
91+
MarkingStoreMethod::PROPERTY => static fn () => $subject->{$propertyName},
92+
};
93+
}
94+
95+
private function getSetter(object $subject): callable
96+
{
97+
$propertyName = $this->property;
98+
$methodName = 'set'.ucfirst($propertyName);
99+
100+
return match ($this->setters[$subject::class] ??= $this->getType($subject, $propertyName, $methodName)) {
101+
MarkingStoreMethod::METHOD => static fn ($marking, $context) => $subject->{$methodName}($marking, $context),
102+
MarkingStoreMethod::PROPERTY => static fn ($marking) => $subject->{$propertyName} = $marking,
103+
};
104+
}
105+
106+
private function getType(object $subject, string $propertyName, string $method): MarkingStoreMethod
107+
{
87108
if (!method_exists($subject, $method)) {
88-
throw new LogicException(sprintf('The method "%s::%s()" does not exist.', get_debug_type($subject), $method));
109+
goto property;
89110
}
90111

91-
$subject->{$method}($marking, $context);
112+
try {
113+
$r = new \ReflectionMethod($subject, $method);
114+
} catch (\ReflectionException) {
115+
property:
116+
try {
117+
$r = new \ReflectionProperty($subject, $propertyName);
118+
} catch (\ReflectionException) {
119+
throw new LogicException(sprintf('The public property "%1$s::%2$s" nor the public method "%1$s::%3$s()" exist. At least one must be declared.', get_debug_type($subject), $propertyName, $method));
120+
}
121+
122+
if (!$r->isPublic()) {
123+
throw new LogicException(sprintf('The public method "%1$s::%3$s()" must be declared, or the property "%1$s::%2$s" must be public.', get_debug_type($subject), $propertyName, $method));
124+
}
125+
126+
return MarkingStoreMethod::PROPERTY;
127+
}
128+
129+
if (!$r->isPublic()) {
130+
try {
131+
$r = new \ReflectionProperty($subject, $propertyName);
132+
if ($r->isPublic()) {
133+
return MarkingStoreMethod::PROPERTY;
134+
}
135+
} catch (\ReflectionException) {
136+
}
137+
138+
throw new LogicException(sprintf('The method "%s::%s()" must be public.', get_debug_type($subject), $method));
139+
}
140+
141+
return MarkingStoreMethod::METHOD;
92142
}
93143
}
144+
145+
enum MarkingStoreMethod
146+
{
147+
case METHOD;
148+
case PROPERTY;
149+
}

src/Symfony/Component/Workflow/Tests/MarkingStore/MethodMarkingStoreTest.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ public function testGetSetMarkingWithMultipleState()
2929

3030
$marking->mark('first_place');
3131

32-
$markingStore->setMarking($subject, $marking);
32+
$markingStore->setMarking($subject, $marking, ['foo' => 'bar']);
3333

3434
$this->assertSame(['first_place' => 1], $subject->getMarking());
35+
$this->assertSame(['foo' => 'bar'], $subject->getContext());
3536

3637
$marking2 = $markingStore->getMarking($subject);
3738

@@ -50,11 +51,12 @@ public function testGetSetMarkingWithSingleState()
5051

5152
$marking->mark('first_place');
5253

53-
$markingStore->setMarking($subject, $marking);
54+
$markingStore->setMarking($subject, $marking, ['foo' => 'bar']);
5455

5556
$this->assertSame('first_place', $subject->getMarking());
5657

5758
$marking2 = $markingStore->getMarking($subject);
59+
$this->assertSame(['foo' => 'bar'], $subject->getContext());
5860

5961
$this->assertEquals($marking, $marking2);
6062
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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\Tests\MarkingStore;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Workflow\MarkingStore\MethodMarkingStore;
16+
use Symfony\Component\Workflow\Tests\MarkingStore\SubjectWithProperties;
17+
18+
class PropertiesMarkingStoreTest extends TestCase
19+
{
20+
public function testGetSetMarkingWithMultipleState()
21+
{
22+
$subject = new SubjectWithProperties();
23+
24+
$markingStore = new MethodMarkingStore(false);
25+
26+
$marking = $markingStore->getMarking($subject);
27+
28+
$this->assertCount(0, $marking->getPlaces());
29+
30+
$marking->mark('first_place');
31+
32+
$markingStore->setMarking($subject, $marking, ['foo' => 'bar']);
33+
34+
$this->assertSame(['first_place' => 1], $subject->marking);
35+
36+
$marking2 = $markingStore->getMarking($subject);
37+
38+
$this->assertEquals($marking, $marking2);
39+
}
40+
41+
public function testGetSetMarkingWithSingleState()
42+
{
43+
$subject = new SubjectWithProperties();
44+
45+
$markingStore = new MethodMarkingStore(true, 'place', 'placeContext');
46+
47+
$marking = $markingStore->getMarking($subject);
48+
49+
$this->assertCount(0, $marking->getPlaces());
50+
51+
$marking->mark('first_place');
52+
53+
$markingStore->setMarking($subject, $marking, ['foo' => 'bar']);
54+
55+
$this->assertSame('first_place', $subject->place);
56+
57+
$marking2 = $markingStore->getMarking($subject);
58+
59+
$this->assertEquals($marking, $marking2);
60+
}
61+
62+
public function testGetSetMarkingWithSingleStateAndAlmostEmptyPlaceName()
63+
{
64+
$subject = new SubjectWithProperties();
65+
$subject->place = 0;
66+
67+
$markingStore = new MethodMarkingStore(true, 'place');
68+
69+
$marking = $markingStore->getMarking($subject);
70+
71+
$this->assertCount(1, $marking->getPlaces());
72+
}
73+
74+
public function testGetMarkingWithValueObject()
75+
{
76+
$subject = new SubjectWithProperties();
77+
$subject->place = $this->createValueObject('first_place');
78+
79+
$markingStore = new MethodMarkingStore(true, 'place');
80+
81+
$marking = $markingStore->getMarking($subject);
82+
83+
$this->assertCount(1, $marking->getPlaces());
84+
$this->assertSame('first_place', (string) $subject->place);
85+
}
86+
87+
public function testGetMarkingWithUninitializedProperty()
88+
{
89+
$subject = new SubjectWithProperties();
90+
91+
$markingStore = new MethodMarkingStore(true, 'place');
92+
93+
$marking = $markingStore->getMarking($subject);
94+
95+
$this->assertCount(0, $marking->getPlaces());
96+
}
97+
98+
private function createValueObject(string $markingValue): object
99+
{
100+
return new class($markingValue) {
101+
/** @var string */
102+
private $markingValue;
103+
104+
public function __construct(string $markingValue)
105+
{
106+
$this->markingValue = $markingValue;
107+
}
108+
109+
public function __toString(): string
110+
{
111+
return $this->markingValue;
112+
}
113+
};
114+
}
115+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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\Tests\MarkingStore;
13+
14+
final class SubjectWithProperties
15+
{
16+
// for type=workflow
17+
public array $marking;
18+
19+
// for type=state_machine
20+
public string $place;
21+
22+
private function getMarking(): array
23+
{
24+
return $this->marking;
25+
}
26+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy