Skip to content

Commit 10c567b

Browse files
committed
[Workflow] Add a MetadataStore
1 parent 1ad8976 commit 10c567b

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

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.1.0
5+
-----
6+
7+
* add a `workflow_metadata` function
8+
49
3.4.0
510
-----
611

Extension/WorkflowExtension.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function getFunctions()
3737
new TwigFunction('workflow_transitions', array($this, 'getEnabledTransitions')),
3838
new TwigFunction('workflow_has_marked_place', array($this, 'hasMarkedPlace')),
3939
new TwigFunction('workflow_marked_places', array($this, 'getMarkedPlaces')),
40+
new TwigFunction('workflow_metadata', array($this, 'getMetadata')),
4041
);
4142
}
4243

@@ -101,6 +102,24 @@ public function getMarkedPlaces($subject, $placesNameOnly = true, $name = null)
101102
return $places;
102103
}
103104

105+
/**
106+
* Returns the metadata for a specific subject.
107+
*
108+
* @param object $subject A subject
109+
* @param null|string|Transition $metadataSubject Use null to get workflow metadata
110+
* Use a string (the place name) to get place metadata
111+
* Use a Transition instance to get transition metadata
112+
*/
113+
public function getMetadata($subject, string $key, $metadataSubject = null, string $name = null): ?string
114+
{
115+
return $this
116+
->workflowRegistry
117+
->get($subject, $name)
118+
->getMetadataStore()
119+
->getMetadata($key, $metadataSubject)
120+
;
121+
}
122+
104123
public function getName()
105124
{
106125
return 'workflow';

Tests/Extension/WorkflowExtensionTest.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Bridge\Twig\Extension\WorkflowExtension;
1616
use Symfony\Component\Workflow\Definition;
17+
use Symfony\Component\Workflow\Metadata\InMemoryMetadataStore;
1718
use Symfony\Component\Workflow\Registry;
1819
use Symfony\Component\Workflow\SupportStrategy\ClassInstanceSupportStrategy;
1920
use Symfony\Component\Workflow\SupportStrategy\InstanceOfSupportStrategy;
@@ -23,6 +24,7 @@
2324
class WorkflowExtensionTest extends TestCase
2425
{
2526
private $extension;
27+
private $t1;
2628

2729
protected function setUp()
2830
{
@@ -32,10 +34,21 @@ protected function setUp()
3234

3335
$places = array('ordered', 'waiting_for_payment', 'processed');
3436
$transitions = array(
35-
new Transition('t1', 'ordered', 'waiting_for_payment'),
37+
$this->t1 = new Transition('t1', 'ordered', 'waiting_for_payment'),
3638
new Transition('t2', 'waiting_for_payment', 'processed'),
3739
);
38-
$definition = new Definition($places, $transitions);
40+
41+
$metadataStore = null;
42+
if (class_exists(InMemoryMetadataStore::class)) {
43+
$transitionsMetadata = new \SplObjectStorage();
44+
$transitionsMetadata->attach($this->t1, array('title' => 't1 title'));
45+
$metadataStore = new InMemoryMetadataStore(
46+
array('title' => 'workflow title'),
47+
array('orderer' => array('title' => 'ordered title')),
48+
$transitionsMetadata
49+
);
50+
}
51+
$definition = new Definition($places, $transitions, null, $metadataStore);
3952
$workflow = new Workflow($definition);
4053

4154
$registry = new Registry();
@@ -88,4 +101,19 @@ public function testGetMarkedPlaces()
88101
$this->assertSame(array('ordered', 'waiting_for_payment'), $this->extension->getMarkedPlaces($subject));
89102
$this->assertSame($subject->marking, $this->extension->getMarkedPlaces($subject, false));
90103
}
104+
105+
public function testGetMetadata()
106+
{
107+
if (!class_exists(InMemoryMetadataStore::class)) {
108+
$this->markTestSkipped('This test requires symfony/workflow:4.1.');
109+
}
110+
$subject = new \stdClass();
111+
$subject->marking = array();
112+
113+
$this->assertSame('workflow title', $this->extension->getMetadata($subject, 'title'));
114+
$this->assertSame('ordered title', $this->extension->getMetadata($subject, 'title', 'orderer'));
115+
$this->assertSame('t1 title', $this->extension->getMetadata($subject, 'title', $this->t1));
116+
$this->assertNull($this->extension->getMetadata($subject, 'not found'));
117+
$this->assertNull($this->extension->getMetadata($subject, 'not found', $this->t1));
118+
}
91119
}

0 commit comments

Comments
 (0)