r3-v2/update_templates.php

176 lines
4.4 KiB
PHP
Raw Normal View History

2021-06-19 11:27:32 +02:00
#!/usr/bin/php
<?php
2021-06-21 11:43:10 +02:00
include "utils.php";
2021-06-19 11:27:32 +02:00
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',
2021-06-21 10:51:57 +02:00
'STATIC_METHODS_START' => 'STATIC_METHODS_END',
2021-06-20 08:13:06 +02:00
'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',
2021-06-21 10:51:57 +02:00
'CUSTOM_OUT_OF_CLASS_IMPLEMENTATION_START' => 'CUSTOM_OUT_OF_CLASS_IMPLEMENTATION_END',
'CUSTOM_[A..Z_]+_METHOD_START' => 'CUSTOM_[A..Z_]+_METHOD_END'
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-21 11:43:10 +02:00
global $files;
2021-06-19 16:41:42 +02:00
foreach ($files as $file) {
2021-06-19 11:27:32 +02:00
2021-06-21 11:43:10 +02:00
if ($argv[2] == '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);
2021-06-21 11:43:10 +02:00
2021-06-19 16:41:42 +02:00
echo "saved file " . $file . ".saved\n";
2021-06-19 11:27:32 +02:00
2021-06-21 11:43:10 +02:00
} else if ($argv[2] == '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;
2021-06-21 10:51:57 +02:00
$saveLine = true;
2021-06-20 08:13:06 +02:00
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;
2021-06-21 10:51:57 +02:00
$saveLine = false;
2021-06-20 08:13:06 +02:00
break;
}
2021-06-21 10:51:57 +02:00
}
2021-06-20 08:13:06 +02:00
if ($startRestoring) {
2021-06-21 10:51:57 +02:00
2021-06-20 08:13:06 +02:00
$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);
2021-06-21 10:51:57 +02:00
$saveLine = true;
2021-06-20 08:13:06 +02:00
} else {
2021-06-21 10:51:57 +02:00
$restored = array_splice($saved, $startIndex, ($endIndex - $startIndex + 1));
2021-06-20 08:13:06 +02:00
$merged = array_merge($merged, $restored);
2021-06-21 10:51:57 +02:00
$saveLine = false;
2021-06-20 08:13:06 +02:00
}
2021-06-21 10:51:57 +02:00
}
if ($saveLine) {
2021-06-20 08:13:06 +02:00
array_push($merged, $line);
2021-06-21 10:51:57 +02:00
} else {
$saveLine = true;
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-21 10:51:57 +02:00
// unlink($saveFile);
2021-06-19 11:27:32 +02:00
}
}
exit(0);
?>