|
3 | 3 | namespace ProcessMaker\Nayra\Bpmn\Models; |
4 | 4 |
|
5 | 5 | use ProcessMaker\Nayra\Bpmn\EventDefinitionTrait; |
| 6 | +use ProcessMaker\Nayra\Contracts\Bpmn\CatchEventInterface; |
| 7 | +use ProcessMaker\Nayra\Contracts\Bpmn\CollectionInterface; |
| 8 | +use ProcessMaker\Nayra\Contracts\Bpmn\DataStoreInterface; |
6 | 9 | use ProcessMaker\Nayra\Contracts\Bpmn\EventDefinitionInterface; |
7 | 10 | use ProcessMaker\Nayra\Contracts\Bpmn\FlowNodeInterface; |
8 | 11 | use ProcessMaker\Nayra\Contracts\Bpmn\MessageEventDefinitionInterface; |
@@ -90,47 +93,78 @@ public function assertsRule(EventDefinitionInterface $event, FlowNodeInterface $ |
90 | 93 | public function execute(EventDefinitionInterface $event, FlowNodeInterface $target, ExecutionInstanceInterface $instance = null, TokenInterface $token = null) |
91 | 94 | { |
92 | 95 | $throwEvent = $token->getOwnerElement(); |
93 | | - $this->evaluateMessagePayload($throwEvent, $token, $instance); |
| 96 | + $this->executeMessageMapping($throwEvent, $target, $instance, $token); |
94 | 97 | return $this; |
95 | 98 | } |
96 | 99 |
|
97 | 100 | /** |
98 | | - * Evaluate the message payload |
| 101 | + * Map a message payload from a ThrowEvent through an optional CatchEvent mapping |
| 102 | + * into the instance data store. |
99 | 103 | * |
100 | 104 | * @param ThrowEventInterface $throwEvent |
| 105 | + * @param CatchEventInterface $catchEvent |
| 106 | + * @param ExecutionInstanceInterface $instance |
101 | 107 | * @param TokenInterface $token |
102 | | - * @param ExecutionInstanceInterface $targetInstance |
| 108 | + * |
| 109 | + * @return void |
| 110 | + */ |
| 111 | + private function executeMessageMapping(ThrowEventInterface $throwEvent, CatchEventInterface $catchEvent, ExecutionInstanceInterface $instance, TokenInterface $token): void |
| 112 | + { |
| 113 | + $sourceMaps = $throwEvent->getDataInputAssociations(); |
| 114 | + $targetMaps = $catchEvent->getDataOutputAssociations(); |
| 115 | + $targetStore = $instance->getDataStore(); |
| 116 | + |
| 117 | + // Source of data is the token's instance store if present; otherwise a fresh store. |
| 118 | + $sourceStore = $token->getInstance()?->getDataStore() ?? new DataStore(); |
| 119 | + |
| 120 | + // If target mappings exist we stage into a buffer; otherwise write straight to the instance store. |
| 121 | + $bufferStore = !count($targetMaps) ? $targetStore : new DataStore(); |
| 122 | + |
| 123 | + // 1) Source mappings: source → buffer/instance |
| 124 | + $this->evaluateMessagePayload($sourceMaps, $sourceStore, $bufferStore); |
| 125 | + |
| 126 | + // 2) Optional target mappings: buffer → instance |
| 127 | + if (count($targetMaps)) { |
| 128 | + $this->evaluateMessagePayload($targetMaps, $bufferStore, $targetStore); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Evaluate the message payload |
| 134 | + * |
| 135 | + * @param CollectionInterface $associations |
| 136 | + * @param DataStoreInterface $sourceStore |
| 137 | + * @param DataStoreInterface $targetStore |
| 138 | + * |
103 | 139 | * @return void |
104 | 140 | */ |
105 | | - private function evaluateMessagePayload(ThrowEventInterface $throwEvent, TokenInterface $token, ExecutionInstanceInterface $targetInstance) |
| 141 | + private function evaluateMessagePayload(CollectionInterface $associations, DataStoreInterface $sourceStore, DataStoreInterface $targetStore): void |
106 | 142 | { |
107 | | - // Initialize message payload |
108 | | - $payload = []; |
109 | | - $associations = $throwEvent->getDataInputAssociations(); |
110 | | - // Get data from source token instance or empty one if not found |
111 | | - $sourceDataStore = $token->getInstance()?->getDataStore() ?? new DataStore(); |
| 143 | + $assignments = []; |
112 | 144 |
|
113 | | - // Associate data inputs to message payload |
114 | 145 | foreach ($associations as $association) { |
115 | | - $data = $sourceDataStore->getData(); |
116 | 146 | $source = $association->getSource(); |
117 | 147 | $target = $association->getTarget(); |
118 | 148 |
|
119 | | - // Add reference to source |
120 | 149 | $hasSource = $source && $source->getName(); |
121 | 150 | $hasTarget = $target && $target->getName(); |
122 | | - $data['sourceRef'] = $hasSource ? $sourceDataStore->getDotData($source->getName()) : null; |
123 | 151 |
|
124 | | - // Apply transformation |
125 | | - $this->applyTransformation($association, $data, $payload, $hasTarget, $hasSource); |
| 152 | + // Base data always starts from full source store |
| 153 | + $data = $sourceStore->getData(); |
| 154 | + |
| 155 | + // Optionally add a direct reference to the source value |
| 156 | + if ($hasSource) { |
| 157 | + $data['sourceRef'] = $sourceStore->getDotData($source->getName()); |
| 158 | + } |
126 | 159 |
|
127 | | - // Evaluate assignments |
128 | | - $this->evaluateAssignments($association, $data, $payload); |
| 160 | + // Transformation and assignments build up the assignments list |
| 161 | + $this->applyTransformation($association, $data, $assignments, $hasTarget, $hasSource); |
| 162 | + $this->evaluateAssignments($association, $data, $assignments); |
129 | 163 | } |
130 | | - // Update data into target $instance |
131 | | - $dataStore = $targetInstance->getDataStore(); |
132 | | - foreach ($payload as $load) { |
133 | | - $dataStore->setDotData($load['key'], $load['value']); |
| 164 | + |
| 165 | + // Flush all assignments into target store |
| 166 | + foreach ($assignments as $assignment) { |
| 167 | + $targetStore->setDotData($assignment['key'], $assignment['value']); |
134 | 168 | } |
135 | 169 | } |
136 | 170 |
|
|
0 commit comments