[Workflow] Made the code more robbust and ease on-boarding

This commit is contained in:
Grégoire Pineau 2016-11-07 18:57:54 +01:00
parent bdd3f95da6
commit 9e491982cf
6 changed files with 29 additions and 28 deletions

View File

@ -35,13 +35,13 @@ class ValidateWorkflowsPass implements CompilerPassInterface
foreach ($taggedServices as $id => $tags) {
$definition = $container->get($id);
foreach ($tags as $tag) {
if (empty($tag['name'])) {
if (!array_key_exists('name', $tag)) {
throw new RuntimeException(sprintf('The "name" for the tag "workflow.definition" of service "%s" must be set.', $id));
}
if (empty($tag['type'])) {
if (!array_key_exists('type', $tag)) {
throw new RuntimeException(sprintf('The "type" for the tag "workflow.definition" of service "%s" must be set.', $id));
}
if (empty($tag['marking_store'])) {
if (!array_key_exists('marking_store', $tag)) {
throw new RuntimeException(sprintf('The "marking_store" for the tag "workflow.definition" of service "%s" must be set.', $id));
}

View File

@ -425,7 +425,7 @@ class FrameworkExtension extends Extension
foreach ($workflow['marking_store']['arguments'] as $argument) {
$markingStoreDefinition->addArgument($argument);
}
} else {
} elseif (isset($workflow['marking_store']['service'])) {
$markingStoreDefinition = new Reference($workflow['marking_store']['service']);
}
@ -438,7 +438,9 @@ class FrameworkExtension extends Extension
$workflowDefinition = new DefinitionDecorator(sprintf('%s.abstract', $type));
$workflowDefinition->replaceArgument(0, $definitionDefinition);
$workflowDefinition->replaceArgument(1, $markingStoreDefinition);
if (isset($markingStoreDefinition)) {
$workflowDefinition->replaceArgument(1, $markingStoreDefinition);
}
$workflowDefinition->replaceArgument(3, $name);
$workflowId = sprintf('%s.%s', $type, $name);

View File

@ -7,13 +7,13 @@
<services>
<service id="workflow.abstract" class="Symfony\Component\Workflow\Workflow" abstract="true">
<argument /> <!-- workflow definition -->
<argument /> <!-- marking store -->
<argument type="constant">null</argument> <!-- marking store -->
<argument type="service" id="event_dispatcher" on-invalid="ignore" />
<argument /> <!-- name -->
</service>
<service id="state_machine.abstract" class="Symfony\Component\Workflow\StateMachine" abstract="true">
<argument /> <!-- workflow definition -->
<argument /> <!-- marking store -->
<argument type="constant">null</argument> <!-- marking store -->
<argument type="service" id="event_dispatcher" on-invalid="ignore" />
<argument /> <!-- name -->
</service>

View File

@ -113,7 +113,9 @@ class WorkflowTest extends \PHPUnit_Framework_TestCase
$subject = new \stdClass();
$subject->marking = null;
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addListener('workflow.workflow_name.guard.t1', function (GuardEvent $event) { $event->setBlocked(true); });
$eventDispatcher->addListener('workflow.workflow_name.guard.t1', function (GuardEvent $event) {
$event->setBlocked(true);
});
$workflow = new Workflow($definition, new PropertyAccessorMarkingStore(), $eventDispatcher, 'workflow_name');
$this->assertFalse($workflow->can($subject, 't1'));
@ -188,7 +190,9 @@ class WorkflowTest extends \PHPUnit_Framework_TestCase
$subject = new \stdClass();
$subject->marking = null;
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addListener('workflow.workflow_name.guard.t1', function (GuardEvent $event) { $event->setBlocked(true); });
$eventDispatcher->addListener('workflow.workflow_name.guard.t1', function (GuardEvent $event) {
$event->setBlocked(true);
});
$workflow = new Workflow($definition, new PropertyAccessorMarkingStore(), $eventDispatcher, 'workflow_name');
$this->assertEmpty($workflow->getEnabledTransitions($subject));

View File

@ -53,7 +53,7 @@ class StateMachineValidator implements DefinitionValidatorInterface
if (isset($transitionFromNames[$from][$transition->getName()])) {
throw new InvalidDefinitionException(
sprintf(
'A transition from a place/state must have an unique name. Multiple transition named "%s" from place/state "%s" where found on StateMachine "%s". ',
'A transition from a place/state must have an unique name. Multiple transitions named "%s" from place/state "%s" where found on StateMachine "%s". ',
$transition->getName(),
$from,
$name

View File

@ -16,6 +16,7 @@ use Symfony\Component\Workflow\Event\Event;
use Symfony\Component\Workflow\Event\GuardEvent;
use Symfony\Component\Workflow\Exception\LogicException;
use Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface;
use Symfony\Component\Workflow\MarkingStore\PropertyAccessorMarkingStore;
/**
* @author Fabien Potencier <fabien@symfony.com>
@ -29,10 +30,10 @@ class Workflow
private $dispatcher;
private $name;
public function __construct(Definition $definition, MarkingStoreInterface $markingStore, EventDispatcherInterface $dispatcher = null, $name = 'unnamed')
public function __construct(Definition $definition, MarkingStoreInterface $markingStore = null, EventDispatcherInterface $dispatcher = null, $name = 'unnamed')
{
$this->definition = $definition;
$this->markingStore = $markingStore;
$this->markingStore = $markingStore ?: new PropertyAccessorMarkingStore();
$this->dispatcher = $dispatcher;
$this->name = $name;
}
@ -163,7 +164,7 @@ class Workflow
* @param Marking $marking
* @param Transition $transition
*
* @return bool|void boolean true if this transition is guarded, ie you cannot use it.
* @return bool|void boolean true if this transition is guarded, ie you cannot use it
*/
private function guardTransition($subject, Marking $marking, Transition $transition)
{
@ -253,25 +254,21 @@ class Workflow
{
$transitions = $this->definition->getTransitions();
$namedTransitions = array_filter(
$transitions,
function (Transition $transition) use ($transitionName) {
return $transitionName === $transition->getName();
}
);
$transitions = array_filter($transitions, function (Transition $transition) use ($transitionName) {
return $transitionName === $transition->getName();
});
if (empty($namedTransitions)) {
throw new LogicException(
sprintf('Transition "%s" does not exist for workflow "%s".', $transitionName, $this->name)
);
if (!$transitions) {
throw new LogicException(sprintf('Transition "%s" does not exist for workflow "%s".', $transitionName, $this->name));
}
return $namedTransitions;
return $transitions;
}
/**
* Return the first Transition in $transitions that is valid for the $subject and $marking. null is returned when
* you cannot do any Transition in $transitions on the $subject.
* Return the first Transition in $transitions that is valid for the
* $subject and $marking. null is returned when you cannot do any Transition
* in $transitions on the $subject.
*
* @param object $subject
* @param Marking $marking
@ -292,7 +289,5 @@ class Workflow
return $transition;
}
}
return;
}
}