r3-v2/update_templates.php

304 lines
6.6 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
function getIndex($needle, $haystack) {
2021-06-19 11:27:32 +02:00
2021-06-21 21:21:54 +02:00
$index = 0;
2021-06-19 11:27:32 +02:00
2021-06-21 21:21:54 +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-21 21:21:54 +02:00
$index++;
}
2021-06-19 11:27:32 +02:00
2021-06-21 21:21:54 +02:00
return false;
2021-06-19 16:41:42 +02:00
}
2021-06-19 11:27:32 +02:00
2021-06-21 21:21:54 +02:00
function loadSavedData($file) {
2021-06-21 11:43:10 +02:00
2021-06-21 21:21:54 +02:00
$saveFile = $file . '.saved';
2021-06-19 11:27:32 +02:00
2021-06-21 21:21:54 +02:00
echo "restoring file " . $saveFile . "\n";
2021-06-19 11:27:32 +02:00
2021-06-21 21:21:54 +02:00
$sh = fopen($saveFile, "r");
2021-06-19 11:27:32 +02:00
2021-06-21 21:21:54 +02:00
$saved = [];
2021-06-20 08:13:06 +02:00
2021-06-21 21:21:54 +02:00
while (!feof($sh)) {
2021-06-20 08:13:06 +02:00
2021-06-21 21:21:54 +02:00
$line = fgets($sh);
2021-06-20 08:13:06 +02:00
2021-06-21 21:21:54 +02:00
array_push($saved, $line);
2021-06-20 08:13:06 +02:00
2021-06-21 21:21:54 +02:00
}
2021-06-20 08:13:06 +02:00
2021-06-21 21:21:54 +02:00
fclose($sh);
2021-06-19 11:27:32 +02:00
2021-06-21 21:21:54 +02:00
return $saved;
}
2021-06-20 08:13:06 +02:00
2021-06-21 21:21:54 +02:00
function restore($file, $tokens, $saved) {
2021-06-20 08:13:06 +02:00
2021-06-21 21:21:54 +02:00
$fh = fopen($file, "r");
2021-06-20 08:13:06 +02:00
2021-06-21 21:21:54 +02:00
$merged = [];
2021-06-19 11:27:32 +02:00
2021-06-21 21:21:54 +02:00
$restoreTokenPair = [];
2021-06-19 11:27:32 +02:00
2021-06-21 21:21:54 +02:00
$startRestoring = false;
2021-06-20 08:13:06 +02:00
2021-06-21 21:21:54 +02:00
$saveLine = true;
2021-06-20 08:13:06 +02:00
2021-06-21 21:21:54 +02:00
while (!feof($fh)) {
2021-06-21 11:43:10 +02:00
2021-06-21 21:21:54 +02:00
$line = fgets($fh);
2021-06-19 11:27:32 +02:00
2021-06-21 21:21:54 +02:00
foreach ($tokens as $startToken => $endToken) {
if (preg_match('/\b' . $startToken . '\b/', $line)) {
$startRestoring = true;
$restoreTokenPair = [$startToken, $endToken];
break;
}
}
2021-06-19 11:27:32 +02:00
2021-06-21 21:21:54 +02:00
foreach ($tokens as $startToken => $endToken) {
if (preg_match('/\b' . $endToken . '\b/', $line)) {
$startRestoring = false;
$saveLine = false;
break;
}
}
2021-06-20 08:13:06 +02:00
2021-06-21 21:21:54 +02:00
if ($startRestoring) {
2021-06-19 11:27:32 +02:00
2021-06-21 21:21:54 +02:00
$startToken = $restoreTokenPair[0];
$endToken = $restoreTokenPair[1];
2021-06-20 08:13:06 +02:00
2021-06-21 21:21:54 +02:00
$startIndex = getIndex($startToken, $saved);
$endIndex = getIndex($endToken, $saved);
2021-06-19 11:27:32 +02:00
2021-06-21 21:21:54 +02:00
if ($startIndex === false || $endIndex === false) {
echo "Could not find data to be restored : startIndex = " . $startIndex . ", endIndex = " . $endIndex;
echo "Restore token pair = ";
print_r($restoreTokenPair);
$saveLine = true;
} else {
$restored = array_splice($saved, $startIndex, ($endIndex - $startIndex + 1));
$merged = array_merge($merged, $restored);
$saveLine = false;
}
}
2021-06-20 08:13:06 +02:00
2021-06-21 21:21:54 +02:00
if ($saveLine) {
array_push($merged, $line);
} else {
$saveLine = true;
}
}
2021-06-20 08:13:06 +02:00
2021-06-21 21:21:54 +02:00
fclose($fh);
2021-06-20 08:13:06 +02:00
2021-06-21 21:21:54 +02:00
file_put_contents($file, $merged);
}
2021-06-20 08:13:06 +02:00
2021-06-21 21:21:54 +02:00
function install_method_templates($file)
{
$fn = fopen($file, "r");
2021-06-20 08:13:06 +02:00
2021-06-21 21:21:54 +02:00
$saveLine = true;
2021-06-20 08:13:06 +02:00
2021-06-21 21:21:54 +02:00
$methods = [];
2021-06-20 08:13:06 +02:00
2021-06-21 21:21:54 +02:00
$saveMethod = false;
2021-06-20 08:13:06 +02:00
2021-06-21 21:21:54 +02:00
$lines = [];
2021-06-20 08:13:06 +02:00
2021-06-21 21:21:54 +02:00
while (!feof($fn)) {
$line = fgets($fn);
/**
* if exiting an options definition block - stop saving that definition
*/
if (preg_match('/\bSTATIC_METHODS_END\b/', $line)) {
$saveMethod = false;
}
/**
* if exiting an method mplementation block - save the line for later
*/
if (preg_match('/\bSTATIC_METHODS_DEFINITION_END\b/', $line)) {
$saveLine = true;
}
/**
* Now save the line
*/
if ($saveLine) {
array_push($lines, $line);
}
/**
* if entering an options implementation block - do not save the line for later
*/
if (preg_match('/\bSTATIC_METHODS_DEFINITION_START\b/', $line)) {
$saveLine = false;
}
if ($saveMethod) {
2021-06-21 10:51:57 +02:00
2021-06-21 21:21:54 +02:00
$line = trim($line);
array_push($methods, $line);
}
/**
* If entering an options definition block - start saving that definition
*/
if (preg_match('/\bSTATIC_METHODS_START\b/', $line)) {
$saveMethod = true;
}
}
fclose($fn);
file_put_contents($file, $lines);
/**
* Process the methods
*/
if (sizeof($methods) > 0) {
$template = file_get_contents('src/templates/static_method.template');
$newOptions = '';
foreach ($methods as $method) {
$updated = str_replace('METHOD_NAME_UPPERCASE', strtoupper($method), $template);
$updated = str_replace('METHOD_NAME', $method, $updated);
$newOptions .= $updated;
}
$contents = file_get_contents($file);
$contents = preg_replace(
'/\/\/\bSTATIC_METHODS_DEFINITION_START\b.*?\/\/\bSTATIC_METHODS_DEFINITION_END\b/s',
"//STATIC_METHODS_DEFINITION_START\n" . $newOptions . "\t\t//STATIC_METHODS_DEFINITION_END",
$contents
);
file_put_contents($file, $contents);
}
}
global $files;
foreach ($files as $file) {
$tokens = [
'OPTIONS_START' => 'OPTIONS_END',
'INSTANCE_OPTIONS_MAPPING_START' => 'INSTANCE_OPTIONS_MAPPING_END',
'LINKED_OBJECTS_START' => 'LINKED_OBJECTS_END',
'STATIC_METHODS_START' => 'STATIC_METHODS_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',
'CUSTOM_START_METHOD_START' => 'CUSTOM_START_METHOD_END',
'CUSTOM_STOP_METHOD_START' => 'CUSTOM_STOP_METHOD_END'
];
if ($argv[2] == 'save') {
echo "saving file " . $file . "\n";
$saved = [];
$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-21 21:21:54 +02:00
}
2021-06-20 08:13:06 +02:00
2021-06-21 21:21:54 +02:00
if ($startSaving) {
array_push($saved, $line);
}
2021-06-20 08:13:06 +02:00
2021-06-21 21:21:54 +02:00
foreach ($tokens as $startToken => $endToken) {
if (preg_match('/\b' . $endToken . '\b/', $line)) {
$startSaving = false;
break;
}
}
2021-06-20 08:13:06 +02:00
2021-06-19 11:27:32 +02:00
}
2021-06-21 21:21:54 +02:00
fclose($fn);
file_put_contents($file . '.saved', $saved);
echo "saved file " . $file . ".saved\n";
} else if ($argv[2] == 'restore') {
$saved = loadSavedData($file);
restore($file, $tokens, $saved);
/**
* At this point - we restored the headers but the method definitions are not installed
* We can't restore them until we have expanded what those definitions are
* i.e. -
* 1. we need to update_methods.php (to define the placeholders)
* 2. then we meed to restore those methods from saved[]
*/
install_method_templates($file);
$methodTokens = [
"CUSTOM_START_METHOD_START" => "CUSTOM_START_METHOD_END",
"CUSTOM_STOP_METHOD_START" => "CUSTOM_STOP_METHOD_END"
];
restore($file, $methodTokens, $saved);
echo "Restored file " . $file;
// unlink($saveFile);
} else if ($argv[2] == 'install-methods') {
install_method_templates($file);
echo "Installed method templates for file " . $file;
// unlink($saveFile);
}
2021-06-19 11:27:32 +02:00
}
exit(0);
?>