import re with open('WorkflowEngine.php', 'r') as f: content = f.read() new_method = """ public function createNewInstance(int $personId, int $processDefinitionId, int $userId, array $context = []): array { if (!is_int($processDefinitionId) || $processDefinitionId <= 0) { throw new InvalidArgumentException("processDefinitionId must be a positive integer."); } if (!is_int($personId) || $personId <= 0) { throw new InvalidArgumentException("personId must be a positive integer."); } $stmt_def = $this->pdo->prepare("SELECT definition_json, code, is_active FROM process_definitions WHERE id = ?"); $stmt_def->execute([$processDefinitionId]); $definition = $stmt_def->fetch(PDO::FETCH_ASSOC); if (!$definition) { throw new WorkflowNotFoundException("Process definition #$processDefinitionId not found."); } if (empty($definition['is_active'])) { throw new WorkflowNotAllowedException("Process is not active and cannot be started."); } $eligibility = $this->checkEligibility($personId, $processDefinitionId, $context); if (!$eligibility['is_eligible']) { throw new WorkflowEligibilityException("Person is not eligible to start this process.", $eligibility['reasons']); } $definition_json = !empty($definition['definition_json']) ? json_decode($definition['definition_json'], true) : []; $start_node = $definition_json['start_node_id'] ?? 'start'; $initial_data_map = $definition_json['initial_data'] ?? []; $data_json = !empty($initial_data_map) ? json_encode($initial_data_map) : null; $stmt = $this->pdo->prepare( "INSERT INTO process_instances (person_id, process_definition_id, current_node_id, current_status, data_json, last_activity_at) VALUES (?, ?, ?, 'in_progress', ?, NOW())" ); $stmt->execute([$personId, $processDefinitionId, $start_node, $data_json]); $newInstanceId = $this->pdo->lastInsertId(); $this->logEvent($newInstanceId, 'process_started', 'Process started', $start_node, ['context' => $context], $userId); $stmt = $this->pdo->prepare("SELECT * FROM process_instances WHERE id = ?"); $stmt->execute([$newInstanceId]); return $stmt->fetch(PDO::FETCH_ASSOC); } " content = content.replace( 'public function getOrCreateInstanceByDefId(int $personId, int $processDefinitionId, int $userId, array $context = []): ?array {', new_method + '\n public function getOrCreateInstanceByDefId(int $personId, int $processDefinitionId, int $userId, array $context = []): ?array {' ) with open('WorkflowEngine.php', 'w') as f: f.write(content) print("Patched.")