r3-v2/update_templates.php

171 lines
4.3 KiB
PHP
Raw Normal View History

2021-06-19 11:27:32 +02:00
#!/usr/bin/php
<?php
if (isset($argc)) {
for ($i = 0; $i < $argc; $i++) {
echo "Argument #" . $i . " - " . $argv[$i] . "\n";
}
}
else {
echo "argc and argv disabled\n";
}
2021-06-20 08:13:06 +02:00
$tokens = [
'OPTIONS_START' => 'OPTIONS_END',
'INSTANCE_OPTIONS_MAPPING_START' => 'INSTANCE_OPTIONS_MAPPING_END',
'LINKED_OBJECTS_START' => 'LINKED_OBJECTS_END',
'EXCLUDED_FROM_INSTANCE_OPTIONS_START' => 'EXCLUDED_FROM_INSTANCE_OPTIONS_END',
'CUSTOM_OPTIONS_INIT_START' => 'CUSTOM_OPTIONS_INIT_END',
'CUSTOM_BEFORE_INIT_START' => 'CUSTOM_BEFORE_INIT_END',
'CUSTOM_CREATE_INSTANCE_START' => 'CUSTOM_CREATE_INSTANCE_END',
'CUSTOM_UPDATE_INSTANCE_START' => 'CUSTOM_UPDATE_INSTANCE_END',
'CUSTOM_UPDATE_FROM_INSTANCE_START' => 'CUSTOM_UPDATE_FROM_INSTANCE_END',
'CUSTOM_DISPOSE_START' => 'CUSTOM_DISPOSE_END',
'CUSTOM_DISPOSE_INSTANCE_START' => 'CUSTOM_DISPOSE_INSTANCE_END',
'CUSTOM_IMPLEMENTATION_START' => 'CUSTOM_IMPLEMENTATION_END',
'CUSTOM_OUT_OF_CLASS_IMPLEMENTATION_START' => 'CUSTOM_OUT_OF_CLASS_IMPLEMENTATION_END'
2021-06-19 16:41:42 +02:00
];
2021-06-20 08:13:06 +02:00
$files = [$argv[2]];
2021-06-19 16:41:42 +02:00
2021-06-20 08:13:06 +02:00
function getIndex($needle, $haystack) {
2021-06-19 11:27:32 +02:00
2021-06-20 08:13:06 +02:00
$index = 0;
2021-06-19 11:27:32 +02:00
2021-06-20 08:13:06 +02:00
foreach ($haystack as $value) {
if (preg_match('/\b' . $needle . '\b/', $value)) {
return $index;
2021-06-19 11:27:32 +02:00
}
2021-06-20 08:13:06 +02:00
$index++;
2021-06-19 11:27:32 +02:00
}
2021-06-20 08:13:06 +02:00
return false;
2021-06-19 16:41:42 +02:00
}
2021-06-19 11:27:32 +02:00
2021-06-19 16:41:42 +02:00
foreach ($files as $file) {
2021-06-19 11:27:32 +02:00
2021-06-19 16:41:42 +02:00
if ($argv[1] == 'save') {
2021-06-19 11:27:32 +02:00
2021-06-19 16:41:42 +02:00
echo "saving file " . $file . "\n";
2021-06-19 11:27:32 +02:00
2021-06-19 16:41:42 +02:00
$saved = [];
2021-06-20 08:13:06 +02:00
$startSaving = false;
$fn = fopen($file, "r");
while (!feof($fn)) {
$line = fgets($fn);
foreach ($tokens as $startToken => $endToken) {
if (preg_match('/\b' . $startToken . '\b/', $line)) {
$startSaving = true;
break;
}
2021-06-19 11:27:32 +02:00
2021-06-20 08:13:06 +02:00
}
if ($startSaving) {
array_push($saved, $line);
}
foreach ($tokens as $startToken => $endToken) {
if (preg_match('/\b' . $endToken . '\b/', $line)) {
$startSaving = false;
break;
}
2021-06-19 11:27:32 +02:00
2021-06-19 16:41:42 +02:00
}
2021-06-19 11:27:32 +02:00
}
2021-06-20 08:13:06 +02:00
fclose($fn);
2021-06-19 16:41:42 +02:00
file_put_contents($file . '.saved', $saved);
echo "saved file " . $file . ".saved\n";
2021-06-19 11:27:32 +02:00
2021-06-19 16:41:42 +02:00
} else if ($argv[1] == 'restore') {
2021-06-19 11:27:32 +02:00
2021-06-20 08:13:06 +02:00
$saveFile = $file . '.saved';
echo "restoring file " . $saveFile . "\n";
2021-06-19 11:27:32 +02:00
2021-06-20 08:13:06 +02:00
$sh = fopen($saveFile, "r");
$saved = [];
2021-06-19 11:27:32 +02:00
2021-06-20 08:13:06 +02:00
while (!feof($sh)) {
$line = fgets($sh);
array_push($saved, $line);
}
fclose($sh);
$fh = fopen($file, "r");
$merged = [];
$restoreTokenPair = [];
$startRestoring = false;
while (!feof($fh)) {
$line = fgets($fh);
foreach ($tokens as $startToken => $endToken) {
if (preg_match('/\b' . $startToken . '\b/', $line)) {
$startRestoring = true;
$restoreTokenPair = [$startToken, $endToken];
break;
}
}
foreach ($tokens as $startToken => $endToken) {
if (preg_match('/\b' . $endToken . '\b/', $line)) {
$startRestoring = false;
break;
}
}
if ($startRestoring) {
$startToken = $restoreTokenPair[0];
$endToken = $restoreTokenPair[1];
$startIndex = getIndex($startToken, $saved);
$endIndex = getIndex($endToken, $saved);
if ($startIndex === false || $endIndex === false) {
echo "Could not find data to be restored : startIndex = " . $startIndex . ", endIndex = " . $endIndex;
echo "Restore token pair = ";
print_r($restoreTokenPair);
array_push($merged, $line);
} else {
$restored = array_splice($saved, $startIndex, $endIndex - $startIndex);
$merged = array_merge($merged, $restored);
}
} else {
array_push($merged, $line);
2021-06-19 11:27:32 +02:00
}
}
2021-06-20 08:13:06 +02:00
fclose($fh);
file_put_contents($file, $merged);
echo "Restored file " . $file;
2021-06-20 20:46:13 +02:00
unlink($saveFile);
2021-06-19 11:27:32 +02:00
}
}
exit(0);
?>