Skip to content

Commit 93eeca2

Browse files
committed
[Workflow] Add support for storing the marking in a property
1 parent deb160a commit 93eeca2

File tree

5 files changed

+210
-22
lines changed

5 files changed

+210
-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: 69 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,45 +15,42 @@
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 exist,
19+
* 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 marking
2425
*
2526
* "multiple state" Marking means a subject can be in many states at the same
26-
* time. Use it with workflow.
27+
* time. Use it with workflow. It uses an array to store the marking
2728
*
2829
* @author Grégoire Pineau <lyrixx@lyrixx.info>
2930
*/
3031
final class MethodMarkingStore implements MarkingStoreInterface
3132
{
32-
private bool $singleState;
33-
private string $property;
33+
private readonly \SplObjectStorage $getters;
34+
private readonly \SplObjectStorage $setters;
3435

3536
/**
3637
* @param string $property Used to determine methods to call
3738
* The `getMarking` method will use `$subject->getProperty()`
3839
* The `setMarking` method will use `$subject->setProperty(string|array $places, array $context = array())`
3940
*/
40-
public function __construct(bool $singleState = false, string $property = 'marking')
41-
{
42-
$this->singleState = $singleState;
43-
$this->property = $property;
41+
public function __construct(
42+
private bool $singleState = false,
43+
private string $property = 'marking',
44+
) {
45+
$this->getters = new \SplObjectStorage();
46+
$this->setters = new \SplObjectStorage();
4447
}
4548

4649
public function getMarking(object $subject): Marking
4750
{
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-
5451
$marking = null;
5552
try {
56-
$marking = $subject->{$method}();
53+
$marking = ($this->getGetter($subject))();
5754
} catch (\Error $e) {
5855
$unInitializedPropertyMessage = sprintf('Typed property %s::$%s must not be accessed before initialization', get_debug_type($subject), $this->property);
5956
if ($e->getMessage() !== $unInitializedPropertyMessage) {
@@ -68,7 +65,7 @@ public function getMarking(object $subject): Marking
6865
if ($this->singleState) {
6966
$marking = [(string) $marking => 1];
7067
} 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));
68+
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));
7269
}
7370

7471
return new Marking($marking);
@@ -82,12 +79,64 @@ public function setMarking(object $subject, Marking $marking, array $context = [
8279
$marking = key($marking);
8380
}
8481

85-
$method = 'set'.ucfirst($this->property);
82+
($this->getSetter($subject))($marking, $context);
83+
}
84+
85+
private function getGetter(object $subject): callable
86+
{
87+
$propertyName = $this->property;
88+
$methodName = 'get'.ucfirst($propertyName);
89+
90+
return match ($this->getters[$subject] ??= $this->getType($subject, $propertyName, $methodName)) {
91+
MarkingStoreMethod::METHOD => static fn () => $subject->{$methodName}(),
92+
MarkingStoreMethod::PROPERTY => static fn () => $subject->{$propertyName},
93+
};
94+
}
95+
96+
private function getSetter(object $subject): callable
97+
{
98+
$propertyName = $this->property;
99+
$methodName = 'set'.ucfirst($propertyName);
100+
101+
return match ($this->setters[$subject] ??= $this->getType($subject, $propertyName, $methodName)) {
102+
MarkingStoreMethod::METHOD => static fn ($marking, $context) => $subject->{$methodName}($marking, $context),
103+
MarkingStoreMethod::PROPERTY => static fn ($marking) => $subject->{$propertyName} = $marking,
104+
};
105+
}
86106

107+
private function getType(object $subject, string $propertyName, string $method): MarkingStoreMethod
108+
{
87109
if (!method_exists($subject, $method)) {
88-
throw new LogicException(sprintf('The method "%s::%s()" does not exist.', get_debug_type($subject), $method));
110+
goto property;
89111
}
90112

91-
$subject->{$method}($marking, $context);
113+
try {
114+
$r = new \ReflectionMethod($subject, $method);
115+
} catch (\ReflectionException) {
116+
property:
117+
try {
118+
$r = new \ReflectionProperty($subject, $propertyName);
119+
} catch (\ReflectionException) {
120+
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));
121+
}
122+
123+
if (!$r->isPublic()) {
124+
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));
125+
}
126+
127+
return MarkingStoreMethod::PROPERTY;
128+
}
129+
130+
if (!$r->isPublic()) {
131+
throw new LogicException(sprintf('The method "%s::%s()" must be public.', get_debug_type($subject), $method));
132+
}
133+
134+
return MarkingStoreMethod::METHOD;
92135
}
93136
}
137+
138+
enum MarkingStoreMethod
139+
{
140+
case METHOD;
141+
case PROPERTY;
142+
}

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\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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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;
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+
}

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