8000 [Workflow] Made the code more robbust and ease on-boarding · symfony/symfony@9e49198 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9e49198

Browse files
committed
[Workflow] Made the code more robbust and ease on-boarding
1 parent bdd3f95 commit 9e49198

File tree

6 files changed

+29
-28
lines changed

6 files changed

+29
-28
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Compiler/ValidateWorkflowsPass.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ public function process(ContainerBuilder $container)
3535
foreach ($taggedServices as $id => $tags) {
3636
$definition = $container->get($id);
3737
foreach ($tags as $tag) {
38-
if (empty($tag['name'])) {
38+
if (!array_key_exists('name', $tag)) {
3939
throw new RuntimeException(sprintf('The "name" for the tag "workflow.definition" of service "%s" must be set.', $id));
4040
}
41-
if (empty($tag['type'])) {
41+
if (!array_key_exists('type', $tag)) {
4242
throw new RuntimeException(sprintf('The "type" for the tag "workflow.definition" of service "%s" must be set.', $id));
4343
}
44-
if (empty($tag['marking_store'])) {
44+
if (!array_key_exists('marking_store', $tag)) {
4545
throw new RuntimeException(sprintf('The "marking_store" for the tag "workflow.definition" of service "%s" must be set.', $id));
4646
}
4747

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

-2Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ private function registerWorkflowConfiguration(array $workflows, ContainerBuilde
425425
foreach ($workflow['marking_store']['arguments'] as $argument) {
426426
$markingStoreDefinition->addArgument($argument);
427427
}
428-
} else {
428+
} elseif (isset($workflow['marking_store']['service'])) {
429429
$markingStoreDefinition = new Reference($workflow['marking_store']['service']);
430430
}
431431

@@ -438,7 +438,9 @@ private function registerWorkflowConfiguration(array $workflows, ContainerBuilde
438438

439439
$workflowDefinition = new DefinitionDecorator(sprintf('%s.abstract', $type));
440440
$workflowDefinition->replaceArgument(0, $definitionDefinition);
441-
$workflowDefinition->replaceArgument(1, $markingStoreDefinition);
441+
if (isset($markingStoreDefinition)) {
442+
$workflowDefinition->replaceArgument(1, $markingStoreDefinition);
443+
}
442444
$workflowDefinition->replaceArgument(3, $name);
443445

444446
$workflowId = sprintf('%s.%s', $type, $name);

src/Symfony/Bundle/FrameworkBundle/Resources/config/workflow.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
<services>
88
<service id="workflow.abstract" class="Symfony\Component\Workflow\Workflow" abstract="true">
99
<argument /> <!-- workflow definition -->
10-
<argument /> <!-- marking store -->
10+
<argument type="constant">null</argument> <!-- marking store -->
1111
<argument type="service" id="event_dispatcher" on-invalid="ignore" />
1212
<argument /> <!-- name -->
1313
</service>
1414
<service id="state_machine.abstract" class="Symfony\Component\Workflow\StateMachine" abstract="true">
1515
<argument /> <!-- workflow definition -->
16-
<argument /> <!-- marking store -->
16+
<argument type="constant">null</argument> <!-- marking store -->
1717
<argument type="service" id="event_dispatcher" on-invalid="ignore" />
1818
<argument /> <!-- name -->
1919
</service>

src/Symfony/Component/Workflow/Tests/WorkflowTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,9 @@ public function testCanWithGuard()
113113
$subject = new \stdClass();
114114
$subject->marking = null;
115115
$eventDispatcher = new EventDispatcher();
116-
$eventDispatcher->addListener('workflow.workflow_name.guard.t1', function (GuardEvent $event) { $event->setBlocked(true); });
116+
$eventDispatcher->addListener('workflow.workflow_name.guard.t1', function (GuardEvent $event) {
117+
$event->setBlocked(true);
118+
});
117119
$workflow = new Workflow($definition, new PropertyAccessorMarkingStore(), $eventDispatcher, 'workflow_name');
118120

119121
$this->assertFalse($workflow->can($subject, 't1'));
@@ -188,7 +190,9 @@ public function testGetEnabledTransitions()
188190
$subject = new \stdClass();
189191
$subject->marking = null;
190192
$eventDispatcher = new EventDispatcher();
191-
$eventDispatcher->addListener('workflow.workflow_name.guard.t1', function (GuardEvent $event) { $event->setBlocked(true); });
193+
$eventDispatcher->addListener('workflow.workflow_name.guard.t1', function (GuardEvent $event) {
194+
$event->setBlocked(true);
195+
});
192196
$workflow = new Workflow($definition, new PropertyAccessorMarkingStore(), $eventDispatcher, 'workflow_name');
193197

194198
$this->assertEmpty($workflow->getEnabledTransitions($subject));

src/Symfony/Component/Workflow/Validator/StateMachineValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public function validate(Definition $definition, $name)
5353
if (isset($transitionFromNames[$from][$transition->getName()])) {
5454
throw new InvalidDefinitionException(
5555
sprintf(
56-
'A transition from a place/state must have an unique name. Multiple transition named "%s" from place/state "%s" where found on StateMachine "%s". ',
56+
'A transition from a place/state must have an unique name. Multiple transitions named "%s" from place/state "%s" where found on StateMachine "%s". ',
5757
$transition->getName(),
5858
$from,
5959
$name

src/Symfony/Component/Workflow/Workflow.php

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Workflow\Event\GuardEvent;
1717
use Symfony\Component\Workflow\Exception\LogicException;
1818
use Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface;
19+
use Symfony\Component\Workflow\MarkingStore\PropertyAccessorMarkingStore;
1920

2021
/**
2122
* @author Fabien Potencier <fabien@symfony.com>
@@ -29,10 +30,10 @@ class Workflow
2930
private $dispatcher;
3031
private $name;
3132

32-
public function __construct(Definition $definition, MarkingStoreInterface $markingStore, EventDispatcherInterface $dispatcher = null, $name = 'unnamed')
33+
public function __construct(Definition $definition, MarkingStoreInterface $markingStore = null, EventDispatcherInterface $dispatcher = null, $name = 'unnamed')
3334
{
3435
$this->definition = $definition;
35-
$this->markingStore = $markingStore;
36+
$this->markingStore = $markingStore ?: new PropertyAccessorMarkingStore();
3637
$this->dispatcher = $dispatcher;
3738
$this->name = $name;
3839
}
@@ -163,7 +164,7 @@ public function getName()
163164
* @param Marking $marking
164165
* @param Transition $transition
165166
*
166-
* @return bool|void boolean true if this transition is guarded, ie you cannot use it.
167+
* @return bool|void boolean true if this transition is guarded, ie you cannot use it
167168
*/
168169
private function guardTransition($subject, Marking $marking, Transition $transition)
169170
{
@@ -253,25 +254,21 @@ private function getTransitions($transitionName)
253254
{
254255
$transitions = $this->definition->getTransitions();
255256

256-
$namedTransitions = array_filter(
257-
$transitions,
258-
function (Transition $transition) use ($transitionName) {
259-
return $transitionName === $transition->getName();
260-
}
261-
);
257+
$transitions = array_filter($transitions, function (Transition $transition) use ($transitionName) {
258+
return $transitionName === $transition->getName();
259+
});
262260

263-
if (empty($namedTransitions)) {
264-
throw new LogicException(
265-
sprintf('Transition "%s" does not exist for workflow "%s".', $transitionName, $this->name)
266-
);
261+
if (!$transitions) {
262+
throw new LogicException(sprintf('Transition "%s" does not exist for workflow "%s".', $transitionName, $this->name));
267263
}
268264

269-
return $namedTransitions;
265+
return $transitions;
270266
}
271267

272268
/**
273-
* Return the first Transition in $transitions that is valid for the $subject and $marking. null is returned when
274-
* you cannot do any Transition in $transitions on the $subject.
269+
* Return the first Transition in $transitions that is valid for the
270+
* $subject and $marking. null is returned when you cannot do any Transition
271+
* in $transitions on the $subject.
275272
*
276273
* @param object $subject
277274
* @param Marking $marking
@@ -292,7 +289,5 @@ private function getTransitionForSubject($subject, Marking $marking, array $tran
292289
return $transition;
293290
}
294291
}
295-
296-
return;
297292
}
298293
}

0 commit comments

Comments
 (0)
0