From 1f82b55285fcf15719d6d2efd08007d759c21919 Mon Sep 17 00:00:00 2001 From: Atsuhiro Kubo Date: Sat, 9 Jan 2021 20:36:11 +0900 Subject: [PATCH 1/5] change the value of @since to x.y.z (PSR-5 style) --- src/Definition/Bpmn2Reader.php | 6 ++-- src/Process/Process.php | 10 +++--- src/Process/WorkItemContext.php | 2 +- src/Process/WorkflowContextInterface.php | 2 +- src/Workflow/Activity/SendTask.php | 2 +- src/Workflow/Activity/ServiceTask.php | 2 +- src/Workflow/Activity/Task.php | 2 +- .../Element/ConnectingObjectCollection.php | 2 +- src/Workflow/Element/FlowObjectInterface.php | 2 +- src/Workflow/Event/EndEvent.php | 6 ++-- src/Workflow/Event/StartEvent.php | 6 ++-- src/Workflow/Gateway/ExclusiveGateway.php | 2 +- src/Workflow/Gateway/ParallelGateway.php | 2 +- .../Operation/OperationRunnerInterface.php | 2 +- .../Operation/OperationalInterface.php | 2 +- src/Workflow/Resource/MessageInterface.php | 2 +- src/Workflow/Workflow.php | 32 +++++++++---------- src/Workflow/WorkflowBuilder.php | 10 +++--- tests/Definition/Bpmn2ReaderTest.php | 2 +- tests/Workflow/WorkflowRepository.php | 10 +++--- tests/Workflow/WorkflowTest.php | 10 +++--- 21 files changed, 58 insertions(+), 58 deletions(-) diff --git a/src/Definition/Bpmn2Reader.php b/src/Definition/Bpmn2Reader.php index f466132..a3461bc 100644 --- a/src/Definition/Bpmn2Reader.php +++ b/src/Definition/Bpmn2Reader.php @@ -42,7 +42,7 @@ public function read($file) * * @throws IdAttributeNotFoundException * - * @since Method available since Release 1.3.0 + * @since 1.3.0 */ public function readSource($source) { @@ -63,7 +63,7 @@ public function readSource($source) * * @throws IdAttributeNotFoundException * - * @since Method available since Release 1.3.0 + * @since 1.3.0 */ private function readDocument(\DOMDocument $document, $workflowId = null) { @@ -251,7 +251,7 @@ private function createIdAttributeNotFoundException(\DOMElement $element, $workf * * @return string * - * @since Method available since Release 1.3.0 + * @since 1.3.0 */ private function provideRoleIdForFlowObject(array $flowObjectRoles, $flowObjectId) { diff --git a/src/Process/Process.php b/src/Process/Process.php index d2de9f9..31943e4 100644 --- a/src/Process/Process.php +++ b/src/Process/Process.php @@ -35,14 +35,14 @@ class Process /** * @var ExpressionLanguage * - * @since Property available since Release 1.2.0 + * @since 1.2.0 */ private $expressionLanguage; /** * @var OperationRunnerInterface * - * @since Property available since Release 1.2.0 + * @since 1.2.0 */ private $operationRunner; @@ -61,7 +61,7 @@ public function __construct($workflowContext, WorkflowRepositoryInterface $workf /** * @param ExpressionLanguage $expressionLanguage * - * @since Method available since Release 1.2.0 + * @since 1.2.0 */ public function setExpressionLanguage(ExpressionLanguage $expressionLanguage) { @@ -164,7 +164,7 @@ public function executeWorkItem(WorkItemContextInterface $workItemContext) /** * @return int|string|WorkflowContextInterface * - * @since Method available since Release 1.1.0 + * @since 1.1.0 */ public function getWorkflowContext() { @@ -192,7 +192,7 @@ private function createWorkflow() * * @return Workflow * - * @since Method available since Release 1.2.0 + * @since 1.2.0 */ private function configureWorkflow(Workflow $workflow) { diff --git a/src/Process/WorkItemContext.php b/src/Process/WorkItemContext.php index 09b8d88..e76cad5 100644 --- a/src/Process/WorkItemContext.php +++ b/src/Process/WorkItemContext.php @@ -66,7 +66,7 @@ public function getActivityId() /** * @param ParticipantInterface $participant * - * @since Method available since Release 1.1.0 + * @since 1.1.0 */ public function setParticipant(ParticipantInterface $participant) { diff --git a/src/Process/WorkflowContextInterface.php b/src/Process/WorkflowContextInterface.php index 1844602..4774bec 100644 --- a/src/Process/WorkflowContextInterface.php +++ b/src/Process/WorkflowContextInterface.php @@ -13,7 +13,7 @@ namespace PHPMentors\Workflower\Process; /** - * @since Interface available since Release 1.1.0 + * @since 1.1.0 */ interface WorkflowContextInterface { diff --git a/src/Workflow/Activity/SendTask.php b/src/Workflow/Activity/SendTask.php index 3801f28..67699fb 100644 --- a/src/Workflow/Activity/SendTask.php +++ b/src/Workflow/Activity/SendTask.php @@ -17,7 +17,7 @@ use PHPMentors\Workflower\Workflow\Resource\MessageInterface; /** - * @since Class available since Release 1.3.0 + * @since 1.3.0 */ class SendTask extends Task implements MessageInterface, OperationalInterface { diff --git a/src/Workflow/Activity/ServiceTask.php b/src/Workflow/Activity/ServiceTask.php index 970d714..4b83b49 100644 --- a/src/Workflow/Activity/ServiceTask.php +++ b/src/Workflow/Activity/ServiceTask.php @@ -16,7 +16,7 @@ use PHPMentors\Workflower\Workflow\Participant\Role; /** - * @since Class available since Release 1.2.0 + * @since 1.2.0 */ class ServiceTask extends Task implements OperationalInterface { diff --git a/src/Workflow/Activity/Task.php b/src/Workflow/Activity/Task.php index a0f86fc..dc0057d 100644 --- a/src/Workflow/Activity/Task.php +++ b/src/Workflow/Activity/Task.php @@ -46,7 +46,7 @@ class Task implements ActivityInterface, \Serializable /** * @var Token * - * @since Property available since Release 2.0.0 + * @since 2.0.0 */ private $token; diff --git a/src/Workflow/Element/ConnectingObjectCollection.php b/src/Workflow/Element/ConnectingObjectCollection.php index fd3b86c..70f9a6f 100644 --- a/src/Workflow/Element/ConnectingObjectCollection.php +++ b/src/Workflow/Element/ConnectingObjectCollection.php @@ -102,7 +102,7 @@ public function filterBySource(TransitionalInterface $flowObject) * * @return ConnectingObjectCollection * - * @since Method available since Release 2.0.0 + * @since 2.0.0 */ public function filterByDestination(TransitionalInterface $flowObject): ConnectingObjectCollection { diff --git a/src/Workflow/Element/FlowObjectInterface.php b/src/Workflow/Element/FlowObjectInterface.php index abc5f05..7d9dde9 100644 --- a/src/Workflow/Element/FlowObjectInterface.php +++ b/src/Workflow/Element/FlowObjectInterface.php @@ -19,7 +19,7 @@ interface FlowObjectInterface extends RoleAwareInterface, WorkflowElementInterfa /** * @param Token $token * - * @since Method available since Release 2.0.0 + * @since 2.0.0 */ public function attachToken(Token $token): void; } diff --git a/src/Workflow/Event/EndEvent.php b/src/Workflow/Event/EndEvent.php index c9ca3d1..9eaa34b 100644 --- a/src/Workflow/Event/EndEvent.php +++ b/src/Workflow/Event/EndEvent.php @@ -19,21 +19,21 @@ class EndEvent extends Event implements EventInterface /** * @var Token * - * @since Property available since Release 2.0.0 + * @since 2.0.0 */ private $token; /** * @var \DateTime * - * @since Property available since Release 2.0.0 + * @since 2.0.0 */ private $endDate; /** * {@inheritdoc} * - * @since Method available since Release 2.0.0 + * @since 2.0.0 */ public function serialize() { diff --git a/src/Workflow/Event/StartEvent.php b/src/Workflow/Event/StartEvent.php index 950eff0..3fcf8cc 100644 --- a/src/Workflow/Event/StartEvent.php +++ b/src/Workflow/Event/StartEvent.php @@ -20,21 +20,21 @@ class StartEvent extends Event implements EventInterface, TransitionalInterface, /** * @var Token * - * @since Property available since Release 2.0.0 + * @since 2.0.0 */ private $token; /** * @var \DateTime * - * @since Property available since Release 2.0.0 + * @since 2.0.0 */ private $startDate; /** * {@inheritdoc} * - * @since Method available since Release 2.0.0 + * @since 2.0.0 */ public function serialize() { diff --git a/src/Workflow/Gateway/ExclusiveGateway.php b/src/Workflow/Gateway/ExclusiveGateway.php index 916639e..7f72a54 100644 --- a/src/Workflow/Gateway/ExclusiveGateway.php +++ b/src/Workflow/Gateway/ExclusiveGateway.php @@ -41,7 +41,7 @@ class ExclusiveGateway implements GatewayInterface, ConditionalInterface, \Seria /** * @var Token * - * @since Property available since Release 2.0.0 + * @since 2.0.0 */ private $token; diff --git a/src/Workflow/Gateway/ParallelGateway.php b/src/Workflow/Gateway/ParallelGateway.php index da455b7..77d3d8a 100644 --- a/src/Workflow/Gateway/ParallelGateway.php +++ b/src/Workflow/Gateway/ParallelGateway.php @@ -16,7 +16,7 @@ use PHPMentors\Workflower\Workflow\Participant\Role; /** - * @since Class available since Release 2.0.0 + * @since 2.0.0 */ class ParallelGateway implements GatewayInterface, \Serializable { diff --git a/src/Workflow/Operation/OperationRunnerInterface.php b/src/Workflow/Operation/OperationRunnerInterface.php index a96833d..890e8b5 100644 --- a/src/Workflow/Operation/OperationRunnerInterface.php +++ b/src/Workflow/Operation/OperationRunnerInterface.php @@ -16,7 +16,7 @@ use PHPMentors\Workflower\Workflow\Workflow; /** - * @since Interface available since Release 1.2.0 + * @since 1.2.0 */ interface OperationRunnerInterface { diff --git a/src/Workflow/Operation/OperationalInterface.php b/src/Workflow/Operation/OperationalInterface.php index c871106..de1fe49 100644 --- a/src/Workflow/Operation/OperationalInterface.php +++ b/src/Workflow/Operation/OperationalInterface.php @@ -13,7 +13,7 @@ namespace PHPMentors\Workflower\Workflow\Operation; /** - * @since Interface available since Release 1.2.0 + * @since 1.2.0 */ interface OperationalInterface { diff --git a/src/Workflow/Resource/MessageInterface.php b/src/Workflow/Resource/MessageInterface.php index 57fa3af..fb3a7da 100644 --- a/src/Workflow/Resource/MessageInterface.php +++ b/src/Workflow/Resource/MessageInterface.php @@ -13,7 +13,7 @@ namespace PHPMentors\Workflower\Workflow\Resource; /** - * @since Interface available since Release 1.3.0 + * @since 1.3.0 */ interface MessageInterface { diff --git a/src/Workflow/Workflow.php b/src/Workflow/Workflow.php index 92ef882..ce42731 100644 --- a/src/Workflow/Workflow.php +++ b/src/Workflow/Workflow.php @@ -40,7 +40,7 @@ class Workflow implements \Serializable /** * @var int|string * - * @since Property available since Release 2.0.0 + * @since 2.0.0 */ private $id; @@ -67,14 +67,14 @@ class Workflow implements \Serializable /** * @var StartEvent * - * @since Property available since Release 2.0.0 + * @since 2.0.0 */ private $startEvent; /** * @var EndEvent[] * - * @since Property available since Release 2.0.0 + * @since 2.0.0 */ private $endEvents = []; @@ -86,28 +86,28 @@ class Workflow implements \Serializable /** * @var ExpressionLanguage * - * @since Property available since Release 1.1.0 + * @since 1.1.0 */ private $expressionLanguage; /** * @var OperationRunnerInterface * - * @since Property available since Release 1.2.0 + * @since 1.2.0 */ private $operationRunner; /** * @var Token[] * - * @since Property available since Release 2.0.0 + * @since 2.0.0 */ private $tokens = []; /** * @var ActivityLogCollection * - * @since Property available since Release 2.0.0 + * @since 2.0.0 */ private $activityLogCollection; @@ -301,7 +301,7 @@ public function getCurrentFlowObject(): ?FlowObjectInterface /** * @return FlowObjectInterface[] * - * @since Method available since Release 2.0.0 + * @since 2.0.0 */ public function getCurrentFlowObjects(): iterable { @@ -327,7 +327,7 @@ public function getPreviousFlowObject(): ?FlowObjectInterface /** * @return FlowObjectInterface[] * - * @since Method available since Release 2.0.0 + * @since 2.0.0 */ public function getPreviousFlowObjects(): iterable { @@ -395,7 +395,7 @@ public function setProcessData(array $processData) /** * @return array * - * @since Method available since Release 1.2.0 + * @since 1.2.0 */ public function getProcessData() { @@ -405,7 +405,7 @@ public function getProcessData() /** * @param ExpressionLanguage $expressionLanguage * - * @since Method available since Release 1.1.0 + * @since 1.1.0 */ public function setExpressionLanguage(ExpressionLanguage $expressionLanguage) { @@ -415,7 +415,7 @@ public function setExpressionLanguage(ExpressionLanguage $expressionLanguage) /** * @param OperationRunnerInterface $operationRunner * - * @since Method available since Release 1.2.0 + * @since 1.2.0 */ public function setOperationRunner(OperationRunnerInterface $operationRunner) { @@ -529,7 +529,7 @@ private function assertCurrentFlowObjectIsExpectedActivity(ActivityInterface $ac } /** - * @since Method available since Release 1.2.0 + * @since 1.2.0 * * @param ActivityInterface $operational */ @@ -547,7 +547,7 @@ private function executeOperationalActivity(ActivityInterface $operational) * @return Token * @throws \Exception * - * @since Method available since Release 2.0.0 + * @since 2.0.0 */ private function generateToken(FlowObjectInterface $flowObject): Token { @@ -557,7 +557,7 @@ private function generateToken(FlowObjectInterface $flowObject): Token /** * @param Token $token * - * @since Method available since Release 2.0.0 + * @since 2.0.0 */ private function removeToken(Token $token): void { @@ -571,7 +571,7 @@ private function removeToken(Token $token): void * @param FlowObjectInterface $flowObject * @throws \Exception * - * @since Method available since Release 2.0.0 + * @since 2.0.0 */ private function flow(Token $token, FlowObjectInterface $flowObject): void { diff --git a/src/Workflow/WorkflowBuilder.php b/src/Workflow/WorkflowBuilder.php index 2e0515d..1e1a86a 100644 --- a/src/Workflow/WorkflowBuilder.php +++ b/src/Workflow/WorkflowBuilder.php @@ -40,7 +40,7 @@ class WorkflowBuilder /** * @var array * - * @since Property available since Release 2.0.0 + * @since 2.0.0 */ private $parallelGateways = []; @@ -67,14 +67,14 @@ class WorkflowBuilder /** * @var array * - * @since Property available since Release 1.2.0 + * @since 1.2.0 */ private $serviceTasks = []; /** * @var array * - * @since Property available since Release 1.3.0 + * @since 1.3.0 */ private $sendTasks = []; @@ -219,7 +219,7 @@ public function addTask($id, $participant, $name = null, $defaultSequenceFlow = * @param string $name * @param int|string $defaultSequenceFlow * - * @since Method available since Release 1.2.0 + * @since 1.2.0 */ public function addServiceTask($id, $participant, $operation, $name = null, $defaultSequenceFlow = null) { @@ -238,7 +238,7 @@ public function addServiceTask($id, $participant, $operation, $name = null, $def * @param string $name * @param int|string $defaultSequenceFlow * - * @since Method available since Release 1.3.0 + * @since 1.3.0 */ public function addSendTask($id, $participant, $message, $operation, $name = null, $defaultSequenceFlow = null) { diff --git a/tests/Definition/Bpmn2ReaderTest.php b/tests/Definition/Bpmn2ReaderTest.php index b9af656..0708b56 100644 --- a/tests/Definition/Bpmn2ReaderTest.php +++ b/tests/Definition/Bpmn2ReaderTest.php @@ -32,7 +32,7 @@ public function read() /** * @test * - * @since Method available since Release 1.3.0 + * @since 1.3.0 */ public function readSource() { diff --git a/tests/Workflow/WorkflowRepository.php b/tests/Workflow/WorkflowRepository.php index 1b65d5b..871d39c 100644 --- a/tests/Workflow/WorkflowRepository.php +++ b/tests/Workflow/WorkflowRepository.php @@ -119,7 +119,7 @@ private function createMultipleWorkItemsProcess() /** * @return Workflow * - * @since Method available since Release 1.2.0 + * @since 1.2.0 */ private function createServiceTasksProcess() { @@ -131,7 +131,7 @@ private function createServiceTasksProcess() /** * @return Workflow * - * @since Method available since Release 1.3.0 + * @since 1.3.0 */ private function createNoLanesProcess() { @@ -143,7 +143,7 @@ private function createNoLanesProcess() /** * @return Workflow * - * @since Method available since Release 1.3.0 + * @since 1.3.0 */ private function createSendTasksProcess() { @@ -155,7 +155,7 @@ private function createSendTasksProcess() /** * @return Workflow * - * @since Method available since Release 2.0.0 + * @since 2.0.0 */ private function createParallelGatewayProcess(): Workflow { @@ -167,7 +167,7 @@ private function createParallelGatewayProcess(): Workflow /** * @return Workflow * - * @since Method available since Release 2.0.0 + * @since 2.0.0 */ private function createMultipleEndEventsProcess(): Workflow { diff --git a/tests/Workflow/WorkflowTest.php b/tests/Workflow/WorkflowTest.php index ec58036..7720b00 100644 --- a/tests/Workflow/WorkflowTest.php +++ b/tests/Workflow/WorkflowTest.php @@ -376,7 +376,7 @@ public function getActivityLogWithMultipleExecutionsOfSameActivity() /** * @test * - * @since Method available since Release 1.2.0 + * @since 1.2.0 */ public function executeServiceTasks() { @@ -405,7 +405,7 @@ public function executeServiceTasks() /** * @test * - * @since Method available since Release 1.3.0 + * @since 1.3.0 */ public function provideDefaultRoleForWorkflowWithoutLanes() { @@ -426,7 +426,7 @@ public function provideDefaultRoleForWorkflowWithoutLanes() /** * @test * - * @since Method available since Release 1.3.0 + * @since 1.3.0 */ public function executeSendTasks() { @@ -457,7 +457,7 @@ public function executeSendTasks() /** * @test * - * @since Method available since Release 2.0.0 + * @since 2.0.0 */ public function parallelGateway() { @@ -515,7 +515,7 @@ public function parallelGateway() * "All the tokens that were generated within the Process MUST be consumed by an End Event before the Process has been completed." * --Business Process Model and Notation, v2.0 https://www.omg.org/spec/BPMN/2.0 p.246 * - * @since Method available since Release 2.0.0 + * @since 2.0.0 */ public function multipleEndEvents() { From c6153b2445831e656eedc900a8995d05d59976e1 Mon Sep 17 00:00:00 2001 From: Atsuhiro Kubo Date: Tue, 12 Jan 2021 19:04:22 +0900 Subject: [PATCH 2/5] introduce `TokenRegistry` --- src/Workflow/TokenRegistry.php | 46 ++++++++++++++++++++++++++ src/Workflow/Workflow.php | 59 ++++++++-------------------------- 2 files changed, 59 insertions(+), 46 deletions(-) create mode 100644 src/Workflow/TokenRegistry.php diff --git a/src/Workflow/TokenRegistry.php b/src/Workflow/TokenRegistry.php new file mode 100644 index 0000000..1844a1f --- /dev/null +++ b/src/Workflow/TokenRegistry.php @@ -0,0 +1,46 @@ + and contributors, + * All rights reserved. + * + * This file is part of Workflower. + * + * This program and the accompanying materials are made available under + * the terms of the BSD 2-Clause License which accompanies this + * distribution, and is available at http://opensource.org/licenses/BSD-2-Clause + */ + +namespace PHPMentors\Workflower\Workflow; + +use PHPMentors\Workflower\Workflow\Element\Token; +use PHPMentors\Workflower\Workflow\Event\EndEvent; + +/** + * @since 2.0.0 + */ +class TokenRegistry +{ + private $tokens = []; + + public function register(Token $token): void + { + $this->tokens[$token->getId()] = $token; + } + + public function remove(Token $token): void + { + unset($this->tokens[$token->getId()]); + } + + public function getTokens(): array + { + return array_values($this->tokens); + } + + public function getActiveTokens(): array + { + return array_values(array_filter($this->tokens, function (Token $token) { + return !($token->getCurrentFlowObject() instanceof EndEvent); + })); + } +} diff --git a/src/Workflow/Workflow.php b/src/Workflow/Workflow.php index ce42731..3c30fe4 100644 --- a/src/Workflow/Workflow.php +++ b/src/Workflow/Workflow.php @@ -98,11 +98,11 @@ class Workflow implements \Serializable private $operationRunner; /** - * @var Token[] + * @var TokenRegistry * * @since 2.0.0 */ - private $tokens = []; + private $tokenRegistry; /** * @var ActivityLogCollection @@ -122,6 +122,7 @@ public function __construct($id, $name) $this->connectingObjectCollection = new ConnectingObjectCollection(); $this->flowObjectCollection = new FlowObjectCollection(); $this->roleCollection = new RoleCollection(); + $this->tokenRegistry = new TokenRegistry(); $this->activityLogCollection = new ActivityLogCollection(); } @@ -138,7 +139,7 @@ public function serialize() 'roleCollection' => $this->roleCollection, 'startEvent' => $this->startEvent, 'endEvents' => $this->endEvents, - 'tokens' => $this->tokens, + 'tokenRegistry' => $this->tokenRegistry, 'activityLogCollection' => $this->activityLogCollection, ]); } @@ -252,17 +253,7 @@ public function getRole($id) */ public function isActive() { - if (count($this->tokens) == 0) { - return false; - } - - foreach ($this->tokens as $token) { - if (!($token->getCurrentFlowObject() instanceof EndEvent)) { - return true; - } - } - - return false; + return count($this->tokenRegistry->getTokens()) > 0 && count($this->tokenRegistry->getActiveTokens()) > 0; } /** @@ -270,19 +261,7 @@ public function isActive() */ public function isEnded() { - if (count($this->tokens) == 0) { - return false; - } - - foreach ($this->tokens as $token) { - if ($token->getCurrentFlowObject() instanceof EndEvent) { - continue; - } - - return false; - } - - return true; + return count($this->tokenRegistry->getTokens()) > 0 && count($this->tokenRegistry->getActiveTokens()) == 0; } /** @@ -303,11 +282,11 @@ public function getCurrentFlowObject(): ?FlowObjectInterface * * @since 2.0.0 */ - public function getCurrentFlowObjects(): iterable + public function getCurrentFlowObjects(): array { return array_map(function (Token $token) { return $token->getCurrentFlowObject(); - }, $this->tokens + }, $this->tokenRegistry->getTokens() ); } @@ -329,11 +308,11 @@ public function getPreviousFlowObject(): ?FlowObjectInterface * * @since 2.0.0 */ - public function getPreviousFlowObjects(): iterable + public function getPreviousFlowObjects(): array { return array_map(function (Token $token) { return $token->getPreviousFlowObject(); - }, $this->tokens + }, $this->tokenRegistry->getTokens() ); } @@ -343,7 +322,7 @@ public function getPreviousFlowObjects(): iterable public function start(StartEvent $event) { $this->startEvent = $event; - $this->tokens[] = $this->generateToken($this->startEvent); + $this->tokenRegistry->register($this->generateToken($this->startEvent)); $this->selectSequenceFlow($this->startEvent); } @@ -554,18 +533,6 @@ private function generateToken(FlowObjectInterface $flowObject): Token return new Token(sha1(random_bytes(24)), $flowObject); } - /** - * @param Token $token - * - * @since 2.0.0 - */ - private function removeToken(Token $token): void - { - $this->tokens = array_filter($this->tokens, function (Token $currentToken) use ($token) { - return $currentToken !== $token; - }); - } - /** * @param Token $token * @param FlowObjectInterface $flowObject @@ -586,12 +553,12 @@ private function flow(Token $token, FlowObjectInterface $flowObject): void if (count($incomingTokens) == count($incomings)) { foreach ($incomingTokens as $incomingToken) { $parallelGateway->detachToken($incomingToken); - $this->removeToken($incomingToken); + $this->tokenRegistry->remove($incomingToken); } foreach ($this->connectingObjectCollection->filterBySource($parallelGateway) as $outgoing) { $outgoingToken = $this->generateToken($parallelGateway); - $this->tokens[] = $outgoingToken; + $this->tokenRegistry->register($outgoingToken); $this->flow($outgoingToken, $outgoing->getDestination()); } } From 64d6cdcfa8bdc7aab32d85564d2f180207fc5e52 Mon Sep 17 00:00:00 2001 From: Atsuhiro Kubo Date: Tue, 12 Jan 2021 19:11:05 +0900 Subject: [PATCH 3/5] introduce `Workflow::getActiveFlowObjects()` --- src/Workflow/Workflow.php | 19 ++++++++++-- tests/Workflow/WorkflowTest.php | 51 +++++++++++++++++---------------- 2 files changed, 42 insertions(+), 28 deletions(-) diff --git a/src/Workflow/Workflow.php b/src/Workflow/Workflow.php index 3c30fe4..5e25984 100644 --- a/src/Workflow/Workflow.php +++ b/src/Workflow/Workflow.php @@ -264,6 +264,19 @@ public function isEnded() return count($this->tokenRegistry->getTokens()) > 0 && count($this->tokenRegistry->getActiveTokens()) == 0; } + /** + * @return FlowObjectInterface[] + * + * @since 2.0.0 + */ + public function getActiveFlowObjects(): array + { + return array_map(function (Token $token) { + return $token->getCurrentFlowObject(); + }, $this->tokenRegistry->getActiveTokens() + ); + } + /** * @return FlowObjectInterface|null */ @@ -498,13 +511,13 @@ private function assertParticipantHasRole(ActivityInterface $activity, Participa */ private function assertCurrentFlowObjectIsExpectedActivity(ActivityInterface $activity) { - foreach ($this->getCurrentFlowObjects() as $currentFlowObject) { - if ($activity->equals($currentFlowObject)) { + foreach ($this->getActiveFlowObjects() as $activeFlowObject) { + if ($activity->equals($activeFlowObject)) { return true; } } - throw new UnexpectedActivityException(sprintf('The current flow object is not equal to the expected activity "%s".', $activity->getId())); + throw new UnexpectedActivityException(sprintf('The activity "%s" is not found in the active flow objects.', $activity->getId())); } /** diff --git a/tests/Workflow/WorkflowTest.php b/tests/Workflow/WorkflowTest.php index 7720b00..25bea3e 100644 --- a/tests/Workflow/WorkflowTest.php +++ b/tests/Workflow/WorkflowTest.php @@ -524,55 +524,56 @@ public function multipleEndEvents() $workflow = $this->workflowRepository->findById('MultipleEndEventsProcess'); $workflow->start($workflow->getFlowObject('Start')); - $currentFlowObjects = $workflow->getCurrentFlowObjects(); + $activeFlowObjects = $workflow->getActiveFlowObjects(); - $this->assertThat(count($currentFlowObjects), $this->equalTo(3)); + $this->assertThat(count($activeFlowObjects), $this->equalTo(3)); - foreach ($workflow->getCurrentFlowObjects() as $currentFlowObject) { - if ($currentFlowObject->getId() == 'Task1') { - $workflow->allocateWorkItem($currentFlowObject, $participant); - $workflow->startWorkItem($currentFlowObject, $participant); - $workflow->completeWorkItem($currentFlowObject, $participant); + foreach ($activeFlowObjects as $activeFlowObject) { + if ($activeFlowObject->getId() == 'Task1') { + $workflow->allocateWorkItem($activeFlowObject, $participant); + $workflow->startWorkItem($activeFlowObject, $participant); + $workflow->completeWorkItem($activeFlowObject, $participant); break; } } + $activeFlowObjects = $workflow->getActiveFlowObjects(); + $this->assertThat($workflow->isActive(), $this->isTrue()); $this->assertThat($workflow->isEnded(), $this->isFalse()); + $this->assertThat(count($activeFlowObjects), $this->equalTo(2)); - $currentFlowObjects = $workflow->getCurrentFlowObjects(); - - $this->assertThat(count($currentFlowObjects), $this->equalTo(3)); - - foreach ($workflow->getCurrentFlowObjects() as $currentFlowObject) { - if ($currentFlowObject->getId() == 'Task2') { - $workflow->allocateWorkItem($currentFlowObject, $participant); - $workflow->startWorkItem($currentFlowObject, $participant); - $workflow->completeWorkItem($currentFlowObject, $participant); + foreach ($activeFlowObjects as $activeFlowObject) { + if ($activeFlowObject->getId() == 'Task2') { + $workflow->allocateWorkItem($activeFlowObject, $participant); + $workflow->startWorkItem($activeFlowObject, $participant); + $workflow->completeWorkItem($activeFlowObject, $participant); break; } } + $activeFlowObjects = $workflow->getActiveFlowObjects(); + $this->assertThat($workflow->isActive(), $this->isTrue()); $this->assertThat($workflow->isEnded(), $this->isFalse()); + $this->assertThat(count($activeFlowObjects), $this->equalTo(1)); - $currentFlowObjects = $workflow->getCurrentFlowObjects(); - - $this->assertThat(count($currentFlowObjects), $this->equalTo(3)); - - foreach ($workflow->getCurrentFlowObjects() as $currentFlowObject) { - if ($currentFlowObject->getId() == 'Task3') { - $workflow->allocateWorkItem($currentFlowObject, $participant); - $workflow->startWorkItem($currentFlowObject, $participant); - $workflow->completeWorkItem($currentFlowObject, $participant); + foreach ($activeFlowObjects as $activeFlowObject) { + if ($activeFlowObject->getId() == 'Task3') { + $workflow->allocateWorkItem($activeFlowObject, $participant); + $workflow->startWorkItem($activeFlowObject, $participant); + $workflow->completeWorkItem($activeFlowObject, $participant); break; } } + $activeFlowObjects = $workflow->getActiveFlowObjects(); + $this->assertThat($workflow->isActive(), $this->isFalse()); $this->assertThat($workflow->isEnded(), $this->isTrue()); + $this->assertThat(count($activeFlowObjects), $this->equalTo(0)); } } From c3b550783271d4cc84c794872c9a0cf894c9a80e Mon Sep 17 00:00:00 2001 From: Atsuhiro Kubo Date: Tue, 12 Jan 2021 19:36:02 +0900 Subject: [PATCH 4/5] WIP: mark `Workflow:getCurrentFlowObject()` and `Workflow:getPreviousFlowObject()` as deprecated --- src/Process/Process.php | 4 ++-- src/Workflow/Workflow.php | 4 ++++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Process/Process.php b/src/Process/Process.php index 31943e4..7b4aa11 100644 --- a/src/Process/Process.php +++ b/src/Process/Process.php @@ -143,14 +143,14 @@ public function executeWorkItem(WorkItemContextInterface $workItemContext) if ($activity->isAllocatable()) { $this->allocateWorkItem($workItemContext); $nextWorkItemContext = new WorkItemContext($workItemContext->getParticipant()); - $nextWorkItemContext->setActivityId($workItemContext->getProcessContext()->getWorkflow()->getCurrentFlowObject()->getId()); + $nextWorkItemContext->setActivityId($workItemContext->getActivityId()); $nextWorkItemContext->setProcessContext($workItemContext->getProcessContext()); return $this->executeWorkItem($nextWorkItemContext); } elseif ($activity->isStartable()) { $this->startWorkItem($workItemContext); $nextWorkItemContext = new WorkItemContext($workItemContext->getParticipant()); - $nextWorkItemContext->setActivityId($workItemContext->getProcessContext()->getWorkflow()->getCurrentFlowObject()->getId()); + $nextWorkItemContext->setActivityId($workItemContext->getActivityId()); $nextWorkItemContext->setProcessContext($workItemContext->getProcessContext()); return $this->executeWorkItem($nextWorkItemContext); diff --git a/src/Workflow/Workflow.php b/src/Workflow/Workflow.php index 5e25984..f097d7d 100644 --- a/src/Workflow/Workflow.php +++ b/src/Workflow/Workflow.php @@ -279,6 +279,8 @@ public function getActiveFlowObjects(): array /** * @return FlowObjectInterface|null + * + * @deprecated 2.0.0 */ public function getCurrentFlowObject(): ?FlowObjectInterface { @@ -305,6 +307,8 @@ public function getCurrentFlowObjects(): array /** * @return FlowObjectInterface|null + * + * @deprecated 2.0.0 */ public function getPreviousFlowObject(): ?FlowObjectInterface { From 510fa9f9d71a0610d9610295bc6a815d8817f6f6 Mon Sep 17 00:00:00 2001 From: Atsuhiro Kubo Date: Tue, 12 Jan 2021 19:44:27 +0900 Subject: [PATCH 5/5] WIP: mark `Workflow:getCurrentFlowObject()` and `Workflow:getPreviousFlowObject()` as deprecated --- tests/Workflow/WorkflowTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/Workflow/WorkflowTest.php b/tests/Workflow/WorkflowTest.php index 25bea3e..880aec4 100644 --- a/tests/Workflow/WorkflowTest.php +++ b/tests/Workflow/WorkflowTest.php @@ -44,8 +44,9 @@ public function isInBeforeStarting() $this->assertThat($workflow->isActive(), $this->isFalse()); $this->assertThat($workflow->isEnded(), $this->isFalse()); - $this->assertThat($workflow->getCurrentFlowObject(), $this->isNull()); - $this->assertThat($workflow->getPreviousFlowObject(), $this->isNull()); + $this->assertThat(count($workflow->getCurrentFlowObjects()), $this->equalTo(0)); + $this->assertThat(count($workflow->getPreviousFlowObjects()), $this->equalTo(0)); + $this->assertThat(count($workflow->getActiveFlowObjects()), $this->equalTo(0)); } /**