function getSupportFunctionsDefinition() { // EXAMPLE of InputArgs_json // { // "simpleArg": "string", // "argWithDefault": { // "type": "int", // "default": 10 // }, // "argByReference": { // "type": "array", // "reference": true // }, // "optionalComplex": { // "type": "?string", // "default": null // } // } $supportFunctions = ""; foreach (sql("SELECT Name, InputArgs_json, PhpCode FROM SYS_PRD_BND.SupportFunctions WHERE PhpCode IS NOT NULL") as $f) { $inputArgs = json_decode($f["InputArgs_json"], true); $compiledArgs = []; if (is_array($inputArgs)) { foreach ($inputArgs as $name => $config) { $parts = [ 'type' => '', 'ref' => '', 'name' => '$' . $name, 'default' => '' ]; // Case A: Expanded Object Configuration if (is_array($config)) { // 1. Type Hinting (e.g., "string", "?int", "MyClass") if (!empty($config['type'])) { $parts['type'] = $config['type'] . ' '; } // 2. Pass by Reference if (!empty($config['reference']) && $config['reference'] === true) { $parts['ref'] = '&'; } // 3. Variadic Arguments (...$args) if (!empty($config['variadic']) && $config['variadic'] === true) { $parts['ref'] = '...'; } // 4. Default Values // We use array_key_exists so we can detect if the default is actually NULL if (array_key_exists('default', $config)) { // var_export converts the value to parsable PHP code (adds quotes to strings, preserves arrays) $parts['default'] = ' = ' . var_export($config['default'], true); } } // Case B: Simple Key-Value pair (Backward Compatibility / Simple Type) // Example: "endpoint": "string" elseif (is_string($config) && !empty($config)) { $parts['type'] = $config . ' '; } // Assemble definition: "string &$name = 'value'" $compiledArgs[] = "{$parts['type']}{$parts['ref']}{$parts['name']}{$parts['default']}"; } } $argsString = implode(", ", $compiledArgs); $supportFunctions .= "function {$f['Name']}($argsString) {\n{$f['PhpCode']}\n}\n"; } return $supportFunctions; }