|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ProcessMaker\Nayra\Services; |
| 4 | + |
| 5 | +use DOMDocument; |
| 6 | +use DOMXPath; |
| 7 | +use Exception; |
| 8 | + |
| 9 | +class FixBpmnSchemaService |
| 10 | +{ |
| 11 | + /** |
| 12 | + * Fix BPMN definition if is necesasary |
| 13 | + * |
| 14 | + * @param string $bpmn |
| 15 | + * @return string |
| 16 | + * @throws \Exception |
| 17 | + */ |
| 18 | + public static function fix(string $bpmn): string |
| 19 | + { |
| 20 | + try { |
| 21 | + // Create main object |
| 22 | + $document = new DOMDocument(); |
| 23 | + $document->preserveWhiteSpace = false; |
| 24 | + $document->loadXml($bpmn); |
| 25 | + |
| 26 | + // Get root node |
| 27 | + $root = $document->documentElement; |
| 28 | + |
| 29 | + // Set "targetNamespace" attribute always |
| 30 | + $root->setAttribute('targetNamespace', 'http://www.omg.org/spec/BPMN/20100524/MODEL'); |
| 31 | + |
| 32 | + // Create XPath object |
| 33 | + $xpath = new DOMXPath($document); |
| 34 | + |
| 35 | + // Register all namespaces, including default |
| 36 | + foreach ($document->documentElement->attributes as $attr) { |
| 37 | + if (strpos($attr->nodeName, 'xmlns:') === 0) { |
| 38 | + $prefix = substr($attr->nodeName, 6); |
| 39 | + $xpath->registerNamespace($prefix, $attr->nodeValue); |
| 40 | + } elseif ($attr->nodeName === 'xmlns') { |
| 41 | + // Register default namespace as "def" |
| 42 | + $xpath->registerNamespace('def', $attr->nodeValue); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + // Find all task nodes |
| 47 | + $taskNodes = $xpath->query('//*[local-name()="task"]'); |
| 48 | + |
| 49 | + foreach ($taskNodes as $task) { |
| 50 | + $taskId = $task->getAttribute("id"); |
| 51 | + $taskPrefix = !empty($task->prefix) ? "$task->prefix:" : ''; |
| 52 | + $taskNS = $task->namespaceURI; |
| 53 | + |
| 54 | + $dataInputAssociation = $xpath->query('./*[local-name()="dataInputAssociation"]', $task)->item(0); |
| 55 | + if (!$dataInputAssociation) { |
| 56 | + continue; |
| 57 | + } |
| 58 | + |
| 59 | + // Skip if targetRef already exists |
| 60 | + $targetRefExists = $xpath->query('./*[local-name()="targetRef"]', $dataInputAssociation)->length > 0; |
| 61 | + if ($targetRefExists) { |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + // Extract sourceRef |
| 66 | + $sourceRef = $xpath->query('./*[local-name()="sourceRef"]', $dataInputAssociation)->item(0); |
| 67 | + if (!$sourceRef) { |
| 68 | + throw new Exception("sourceRef not found in dataInputAssociation for task $taskId"); |
| 69 | + } |
| 70 | + $sourceId = $sourceRef->nodeValue; |
| 71 | + |
| 72 | + // Create ioSpecification and children |
| 73 | + $ioSpec = $document->createElementNS($taskNS, "{$taskPrefix}ioSpecification"); |
| 74 | + $ioSpecId = "{$taskId}_inner_" . round(microtime(true) * 1000); |
| 75 | + $ioSpec->setAttribute("id", $ioSpecId); |
| 76 | + |
| 77 | + $dataInputId = "data_input_{$sourceId}"; |
| 78 | + $dataInput = $document->createElementNS($taskNS, "{$taskPrefix}dataInput"); |
| 79 | + $dataInput->setAttribute("id", $dataInputId); |
| 80 | + $dataInput->setAttribute("name", "Template for protocol"); |
| 81 | + $ioSpec->appendChild($dataInput); |
| 82 | + |
| 83 | + $inputSet = $document->createElementNS($taskNS, "{$taskPrefix}inputSet"); |
| 84 | + $inputSet->setAttribute("id", "{$taskId}_inner_" . (round(microtime(true) * 1000) + 2)); |
| 85 | + $dataInputRefs = $document->createElementNS($taskNS, "{$taskPrefix}dataInputRefs", $dataInputId); |
| 86 | + $inputSet->appendChild($dataInputRefs); |
| 87 | + $ioSpec->appendChild($inputSet); |
| 88 | + |
| 89 | + $outputSet = $document->createElementNS($taskNS, "{$taskPrefix}outputSet"); |
| 90 | + $outputSet->setAttribute("id", "{$taskId}_inner_" . (round(microtime(true) * 1000) + 3)); |
| 91 | + $ioSpec->appendChild($outputSet); |
| 92 | + |
| 93 | + $task->insertBefore($ioSpec, $dataInputAssociation); |
| 94 | + |
| 95 | + // Add targetRef |
| 96 | + $targetRef = $document->createElementNS($taskNS, "{$taskPrefix}targetRef", $dataInputId); |
| 97 | + $dataInputAssociation->appendChild($targetRef); |
| 98 | + |
| 99 | + // Add BPMNEdge to BPMNDiagram |
| 100 | + $diagramNodes = $xpath->query('//*[local-name() = "BPMNDiagram"]'); |
| 101 | + if ($diagramNodes->length === 0) { |
| 102 | + throw new Exception("No BPMNDiagram node found in the BPMN file."); |
| 103 | + } |
| 104 | + $bpmnDiagram = $diagramNodes->item(0); |
| 105 | + $diagramPrefix = $bpmnDiagram->prefix; |
| 106 | + $diagramNS = $bpmnDiagram->namespaceURI; |
| 107 | + |
| 108 | + $bpmnPlaneNodes = $xpath->query('.//*[local-name() = "BPMNPlane"]', $bpmnDiagram); |
| 109 | + if ($bpmnPlaneNodes->length === 0) { |
| 110 | + throw new Exception("No BPMNPlane found inside BPMNDiagram."); |
| 111 | + } |
| 112 | + $bpmnPlane = $bpmnPlaneNodes->item(0); |
| 113 | + |
| 114 | + $edgeId = 'BPMNEdge_' . $dataInputAssociation->getAttribute("id"); |
| 115 | + $bpmnEdge = $document->createElementNS($diagramNS, "{$diagramPrefix}:BPMNEdge"); |
| 116 | + $bpmnEdge->setAttribute("id", $edgeId); |
| 117 | + $bpmnEdge->setAttribute("bpmnElement", $dataInputAssociation->getAttribute("id")); |
| 118 | + |
| 119 | + $diNS = 'http://www.omg.org/spec/DD/20100524/DI'; |
| 120 | + $waypoint1 = $document->createElementNS($diNS, "di:waypoint"); |
| 121 | + $waypoint1->setAttribute("x", "100"); |
| 122 | + $waypoint1->setAttribute("y", "100"); |
| 123 | + |
| 124 | + $waypoint2 = $document->createElementNS($diNS, "di:waypoint"); |
| 125 | + $waypoint2->setAttribute("x", "200"); |
| 126 | + $waypoint2->setAttribute("y", "200"); |
| 127 | + |
| 128 | + $bpmnEdge->appendChild($waypoint1); |
| 129 | + $bpmnEdge->appendChild($waypoint2); |
| 130 | + $bpmnPlane->appendChild($bpmnEdge); |
| 131 | + } |
| 132 | + |
| 133 | + return $document->saveXml(); |
| 134 | + } catch (Exception $e) { |
| 135 | + throw $e; |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments