-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sync_method_directly.php
More file actions
125 lines (97 loc) · 4.73 KB
/
test_sync_method_directly.php
File metadata and controls
125 lines (97 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
require_once 'vendor/autoload.php';
// Create test files
file_put_contents('.env', 'VITE_APP_NAME="${APP_NAME}"' . "\n");
file_put_contents('.env.test', 'VITE_APP_NAME="${OLD_NAME}"' . "\n");
echo "=== Direct Method Test ===\n\n";
echo "📄 Before sync:\n";
echo ".env: " . file_get_contents('.env');
echo ".env.test: " . file_get_contents('.env.test');
// Test the sync method directly without Laravel facades
// We'll create a simple version that doesn't use File facade
class SimpleEnvSync {
public function parseEnvFileWithStructure(string $filePath): array {
$content = file_get_contents($filePath);
$lines = explode("\n", $content);
$entries = [];
$structure = [];
foreach ($lines as $lineNumber => $line) {
$originalLine = $line;
$trimmedLine = trim($line);
$structure[$lineNumber] = [
'original' => $originalLine,
'type' => 'other'
];
if (empty($trimmedLine) || str_starts_with($trimmedLine, '#')) {
$structure[$lineNumber]['type'] = empty($trimmedLine) ? 'empty' : 'comment';
continue;
}
if (str_contains($trimmedLine, '=')) {
$parts = explode('=', $trimmedLine, 2);
$key = trim($parts[0]);
$value = isset($parts[1]) ? trim($parts[1]) : '';
$hasIgnore = str_contains($originalLine, '#ENVIGNORE');
if (str_contains($value, '#ENVIGNORE')) {
$value = trim(str_replace('#ENVIGNORE', '', $value));
}
if ((str_starts_with($value, '"') && str_ends_with($value, '"')) ||
(str_starts_with($value, "'") && str_ends_with($value, "'"))) {
$value = substr($value, 1, -1);
}
$structure[$lineNumber] = [
'original' => $originalLine,
'type' => 'env_var',
'key' => $key,
'value' => $value,
'ignored' => $hasIgnore
];
if (!$hasIgnore) {
$entries[$key] = $value;
}
}
}
return ['entries' => $entries, 'structure' => $structure];
}
public function syncValueToTarget(string $targetFile, string $key, string $newValue): void {
$targetData = $this->parseEnvFileWithStructure($targetFile);
$structure = $targetData['structure'];
// Get the original value from source to preserve quoting
$sourceData = $this->parseEnvFileWithStructure('.env');
$originalQuoted = false;
foreach ($sourceData['structure'] as $sourceLineData) {
if ($sourceLineData['type'] === 'env_var' && $sourceLineData['key'] === $key) {
$originalLine = $sourceLineData['original'];
if (preg_match('/=\s*"/', $originalLine) || preg_match("/=\s*'/", $originalLine)) {
$originalQuoted = true;
}
break;
}
}
echo "🔍 Quote detection for key '{$key}':\n";
echo " Original quoted: " . ($originalQuoted ? 'YES' : 'NO') . "\n";
// Find the line with this key and update it
foreach ($structure as $lineNumber => $lineData) {
if ($lineData['type'] === 'env_var' && $lineData['key'] === $key) {
$formattedValue = $newValue;
if ($originalQuoted || str_contains($formattedValue, ' ') || str_contains($formattedValue, '#') || str_contains($formattedValue, '"') || str_contains($formattedValue, '$')) {
$formattedValue = '"' . str_replace('"', '\"', $formattedValue) . '"';
}
echo " Formatted value: '{$formattedValue}'\n";
$structure[$lineNumber]['original'] = "{$key}={$formattedValue}";
break;
}
}
$content = '';
foreach ($structure as $lineData) {
$content .= $lineData['original'] . "\n";
}
$content = rtrim($content, "\n") . "\n";
file_put_contents($targetFile, $content);
}
}
$sync = new SimpleEnvSync();
$sync->syncValueToTarget('.env.test', 'VITE_APP_NAME', '${APP_NAME}');
echo "\n📄 After sync:\n";
echo ".env.test: " . file_get_contents('.env.test');
echo "\n✅ Expected: VITE_APP_NAME=\"\${APP_NAME}\"\n";
echo "✅ Result shows quotes are " . (str_contains(file_get_contents('.env.test'), 'VITE_APP_NAME="${APP_NAME}"') ? 'PRESERVED' : 'MISSING') . "\n";