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-28 10:07:15 +02:00
|
|
|
function from_camel_case($input) {
|
|
|
|
preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
|
|
|
|
$ret = $matches[0];
|
|
|
|
foreach ($ret as &$match) {
|
|
|
|
$match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
|
|
|
|
}
|
|
|
|
return implode('_', $ret);
|
|
|
|
}
|
|
|
|
|
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-28 09:05:07 +02:00
|
|
|
while ($saved[sizeof($saved) - 1] === false && sizeof($saved) > 0) {
|
|
|
|
array_pop($saved);
|
|
|
|
}
|
|
|
|
|
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-28 09:05:07 +02:00
|
|
|
$data = [];
|
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-28 09:05:07 +02:00
|
|
|
$restoreDepth = 0;
|
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) {
|
2021-06-28 09:05:07 +02:00
|
|
|
|
2021-06-21 21:21:54 +02:00
|
|
|
if (preg_match('/\b' . $startToken . '\b/', $line)) {
|
2021-06-28 09:05:07 +02:00
|
|
|
$restoreDepth++;
|
|
|
|
|
|
|
|
if ($restoreDepth === 1) {
|
|
|
|
$restoreTokenPair[0] = $startToken;
|
|
|
|
$restoreTokenPair[1] = $endToken;
|
|
|
|
}
|
|
|
|
|
2021-06-21 21:21:54 +02:00
|
|
|
break;
|
|
|
|
}
|
2021-06-19 11:27:32 +02:00
|
|
|
|
2021-06-21 21:21:54 +02:00
|
|
|
if (preg_match('/\b' . $endToken . '\b/', $line)) {
|
2021-06-28 09:05:07 +02:00
|
|
|
$restoreDepth--;
|
2021-06-21 21:21:54 +02:00
|
|
|
break;
|
|
|
|
}
|
2021-06-28 09:05:07 +02:00
|
|
|
|
2021-06-21 21:21:54 +02:00
|
|
|
}
|
2021-06-20 08:13:06 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
if ($restoreDepth === 1) {
|
2021-06-19 11:27:32 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
$startIndex = getIndex($restoreTokenPair[0], $saved);
|
|
|
|
$endIndex = getIndex($restoreTokenPair[1], $saved);
|
2021-06-20 08:13:06 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
for ($i = $startIndex; $i <= $endIndex; $i++) {
|
|
|
|
array_push($data, $saved[$i]);
|
|
|
|
}
|
2021-06-19 11:27:32 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
/**
|
|
|
|
* Seek to the end token in the file
|
|
|
|
*/
|
|
|
|
while (!feof($fh) && !(preg_match('/\b' . $restoreTokenPair[1] . '\b/', $line))){
|
|
|
|
$line = fgets($fh);
|
2021-06-21 21:21:54 +02:00
|
|
|
}
|
2021-06-20 08:13:06 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
$restoreDepth--;
|
|
|
|
|
2021-06-21 21:21:54 +02:00
|
|
|
} else {
|
2021-06-28 09:05:07 +02:00
|
|
|
array_push($data, $line);
|
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($fh);
|
2021-06-20 08:13:06 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
file_put_contents($file, $data);
|
2021-06-21 21:21:54 +02:00
|
|
|
}
|
2021-06-20 08:13:06 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
//function install_method_templates($file)
|
|
|
|
//{
|
|
|
|
// $fn = fopen($file, "r");
|
|
|
|
//
|
|
|
|
// $saveLine = true;
|
|
|
|
//
|
|
|
|
// $methods = [];
|
|
|
|
//
|
|
|
|
// $saveMethod = false;
|
|
|
|
//
|
|
|
|
// $lines = [];
|
|
|
|
//
|
|
|
|
//// $discoveredMethodTokens = [];
|
|
|
|
//
|
|
|
|
// 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;
|
|
|
|
// }
|
|
|
|
////
|
|
|
|
//// $matches = [];
|
|
|
|
//// if (preg_match('/\bCUSTOM_.*?_METHOD_START\b/', $line, $matches)) {
|
|
|
|
//// $discoveredMethodTokens[$matches[0]] = preg_replace('/_START$/','_END',$matches[0]);
|
|
|
|
//// }
|
|
|
|
//
|
|
|
|
// /**
|
|
|
|
// * if exiting an method implementation 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) {
|
|
|
|
//
|
|
|
|
// $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);
|
|
|
|
//
|
|
|
|
|
|
|
|
function getTokens($type)
|
2021-06-21 21:21:54 +02:00
|
|
|
{
|
2021-06-28 09:05:07 +02:00
|
|
|
$fn = fopen('src/templates/token.db', "r");
|
|
|
|
|
|
|
|
$tokens = [];
|
|
|
|
|
|
|
|
while (!feof($fn)) {
|
|
|
|
|
|
|
|
$line = fgets($fn);
|
|
|
|
|
|
|
|
$matches = [];
|
|
|
|
if (preg_match('/^(' . $type . '.*$)/', $line, $matches)) {
|
|
|
|
$tokens[$matches[1]] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose($fn);
|
|
|
|
|
|
|
|
return $tokens;
|
|
|
|
}
|
|
|
|
|
|
|
|
//function get_method_tokens($file)
|
|
|
|
//{
|
|
|
|
// $fn = fopen($file, "r");
|
|
|
|
//
|
|
|
|
// $discoveredMethodTokens = [];
|
|
|
|
//
|
|
|
|
// while (!feof($fn)) {
|
|
|
|
//
|
|
|
|
// $line = fgets($fn);
|
|
|
|
//
|
|
|
|
// $matches = [];
|
|
|
|
// if (preg_match('/\bCUSTOM_.*?_METHOD_START\b/', $line, $matches)) {
|
|
|
|
// $discoveredMethodTokens[$matches[0]] = preg_replace('/_START$/','_END',$matches[0]);
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// fclose($fn);
|
|
|
|
//
|
|
|
|
// return $discoveredMethodTokens;
|
|
|
|
//
|
|
|
|
//}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws ErrorException
|
|
|
|
*/
|
|
|
|
function loadSaved($file, $tokens) {
|
|
|
|
|
|
|
|
$saveFile = $file . '.saved';
|
|
|
|
|
|
|
|
echo "loading file " . $saveFile . "\n";
|
2021-06-20 08:13:06 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
$loadedTokens = [];
|
2021-06-20 08:13:06 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
$fn = fopen($saveFile, "r");
|
2021-06-20 08:13:06 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
if (!$fn) {
|
|
|
|
throw new ErrorException('Could not open save file: ' . $saveFile . "\n");
|
|
|
|
}
|
2021-06-20 08:13:06 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
$currentTokenKey = null;
|
2021-06-20 08:13:06 +02:00
|
|
|
|
2021-06-21 21:21:54 +02:00
|
|
|
while (!feof($fn)) {
|
|
|
|
|
|
|
|
$line = fgets($fn);
|
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
$tokenFound = false;
|
|
|
|
|
|
|
|
foreach ($tokens as $key => $store) {
|
|
|
|
if (preg_match('/\b' . $key . '\b/', $line))
|
|
|
|
{
|
|
|
|
if (array_key_exists($key, $loadedTokens)) {
|
|
|
|
throw new ErrorException("TOKEN ALREADY EXISTS! : $key\n");
|
|
|
|
}
|
|
|
|
$tokenFound = true;
|
|
|
|
$loadedTokens[$key] = [];
|
|
|
|
$currentTokenKey = $key;
|
|
|
|
break;
|
|
|
|
}
|
2021-06-21 21:21:54 +02:00
|
|
|
}
|
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
if (!$tokenFound && $line != false) {
|
|
|
|
array_push($loadedTokens[$currentTokenKey], $line);
|
2021-06-21 21:21:54 +02:00
|
|
|
}
|
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose($fn);
|
|
|
|
|
|
|
|
return $loadedTokens;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function save($file, $tokens) {
|
|
|
|
|
|
|
|
echo "saving file " . $file . "\n";
|
|
|
|
|
|
|
|
$currentTokens = [];
|
|
|
|
|
|
|
|
$fn = fopen($file, "r");
|
|
|
|
|
|
|
|
while (!feof($fn)) {
|
|
|
|
|
|
|
|
$line = fgets($fn);
|
|
|
|
|
|
|
|
foreach ($tokens as $key => $store) {
|
|
|
|
if (preg_match('/\b' . $key . '_END\b/', $line)) {
|
|
|
|
array_pop($currentTokens);
|
|
|
|
break;
|
|
|
|
}
|
2021-06-21 21:21:54 +02:00
|
|
|
}
|
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
$size = sizeof($currentTokens);
|
|
|
|
|
|
|
|
if ($size > 0) {
|
|
|
|
array_push($tokens[$currentTokens[$size - 1]], $line);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($tokens as $key => $store) {
|
|
|
|
if (preg_match('/\b' . $key . '_START\b/', $line)) {
|
|
|
|
array_push($currentTokens, $key);
|
|
|
|
break;
|
|
|
|
}
|
2021-06-21 21:21:54 +02:00
|
|
|
}
|
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose($fn);
|
|
|
|
|
|
|
|
$saveFile =$file . '.saved';
|
|
|
|
|
|
|
|
$saved = [];
|
2021-06-21 10:51:57 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
$stores = [];
|
2021-06-21 21:21:54 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
foreach ($tokens as $key => $store) {
|
|
|
|
if (sizeof($store) > 0) {
|
|
|
|
$stores[$key] = $store;
|
|
|
|
array_push($saved, $key . "\n");
|
|
|
|
foreach ($store as $line) {
|
|
|
|
array_push($saved, $line);
|
|
|
|
}
|
2021-06-21 21:21:54 +02:00
|
|
|
}
|
2021-06-28 09:05:07 +02:00
|
|
|
}
|
2021-06-21 21:21:54 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
file_put_contents($saveFile, $saved);
|
|
|
|
|
|
|
|
echo "saved file $saveFile\n";
|
|
|
|
|
|
|
|
return [
|
|
|
|
$saveFile,
|
|
|
|
$stores
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteSavedFile($saveFile)
|
|
|
|
{
|
|
|
|
if ($saveFile) {
|
|
|
|
unlink($saveFile);
|
|
|
|
} else {
|
|
|
|
echo "saved file was null";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getWhitespace($contents, $token)
|
|
|
|
{
|
|
|
|
$matches = [];
|
|
|
|
|
|
|
|
if (preg_match('/^(\s*)\b' . $token . '\b/m', $contents, $matches)) {
|
|
|
|
return [
|
|
|
|
'white-space' => $matches[1],
|
|
|
|
'inline-comment' => false
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (preg_match('/^(\s*)\/\/\b' . $token . '\b/m', $contents, $matches)) {
|
|
|
|
return [
|
|
|
|
'white-space' => $matches[1],
|
|
|
|
'inline-comment' => true
|
|
|
|
];
|
2021-06-21 21:21:54 +02:00
|
|
|
}
|
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
return null;
|
2021-06-21 21:21:54 +02:00
|
|
|
}
|
2021-06-28 09:05:07 +02:00
|
|
|
}
|
2021-06-21 21:21:54 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
function updateSection($file, $token, $updates, $separator = "") {
|
2021-06-21 21:21:54 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
if (getType($updates) === 'array') {
|
|
|
|
$updates = implode($separator, $updates);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen($updates) <= 0) {
|
|
|
|
echo "No contents to be updated for token $token\n";
|
2021-06-28 10:07:15 +02:00
|
|
|
return false;
|
2021-06-28 09:05:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (substr($updates, -1) !== "\n") {
|
|
|
|
$updates .= "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
$contents = file_get_contents($file);
|
|
|
|
|
|
|
|
$whiteSpaceObject = getWhitespace($contents, $token . '_END');
|
|
|
|
|
|
|
|
if ($whiteSpaceObject === null) {
|
|
|
|
/**
|
|
|
|
* This file does not contain the token which requires an update - we can return here
|
|
|
|
*/
|
2021-06-28 10:07:15 +02:00
|
|
|
echo "Skipping token $token because it was not found but it could be generated soon...\n";
|
|
|
|
return $token;
|
2021-06-28 09:05:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$endToken = $token . "_END";
|
|
|
|
|
|
|
|
if ($whiteSpaceObject['inline-comment']) {
|
|
|
|
$endToken = '//' . $endToken;
|
|
|
|
}
|
|
|
|
|
|
|
|
$whiteSpace = $whiteSpaceObject['white-space'];
|
2021-06-21 21:21:54 +02:00
|
|
|
|
|
|
|
/**
|
2021-06-28 09:05:07 +02:00
|
|
|
* If we already have whitespace in our updates at the start - we took care of
|
|
|
|
* formatting from a template, otherwise - apply some whitespace to it
|
2021-06-21 21:21:54 +02:00
|
|
|
*/
|
2021-06-28 09:05:07 +02:00
|
|
|
// if (!preg_match('/^(\s)+/', $updates)) {
|
|
|
|
// $updates = $whiteSpace . $updates;
|
|
|
|
// }
|
2021-06-21 21:21:54 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
$contents = preg_replace(
|
|
|
|
'/\b' . $token . '_START\b.*?\b' . $token . '_END\b/s',
|
|
|
|
$token . "_START\n" . $updates . $whiteSpace . $endToken,
|
|
|
|
$contents
|
|
|
|
);
|
2021-06-21 21:21:54 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
file_put_contents($file, $contents);
|
2021-06-28 10:07:15 +02:00
|
|
|
|
|
|
|
return true;
|
2021-06-28 09:05:07 +02:00
|
|
|
}
|
2021-06-21 21:21:54 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
function generateInitOptions($file, $tokens)
|
|
|
|
{
|
|
|
|
$token = 'CUSTOM_OPTIONS';
|
|
|
|
$store = getTokenStore($token, $tokens);
|
|
|
|
|
|
|
|
if (sizeof($store) <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo "Will be building options for $token\n";
|
2021-06-21 21:21:54 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
$header = file_get_contents('src/templates/generate_custom_options_init_header.template');
|
|
|
|
$template = file_get_contents('src/templates/generate_custom_options_init.template');
|
|
|
|
|
|
|
|
$updates = $header;
|
|
|
|
|
|
|
|
foreach ($store as $item) {
|
|
|
|
$item = trim($item);
|
|
|
|
$key_value = preg_split('/=/', $item);
|
|
|
|
if ($key_value === false) {
|
|
|
|
continue;
|
2021-06-21 21:21:54 +02:00
|
|
|
}
|
2021-06-28 09:05:07 +02:00
|
|
|
$key = $key_value[0];
|
|
|
|
$value = $key_value[1];
|
|
|
|
$updated = str_replace('KEY', $key, $template);
|
|
|
|
$updated = str_replace('VALUE', $value, $updated);
|
|
|
|
$updates .= $updated;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateSection($file, 'GENERATE_OPTIONS_INIT' , $updates);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getTokenStore($tokenName, $tokens)
|
|
|
|
{
|
|
|
|
if (array_key_exists($tokenName, $tokens)) {
|
|
|
|
return $tokens[$tokenName];
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
function getInstanceMappings($tokens)
|
|
|
|
{
|
|
|
|
$instanceMappings = [];
|
2021-06-21 21:21:54 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
foreach (getTokenStore('CUSTOM_INSTANCE_OPTIONS_MAPPING', $tokens) as $mapping) {
|
2021-06-21 21:21:54 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
$mapping = trim($mapping);
|
2021-06-21 21:21:54 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
$key_value = preg_split('/=/', $mapping);
|
|
|
|
|
|
|
|
if ($key_value === false) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$key = $key_value[0];
|
|
|
|
$value = $key_value[1];
|
|
|
|
|
|
|
|
$instanceMappings[$key] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $instanceMappings;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isExcluded($key, $tokens)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (array_key_exists('CUSTOM_EXCLUDED_FROM_INSTANCE_OPTIONS', $tokens)) {
|
|
|
|
|
|
|
|
$excluded = [];
|
|
|
|
|
|
|
|
foreach ($tokens['CUSTOM_EXCLUDED_FROM_INSTANCE_OPTIONS'] as $exclude) {
|
|
|
|
array_push($excluded, trim($exclude));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_array($key, $excluded)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function doInstanceUpdate($template, $tokens, $asArray = false)
|
|
|
|
{
|
|
|
|
$token = 'CUSTOM_OPTIONS';
|
|
|
|
$store = getTokenStore($token, $tokens);
|
|
|
|
|
|
|
|
if (sizeof($store) <= 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$instanceMappings = getInstanceMappings($tokens);
|
|
|
|
|
|
|
|
$updates = '';
|
|
|
|
|
|
|
|
if ($asArray) {
|
|
|
|
$updates = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($store as $item) {
|
|
|
|
|
|
|
|
$item = trim($item);
|
|
|
|
|
|
|
|
$key_value = preg_split('/=/', $item);
|
|
|
|
|
|
|
|
if ($key_value === false) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$key = $key_value[0];
|
|
|
|
$value = $key_value[0];
|
|
|
|
|
|
|
|
if (array_key_exists($key, $instanceMappings)) {
|
|
|
|
$value = $instanceMappings[$key];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isExcluded($key, $tokens)){
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$updated = str_replace('INSTANCE_KEY', $value, $template);
|
|
|
|
$updated = str_replace('KEY', $key, $updated);
|
|
|
|
if ($asArray) {
|
|
|
|
array_push($updates, $updated);
|
|
|
|
} else {
|
|
|
|
$updates .= $updated;
|
|
|
|
}
|
2021-06-21 21:21:54 +02:00
|
|
|
}
|
2021-06-28 09:05:07 +02:00
|
|
|
|
|
|
|
return $updates;
|
|
|
|
}
|
|
|
|
|
|
|
|
function generateCreateInstanceOptions($file, $tokens)
|
|
|
|
{
|
|
|
|
$template = file_get_contents('src/templates/generate_create_instance_options.template');
|
|
|
|
|
|
|
|
$updates = doInstanceUpdate($template, $tokens, true);
|
|
|
|
|
|
|
|
if ($updates) {
|
|
|
|
echo "Updating create instance options\n";
|
|
|
|
updateSection($file, 'GENERATE_CREATE_INSTANCE_OPTIONS', $updates, ",\n");
|
|
|
|
} else {
|
|
|
|
echo "No create instance options found to generate\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process updateInstance options
|
|
|
|
*/
|
|
|
|
function generateUpdateInstanceOptions($file, $tokens)
|
|
|
|
{
|
|
|
|
$template = file_get_contents('src/templates/generate_update_instance_options.template');
|
|
|
|
|
|
|
|
$updates = doInstanceUpdate($template, $tokens);
|
|
|
|
|
|
|
|
if ($updates) {
|
|
|
|
echo "Updating update instance options\n";
|
|
|
|
updateSection($file, 'GENERATE_UPDATE_INSTANCE_OPTIONS', $updates);
|
|
|
|
} else {
|
|
|
|
echo "No update instance options found to generate\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process updateFromInstance options
|
|
|
|
*/
|
|
|
|
function generateUpdateFromInstanceOptions($file, $tokens)
|
|
|
|
{
|
|
|
|
$template = file_get_contents('src/templates/generate_update_from_instance_options.template');
|
|
|
|
|
|
|
|
$updates = doInstanceUpdate($template, $tokens);
|
|
|
|
|
|
|
|
if ($updates) {
|
|
|
|
echo "Updating update from instance options\n";
|
|
|
|
updateSection($file, 'GENERATE_UPDATE_FROM_INSTANCE_OPTIONS', $updates);
|
|
|
|
} else {
|
|
|
|
echo "No update from instance options found to generate\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function doMethodUpdate($template, $tokens, $token)
|
|
|
|
{
|
|
|
|
$store = getTokenStore($token, $tokens);
|
|
|
|
|
|
|
|
if (sizeof($store) <= 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$updates = '';
|
|
|
|
|
|
|
|
foreach ($store as $item) {
|
|
|
|
|
|
|
|
$item = trim($item);
|
|
|
|
|
|
|
|
$methodName = $item;
|
|
|
|
|
|
|
|
$matches = [];
|
|
|
|
$result = preg_match('/^(.*?)\(/', $item, $matches);
|
|
|
|
|
|
|
|
if ($result !== false && sizeof($matches) >= 2) {
|
|
|
|
$methodName = $matches[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
$args = '';
|
|
|
|
$result = preg_match('/\((.*)\)/', $item, $matches);
|
|
|
|
|
|
|
|
if ($result !== false && sizeof($matches) >= 2) {
|
|
|
|
$args = $matches[1];
|
|
|
|
}
|
|
|
|
|
2021-06-28 10:07:15 +02:00
|
|
|
$methodTokenName = strtoupper(from_camel_case($methodName));
|
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
$updated = str_replace('METHOD_ARGS', $args, $template);
|
2021-06-28 10:07:15 +02:00
|
|
|
$updated = str_replace('METHOD_NAME_UPPERCASE', $methodTokenName, $updated);
|
2021-06-28 09:05:07 +02:00
|
|
|
$updated = str_replace('METHOD_NAME', $methodName, $updated);
|
|
|
|
$updates .= $updated;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $updates;
|
|
|
|
}
|
|
|
|
|
|
|
|
function generateStaticMethods($file, $tokens)
|
|
|
|
{
|
|
|
|
$token = 'CUSTOM_STATIC_METHODS';
|
|
|
|
|
|
|
|
$template = file_get_contents('src/templates/generate_static_methods.template');
|
|
|
|
|
|
|
|
$updates = doMethodUpdate($template, $tokens, $token);
|
|
|
|
|
|
|
|
if ($updates) {
|
|
|
|
echo "Updating static methods\n";
|
|
|
|
updateSection($file, 'GENERATE_STATIC_METHODS', $updates);
|
|
|
|
} else {
|
|
|
|
echo "No static methods found to generate\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function generateMethods($file, $tokens)
|
|
|
|
{
|
|
|
|
$token = 'CUSTOM_METHODS';
|
|
|
|
|
|
|
|
$template = file_get_contents('src/templates/generate_methods.template');
|
|
|
|
|
|
|
|
$updates = doMethodUpdate($template, $tokens, $token);
|
|
|
|
|
|
|
|
if ($updates) {
|
|
|
|
echo "Updating methods\n";
|
|
|
|
updateSection($file, 'GENERATE_METHODS', $updates);
|
|
|
|
} else {
|
|
|
|
echo "No methods found to generate\n";
|
|
|
|
}
|
|
|
|
|
2021-06-21 21:21:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
global $files;
|
|
|
|
|
|
|
|
foreach ($files as $file) {
|
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
$saveFile = null;
|
2021-06-21 21:21:54 +02:00
|
|
|
|
|
|
|
if ($argv[2] == 'save') {
|
|
|
|
|
2021-06-28 10:07:15 +02:00
|
|
|
$saveFile = $file . '.saved';
|
|
|
|
|
|
|
|
if (file_exists($saveFile)) {
|
|
|
|
|
|
|
|
echo "A previous restore operation did not complete - please remove the saved file before trying this again\n";
|
|
|
|
echo "The save file is located at $saveFile\n";
|
|
|
|
echo "Remove easily with:\n";
|
|
|
|
echo "rm $saveFile\n";
|
|
|
|
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
$tokens = getTokens('CUSTOM');
|
2021-06-21 21:21:54 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
$result = save($file, $tokens);
|
2021-06-21 21:21:54 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
exit(0);
|
2021-06-21 21:21:54 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
} else if ($argv[2] == 'restore') {
|
2021-06-21 21:21:54 +02:00
|
|
|
|
2021-06-28 10:07:15 +02:00
|
|
|
$saveFile = $file . '.saved';
|
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
$restoreTokens = getTokens('CUSTOM');
|
2021-06-21 21:21:54 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
$restoreTokenKeys = array_keys($restoreTokens);
|
2021-06-21 21:21:54 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
$tokens = loadSaved($file, $restoreTokens);
|
2021-06-19 11:27:32 +02:00
|
|
|
|
2021-06-28 10:07:15 +02:00
|
|
|
$skipped = [];
|
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
foreach ($tokens as $token => $store) {
|
|
|
|
if (in_array($token, $restoreTokenKeys)) {
|
|
|
|
updateSection($file, $token, $store);
|
2021-06-21 21:21:54 +02:00
|
|
|
}
|
2021-06-28 09:05:07 +02:00
|
|
|
}
|
2021-06-20 08:13:06 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
generateInitOptions($file, $tokens);
|
2021-06-20 08:13:06 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
generateCreateInstanceOptions($file, $tokens);
|
2021-06-21 21:21:54 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
generateUpdateInstanceOptions($file, $tokens);
|
|
|
|
|
|
|
|
generateUpdateFromInstanceOptions($file, $tokens);
|
|
|
|
|
|
|
|
generateMethods($file, $tokens);
|
|
|
|
|
|
|
|
generateStaticMethods($file, $tokens);
|
|
|
|
|
|
|
|
echo `r3 update-token-db`;
|
|
|
|
|
2021-06-28 10:07:15 +02:00
|
|
|
/**
|
|
|
|
* Try to restore the rest of the old data because now methods were generated.
|
|
|
|
* If not all data restores now - a method name / token has changed and we need
|
|
|
|
* to notify the user
|
|
|
|
*/
|
2021-06-21 21:21:54 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
foreach ($tokens as $token => $store) {
|
|
|
|
if (in_array($token, $restoreTokenKeys)) {
|
2021-06-28 10:07:15 +02:00
|
|
|
$result = updateSection($file, $token, $store);
|
|
|
|
if ($result !== true && $result !== false) {
|
|
|
|
array_push($skipped, $result);
|
|
|
|
}
|
2021-06-21 21:21:54 +02:00
|
|
|
}
|
2021-06-28 09:05:07 +02:00
|
|
|
}
|
|
|
|
|
2021-06-28 10:07:15 +02:00
|
|
|
if (sizeof($skipped) !== 0) {
|
|
|
|
echo "Some tokens could not be restored because they could not be found in the new version\n";
|
|
|
|
print_r($skipped);
|
|
|
|
echo "Please restore them manually from the saved file :$saveFile\n";
|
|
|
|
echo "If you do not do it now - on the next template update code will be overwritten and you could lose code!!!\n";
|
|
|
|
exit(1);
|
|
|
|
} else {
|
|
|
|
deleteSavedFile($saveFile);
|
|
|
|
exit(0);
|
|
|
|
}
|
2021-06-28 09:05:07 +02:00
|
|
|
|
|
|
|
} else if ($argv[2] == 'update-methods') {
|
|
|
|
|
|
|
|
$restoreTokens = getTokens('CUSTOM');
|
|
|
|
|
|
|
|
$restoreTokenKeys = array_keys($restoreTokens);
|
|
|
|
|
|
|
|
$tokens = loadSaved($file, $restoreTokens);
|
2021-06-20 08:13:06 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
foreach ($tokens as $token => $store) {
|
|
|
|
if (in_array($token, $restoreTokenKeys)) {
|
|
|
|
updateSection($file, $token, $store, "\n ");
|
|
|
|
}
|
2021-06-19 11:27:32 +02:00
|
|
|
}
|
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
generateMethods($file, $tokens);
|
2021-06-21 21:21:54 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
generateStaticMethods($file, $tokens);
|
2021-06-21 21:21:54 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
echo `r3 update-token-db`;
|
2021-06-21 21:21:54 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
} else if ($argv[2] == 'update-options') {
|
2021-06-21 21:21:54 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
$tokens = getTokens('CUSTOM');
|
2021-06-21 21:21:54 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
$result = save($file, $tokens);
|
2021-06-21 21:21:54 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
$saveFile = $result[0];
|
|
|
|
$tokens = $result[1];
|
2021-06-21 21:21:54 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
try {
|
|
|
|
generateInitOptions($file, $tokens);
|
2021-06-21 21:21:54 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
generateCreateInstanceOptions($file, $tokens);
|
|
|
|
|
|
|
|
generateUpdateInstanceOptions($file, $tokens);
|
2021-06-21 21:21:54 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
generateUpdateFromInstanceOptions($file, $tokens);
|
2021-06-21 21:21:54 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
generateMethods($file, $tokens);
|
2021-06-21 21:21:54 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
generateStaticMethods($file, $tokens);
|
2021-06-21 21:21:54 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
echo `r3 update-token-db`;
|
2021-06-21 21:21:54 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
// unlink($saveFile);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
print_r($e);
|
|
|
|
echo "An error occurred and the custom data is saved at $saveFile\n";
|
|
|
|
echo "Please restore it manually...\n";
|
|
|
|
}
|
2021-06-21 21:21:54 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-19 11:27:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
|
|
|
|
?>
|