8000 [Workflow] Add a MetadataStore · symfony/symfony@dcac1cc · GitHub
[go: up one dir, main page]

Skip to content

Commit dcac1cc

Browse files
committed
[Workflow] Add a MetadataStore
1 parent 89d1b65 commit dcac1cc

File tree

7 files changed

+161
-13
lines changed

7 files changed

+161
-13
lines changed

src/Symfony/Component/Workflow/Event/Event.php

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
namespace Symfony\Component\Workflow\Event;
1313

1414
use Symfony\Component\EventDispatcher\Event as BaseEvent;
15+
use Symfony\Component\Workflow\Exception\InvalidArgumentException;
1516
use Symfony\Component\Workflow\Marking;
1617
use Symfony\Component\Workflow\Transition;
18+
use Symfony\Component\Workflow\WorkflowInterface;
1719

1820
/**
1921
* @author Fabien Potencier <fabien@symfony.com>
@@ -24,20 +26,28 @@ class Event extends BaseEvent
2426
private $subject;
2527
private $marking;
2628
private $transition;
29+
private $workflow;
2730
private $workflowName;
2831

2932
/**
3033
* @param object $subject
3134
* @param Marking $marking
3235
* @param Transition $transition
33-
* @param string $workflowName
36+
* @param Workflow $workflow
3437
*/
35-
public function __construct($subject, Marking $marking, Transition $transition, string $workflowName = 'unnamed')
38+
public function __construct($subject, Marking $marking, Transition $transition, $workflow = null)
3639
{
3740
$this->subject = $subject;
3841
$this->marking = $marking;
3942
$this->transition = $transition;
40-
$this->workflowName = $workflowName;
43+
if (is_string($workflow)) {
44+
@trigger_error(sprintf('Passing a string as 4th parameter of "%s" is deprecated since Symfony 4.1. Pass a %s instance instead.', __METHOD__, WorkflowInterface::class), E_USER_DEPRECATED);
45+
$this->workflowName = $workflow;
46+
} elseif ($workflow instanceof WorkflowInterface) {
47+
$this->workflow = $workflow;
48+
} else {
49+
throw new InvalidArgumentException(sprintf('The 4th parameter of "%s" should be a "%s" instance instead.', __METHOD__, WorkflowInterface::class));
50+
}
4151
}
4252

4353
public function getMarking()
@@ -55,8 +65,28 @@ public function getTransition()
5565
return $this->transition;
5666
}
5767

68+
public function getWorkflow(): WorkflowInterface
69+
{
70+
// BC layer
71+
if (!$this->workflow instanceof WorkflowInterface) {
72+
throw new \RuntimeException(sprintf('The 4th parameter of "%s"::__construct() should be a "%s" instance.', __CLASS__, WorkflowInterface::class));
73+
}
74+
75+
return $this->workflow;
76+
}
77+
5878
public function getWorkflowName()
5979
{
60-
return $this->workflowName;
80+
// BC layer
81+
if ($this->workflowName) {
82+
return $this->workflowName;
83+
}
84+
85+
// BC layer
86+
if (!$this->workflow instanceof WorkflowInterface) {
87+
throw new \RuntimeException(sprintf('The 4th parameter of "%s"::__construct() should be a "%s" instance.', __CLASS__, WorkflowInterface::class));
88+
}
89+
90+
return $this->workflow->getName();
6191
}
6292
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Symfony\Component\Workflow\Metadata;
4+
5+
use Symfony\Component\Workflow\Transition;
6+
7+
class InMemoryMetadataStore implements MetadataStoreInterface
8+
{
9+
private $workflowMetadata;
10+
private $placesMetadata;
11+
private $transitionsMetadata;
12+
13+
public function __construct(MetadataBag $workflowMetadata = null, array $placesMetadata = array(), \SplObjectStorage $transitionsMetadata = null)
14+
{
15+
$this->workflowMetadata = $workflowMetadata;
16+
$this->placesMetadata = $placesMetadata;
17+
$this->transitionsMetadata = $transitionsMetadata ?: new \SplObjectStorage();
18+
}
19+
20+
public function getWorkflowMetadata(): ?MetadataBag
21+
{
22+
return $this->workflowMetadata;
23+
}
24+
25+
public function getPlaceMetadata(string $place): ?MetadataBag
26+
{
27+
return $this->placesMetadata[$places] ?? null;
28+
}
29+
30+
public function getTransitionMetadata(Transition $transition): ?MetadataBag
31+
{
32+
return $this->transitionsMetadata[$transition] ?? null;
33+
}
34+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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\Metadata;
13+
14+
/**
15+
* MetadataBag is a container for key/value pairs.
16+
*
17+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
18+
*/
19+
class MetadataBag
20+
{
21+
private $parameters;
22+
23+
public function __construct(array $parameters = array())
24+
{
25+
$this->parameters = $parameters;
26+
}
27+
28+
public function has(string $key): bool
29+
{
30+
return array_key_exists($key, $this->parameters);
31+
}
32+
33+
public function get(string $key, $default = null)
34+
{
35+
return $this->parameters[$key] ?? $default;
36+
}
37+
38+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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\Metadata;
13+
14+
use Symfony\Component\Workflow\Transition;
15+
16+
/**
17+
* MetadataStoreInterface is able to fetch metadata for a specific workflow
18+
*
19+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
20+
*/
21+
interface MetadataStoreInterface
22+
{
23+
public function getWorkflowMetadata(): ?MetadataBag;
24+
25+
public function getPlaceMetadata(string $place): ?MetadataBag;
26+
27+
public function getTransitionMetadata(Transition $transition): ?MetadataBag;
28+
}

src/Symfony/Component/Workflow/Tests/EventListener/GuardListenerTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Workflow\Event\GuardEvent;
1515
use Symfony\Component\Workflow\Marking;
1616
use Symfony\Component\Workflow\Transition;
17+
use Symfony\Component\Workflow\WorkflowInterface;
1718

1819
class GuardListenerTest extends TestCase
1920
{
@@ -102,7 +103,9 @@ private function createEvent()
102103
$subject->marking = new Marking();
103104
$transition = new Transition('name', 'from', 'to');
104105

105-
return new GuardEvent($subject, $subject->marking, $transition);
106+
$workflow = $this->getMockBuilder(WorkflowInterface::class)->getMock();
107+
108+
return new GuardEvent($subject, $subject->marking, $transition, $workflow);
106109
}
107110

108111
private function configureAuthenticationChecker($isUsed, $granted = true)

src/Symfony/Component/Workflow/Workflow.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use Symfony\Component\Workflow\Exception\LogicException;
1818
use Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface;
1919
use Symfony\Component\Workflow\MarkingStore\MultipleStateMarkingStore;
20+
use Symfony\Component\Workflow\Metadata\InMemoryMetadataStore;
21+
use Symfony\Component\Workflow\Metadata\MetadataStoreInterface;
20 93C6 22

2123
/**
2224
* @author Fabien Potencier <fabien@symfony.com>
@@ -29,13 +31,15 @@ class Workflow implements WorkflowInterface
2931
private $markingStore;
3032
private $dispatcher;
3133
private $name;
34+
private $metadataStore;
3235

33-
public function __construct(Definition $definition, MarkingStoreInterface $markingStore = null, EventDispatcherInterface $dispatcher = null, string $name = 'unnamed')
36+
public function __construct(Definition $definition, MarkingStoreInterface $markingStore = null, EventDispatcherInterface $dispatcher = null, string $name = 'unnamed', MetadataStoreInterface $metadataStore = null)
3437
{
3538
$this->definition = $definition;
3639
$this->markingStore = $markingStore ?: new MultipleStateMarkingStore();
3740
$this->dispatcher = $dispatcher;
3841
$this->name = $name;
42+
$this->metadataStore = $metadataStore ?: new InMemoryMetadataStore();
3943
}
4044

4145
/**
@@ -185,6 +189,14 @@ public function getMarkingStore()
185189
return $this->markingStore;
186190
}
187191

192+
/**
193+
* {@inheritdoc}
194+
*/
195+
public function getMetadataStore(): MetadataStoreInterface
196+
{
197+
return $this->metadataStore;
198+
}
199+
188200
private function doCan($subject, Marking $marking, Transition $transition)
189201
{
190202
foreach ($transition->getFroms() as $place) {
@@ -213,7 +225,7 @@ private function guardTransition($subject, Marking $marking, Transition $transit
213225
return;
214226
}
215227

216-
$event = new GuardEvent($subject, $marking, $transition, $this->name);
228+
$event = new GuardEvent($subject, $marking, $transition, $this);
217229

218230
$this->dispatcher->dispatch('workflow.guard', $event);
219231
$this->dispatcher->dispatch(sprintf('workflow.%s.guard', $this->name), $event);
@@ -227,7 +239,7 @@ private function leave($subject, Transition $transition, Marking $marking)
227239
$places = $transition->getFroms();
228240

229241
if (null !== $this->dispatcher) {
230-
$event = new Event($subject, $marking, $transition, $this->name);
242+
$event = new Event($subject, $marking, $transition, $this);
231243

232244
$this->dispatcher->dispatch('workflow.leave', $event);
233245
$this->dispatcher->dispatch(sprintf('workflow.%s.leave', $this->name), $event);
@@ -248,7 +260,7 @@ private function transition($subject, Transition $transition, Marking $marking)
248260
return;
249261
}
250262

251-
$event = new Event($subject, $marking, $transition, $this->name);
263+
$event = new Event($subject, $marking, $transition, $this);
252264

253265
$this->dispatcher->dispatch('workflow.transition', $event);
254266
$this->dispatcher->dispatch(sprintf('workflow.%s.transition', $this->name), $event);
@@ -260,7 +272,7 @@ private function enter($subject, Transition $transition, Marking $marking)
260272
$places = $transition->getTos();
261273

262274
if (null !== $this->dispatcher) {
263-
10000 $event = new Event($subject, $marking, $transition, $this->name);
275+
$event = new Event($subject, $marking, $transition, $this);
264276

265277
$this->dispatcher->dispatch('workflow.enter', $event);
266278
$this->dispatcher->dispatch(sprintf('workflow.%s.enter', $this->name), $event);
@@ -281,7 +293,7 @@ private function entered($subject, Transition $transition, Marking $marking)
281293
return;
282294
}
283295

284-
$event = new Event($subject, $marking, $transition, $this->name);
296+
$event = new Event($subject, $marking, $transition, $this);
285297

286298
$this->dispatcher->dispatch('workflow.entered', $event);
287299
$this->dispatcher->dispatch(sprintf('workflow.%s.entered', $this->name), $event);
@@ -297,7 +309,7 @@ private function completed($subject, Transition $transition, Marking $marking)
297309
return;
298310
}
299311

300-
$event = new Event($subject, $marking, $transition, $this->name);
312+
$event = new Event($subject, $marking, $transition, $this);
301313

302314
$this->dispatcher->dispatch('workflow.completed', $event);
303315
$this->dispatcher->dispatch(sprintf('workflow.%s.completed', $this->name), $event);
@@ -310,7 +322,7 @@ private function announce($subject, Transition $initialTransition, Marking $mark
310322
return;
311323
}
312324

313-
$event = new Event($subject, $marking, $initialTransition, $this->name);
325+
$event = new Event($subject, $marking, $initialTransition, $this);
314326

315327
$this->dispatcher->dispatch('workflow.announce', $event);
316328
$this->dispatcher->dispatch(sprintf('workflow.%s.announce', $this->name), $event);

src/Symfony/Component/Workflow/WorkflowInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Workflow\Exception\LogicException;
1515
use Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface;
16+
use Symfony\Component\Workflow\Metadata\MetadataStoreInterface;
1617

1718
/**
1819
* @author Amrouche Hamza <hamza.simperfit@gmail.com>
@@ -78,4 +79,6 @@ public function getDefinition();
7879
* @return MarkingStoreInterface
7980
*/
8081
public function getMarkingStore();
82+
83+
public function getMetadataStore(): MetadataStoreInterface;
8184
}

0 commit comments

Comments
 (0)
0