Skip to content

Commit 58e9f8b

Browse files
Added a method to replace copied variables with new ones in the script
1 parent 31948e0 commit 58e9f8b

1 file changed

Lines changed: 180 additions & 4 deletions

File tree

ProcessMaker/ImportExport/Exporters/ScriptExporter.php

Lines changed: 180 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace ProcessMaker\ImportExport\Exporters;
44

5+
use Illuminate\Support\Facades\DB;
6+
use Illuminate\Support\Facades\Log;
57
use ProcessMaker\ImportExport\DependentType;
68
use ProcessMaker\Models\EnvironmentVariable;
79
use ProcessMaker\Models\ScriptCategory;
@@ -46,16 +48,19 @@ public function import() : bool
4648
$this->model->script_executor_id = $executor->id;
4749
}
4850

51+
// Update environment variable references in script code
52+
$this->updateEnvironmentVariableReferences();
53+
4954
// Pre-save cleanup for data source scripts to prevent constraint violations
5055
if ($this->mode === 'update' && $this->model->exists) {
5156
// Check if this script has any data source relationships that might cause conflicts
52-
$hasDataSourceRelationships = \DB::table('data_source_scripts')
57+
$hasDataSourceRelationships = DB::table('data_source_scripts')
5358
->where('script_id', $this->model->id)
5459
->exists();
5560

5661
if ($hasDataSourceRelationships) {
5762
// Clean up any conflicting records in data_source_scripts
58-
\DB::table('data_source_scripts')
63+
DB::table('data_source_scripts')
5964
->where('script_id', $this->model->id)
6065
->where('id', '!=', $this->model->id)
6166
->delete();
@@ -72,11 +77,182 @@ private function getEnvironmentVariables() : array
7277

7378
// Search for environment variable present in the code
7479
foreach ($environmentVariables as $variable) {
75-
if (preg_match('/[^a-zA-Z0-9\s]' . $variable->name . '[^a-zA-Z0-9\s]?/', $this->model->code)) {
76-
$environmentVariablesFound[] = $variable;
80+
// Multiple patterns to catch different usage styles
81+
$patterns = [
82+
// JavaScript patterns
83+
'/\bprocess\.env\.' . preg_quote($variable->name, '/') . '\b/', // process.env.VAR
84+
'/\bprocess\.env\[\s*[\'"]' . preg_quote($variable->name, '/') . '[\'"]\s*\]/', // process.env['VAR']
85+
86+
// PHP patterns
87+
'/\bconfig\s*\(\s*[\'"]' . preg_quote($variable->name, '/') . '[\'"]\s*\)/', // config('VAR')
88+
'/\benv\s*\(\s*[\'"]' . preg_quote($variable->name, '/') . '[\'"]\s*\)/', // env('VAR')
89+
'/\$_ENV\s*\[\s*[\'"]' . preg_quote($variable->name, '/') . '[\'"]\s*\]/', // $_ENV['VAR']
90+
'/\$_SERVER\s*\[\s*[\'"]' . preg_quote($variable->name, '/') . '[\'"]\s*\]/', // $_SERVER['VAR']
91+
92+
// Java patterns
93+
'/\bSystem\.getenv\s*\(\s*[\'"]' . preg_quote($variable->name, '/') . '[\'"]\s*\)/', // System.getenv("VAR")
94+
'/\bSystem\.getProperty\s*\(\s*[\'"]' . preg_quote($variable->name, '/') . '[\'"]\s*\)/', // System.getProperty("VAR")
95+
96+
// C# patterns
97+
'/\bEnvironment\.GetEnvironmentVariable\s*\(\s*[\'"]' . preg_quote($variable->name, '/') . '[\'"]\s*\)/', // Environment.GetEnvironmentVariable("VAR")
98+
'/\bConfigurationManager\.AppSettings\s*\[\s*[\'"]' . preg_quote($variable->name, '/') . '[\'"]\s*\]/', // ConfigurationManager.AppSettings["VAR"]
99+
100+
// Python patterns
101+
'/\bos\.environ\s*\[\s*[\'"]' . preg_quote($variable->name, '/') . '[\'"]\s*\]/', // os.environ['VAR']
102+
'/\bos\.environ\.get\s*\(\s*[\'"]' . preg_quote($variable->name, '/') . '[\'"]\s*\)/', // os.environ.get('VAR')
103+
'/\bos\.getenv\s*\(\s*[\'"]' . preg_quote($variable->name, '/') . '[\'"]\s*\)/', // os.getenv('VAR')
104+
];
105+
106+
foreach ($patterns as $pattern) {
107+
if (preg_match($pattern, $this->model->code)) {
108+
$environmentVariablesFound[] = $variable;
109+
break;
110+
}
77111
}
78112
}
79113

80114
return $environmentVariablesFound;
81115
}
116+
117+
/**
118+
* Update environment variable references in script code when variables are renamed during import
119+
*
120+
* Note: This is a basic implementation. For a complete solution, you would need to:
121+
* 1. Track original variable names during export phase
122+
* 2. Map original names to new names after duplicate handling
123+
* 3. Update all references in the script code
124+
*/
125+
private function updateEnvironmentVariableReferences(): void
126+
{
127+
// Only process in copy mode where variables might be renamed
128+
if ($this->mode !== 'copy') {
129+
return;
130+
}
131+
132+
// Get the original variables that were detected during export
133+
$originalVariables = $this->getEnvironmentVariables();
134+
$variableMapping = [];
135+
136+
// Compare with the imported variables to detect name changes
137+
foreach ($this->getDependents(DependentType::ENVIRONMENT_VARIABLES, true) as $dependent) {
138+
$importedVar = $dependent->model;
139+
140+
// Find matching original variable by comparing the base name
141+
foreach ($originalVariables as $originalVar) {
142+
$mapped = false;
143+
144+
// Check if this is a renamed version (has suffix like _1, _2, etc.)
145+
if (preg_match('/^' . preg_quote($originalVar->name) . '_\d+$/', $importedVar->name)) {
146+
$variableMapping[$originalVar->name] = $importedVar->name;
147+
$mapped = true;
148+
}
149+
// Check if it's exactly the same name (no rename needed)
150+
elseif ($originalVar->name === $importedVar->name) {
151+
// Don't add to mapping since no replacement is needed
152+
$mapped = true;
153+
}
154+
// Check if it has other suffixes like _copy, _duplicate, etc.
155+
elseif (preg_match('/^' . preg_quote($originalVar->name) . '_(copy|duplicate|\d+)$/i', $importedVar->name)) {
156+
$variableMapping[$originalVar->name] = $importedVar->name;
157+
$mapped = true;
158+
}
159+
160+
if ($mapped) {
161+
break;
162+
}
163+
}
164+
}
165+
166+
// Update script code with new variable names
167+
if (!empty($variableMapping)) {
168+
$updatedCode = $this->model->code;
169+
170+
foreach ($variableMapping as $oldName => $newName) {
171+
// Update common patterns for environment variable access
172+
$patterns = [
173+
// JavaScript patterns - process.env.VARIABLE_NAME → process.env.VARIABLE_NAME_1
174+
'/\bprocess\.env\.' . preg_quote($oldName, '/') . '\b/',
175+
'/\bprocess\.env\[\s*[\'"]' . preg_quote($oldName, '/') . '[\'"]\s*\]/',
176+
'/\bprocess\.env\[\s*"' . preg_quote($oldName, '/') . '"\s*\]/', // Double quotes
177+
178+
// PHP patterns
179+
// config('VARIABLE_NAME') → config('VARIABLE_NAME_1')
180+
'/\bconfig\s*\(\s*[\'"]' . preg_quote($oldName, '/') . '[\'"]\s*\)/',
181+
// env('VARIABLE_NAME') → env('VARIABLE_NAME_1')
182+
'/\benv\s*\(\s*[\'"]' . preg_quote($oldName, '/') . '[\'"]\s*\)/',
183+
// $_ENV['VARIABLE_NAME'] → $_ENV['VARIABLE_NAME_1']
184+
'/\$_ENV\s*\[\s*[\'"]' . preg_quote($oldName, '/') . '[\'"]\s*\]/',
185+
// $_SERVER['VARIABLE_NAME'] → $_SERVER['VARIABLE_NAME_1']
186+
'/\$_SERVER\s*\[\s*[\'"]' . preg_quote($oldName, '/') . '[\'"]\s*\]/',
187+
// getenv('VARIABLE_NAME') → getenv('VARIABLE_NAME_1')
188+
'/\bgetenv\s*\(\s*[\'"]' . preg_quote($oldName, '/') . '[\'"]\s*\)/',
189+
// Config::get('VARIABLE_NAME') → Config::get('VARIABLE_NAME_1')
190+
'/\bConfig\s*::\s*get\s*\(\s*[\'"]' . preg_quote($oldName, '/') . '[\'"]\s*\)/',
191+
192+
// Java patterns
193+
// System.getenv("VARIABLE_NAME") → System.getenv("VARIABLE_NAME_1")
194+
'/\bSystem\.getenv\s*\(\s*[\'"]' . preg_quote($oldName, '/') . '[\'"]\s*\)/',
195+
// System.getProperty("VARIABLE_NAME") → System.getProperty("VARIABLE_NAME_1")
196+
'/\bSystem\.getProperty\s*\(\s*[\'"]' . preg_quote($oldName, '/') . '[\'"]\s*\)/',
197+
198+
// C# patterns
199+
// Environment.GetEnvironmentVariable("VARIABLE_NAME") → Environment.GetEnvironmentVariable("VARIABLE_NAME_1")
200+
'/\bEnvironment\.GetEnvironmentVariable\s*\(\s*[\'"]' . preg_quote($oldName, '/') . '[\'"]\s*\)/',
201+
// ConfigurationManager.AppSettings["VARIABLE_NAME"] → ConfigurationManager.AppSettings["VARIABLE_NAME_1"]
202+
'/\bConfigurationManager\.AppSettings\s*\[\s*[\'"]' . preg_quote($oldName, '/') . '[\'"]\s*\]/',
203+
204+
// Python patterns
205+
// os.environ['VARIABLE_NAME'] → os.environ['VARIABLE_NAME_1']
206+
'/\bos\.environ\s*\[\s*[\'"]' . preg_quote($oldName, '/') . '[\'"]\s*\]/',
207+
// os.environ.get('VARIABLE_NAME') → os.environ.get('VARIABLE_NAME_1')
208+
'/\bos\.environ\.get\s*\(\s*[\'"]' . preg_quote($oldName, '/') . '[\'"]\s*\)/',
209+
// os.getenv('VARIABLE_NAME') → os.getenv('VARIABLE_NAME_1')
210+
'/\bos\.getenv\s*\(\s*[\'"]' . preg_quote($oldName, '/') . '[\'"]\s*\)/',
211+
];
212+
213+
foreach ($patterns as $patternIndex => $pattern) {
214+
$updatedCode = preg_replace_callback($pattern, function ($matches) use ($oldName, $newName, $patternIndex) {
215+
$original = $matches[0];
216+
217+
// Handle different replacement patterns
218+
switch ($patternIndex) {
219+
case 0: // process.env.VARIABLE_NAME
220+
return str_replace('process.env.' . $oldName, 'process.env.' . $newName, $original);
221+
case 1: // process.env['VARIABLE_NAME']
222+
case 2: // process.env["VARIABLE_NAME"]
223+
return str_replace($oldName, $newName, $original);
224+
case 6: // System.getenv for Java
225+
return str_replace($oldName, $newName, $original);
226+
case 7: // System.getProperty for Java
227+
return str_replace($oldName, $newName, $original);
228+
case 8: // Environment.GetEnvironmentVariable for C#
229+
return str_replace($oldName, $newName, $original);
230+
case 9: // ConfigurationManager.AppSettings for C#
231+
return str_replace($oldName, $newName, $original);
232+
case 10: // os.environ for Python
233+
case 11: // os.environ.get for Python
234+
case 12: // os.getenv for Python
235+
return str_replace($oldName, $newName, $original);
236+
default: // All other patterns (PHP)
237+
return str_replace($oldName, $newName, $original);
238+
}
239+
}, $updatedCode);
240+
}
241+
}
242+
243+
// Only update if changes were made
244+
if ($updatedCode !== $this->model->code) {
245+
$this->model->code = $updatedCode;
246+
247+
// Log the variable name changes (summary)
248+
Log::info('ScriptExporter: Updated variable references in script', [
249+
'script_id' => $this->model->id,
250+
'script_title' => $this->model->title,
251+
'variables_updated' => count($variableMapping),
252+
'mappings' => $variableMapping,
253+
'mode' => $this->mode,
254+
]);
255+
}
256+
}
257+
}
82258
}

0 commit comments

Comments
 (0)