2021-06-19 11:27:32 +02:00
|
|
|
#!/usr/bin/php
|
|
|
|
<?php
|
|
|
|
|
2021-06-21 11:43:10 +02:00
|
|
|
include "utils.php";
|
2021-07-01 11:04:00 +02:00
|
|
|
include "graph.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-28 09:05:07 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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 = [];
|
|
|
|
|
2021-06-29 10:57:24 +02:00
|
|
|
if (preg_match('/^\n*(\s*)\b' . $token . '\b/m', $contents, $matches)) {
|
2021-06-28 09:05:07 +02:00
|
|
|
return [
|
|
|
|
'white-space' => $matches[1],
|
|
|
|
'inline-comment' => false
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
|
2021-06-29 10:57:24 +02:00
|
|
|
if (preg_match('/^\n*(\s*)\/\/\b' . $token . '\b/m', $contents, $matches)) {
|
2021-06-28 09:05:07 +02:00
|
|
|
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
|
|
|
$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-07-01 16:39:25 +02:00
|
|
|
function doGetInheritedTemplateUpdates($node, $restoreTokens)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$tokens = loadSaved($node->file, $restoreTokens);
|
|
|
|
} catch (ErrorException $e) {
|
|
|
|
echo $e->getMessage();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
$updates = '';
|
|
|
|
|
|
|
|
if ($node->parent !== null && $node->parent->name !== "R3") {
|
|
|
|
$updates = doGetInheritedTemplateUpdates($node->parent, $restoreTokens);
|
|
|
|
}
|
|
|
|
|
|
|
|
$template = file_get_contents('src/templates/generate_inherited.template');
|
|
|
|
|
|
|
|
$CLASS_NAME = $node->name;
|
|
|
|
$PROPERTY_LIST = 'test';
|
|
|
|
$METHOD_LIST = 'test';
|
|
|
|
$STATIC_METHOD_LIST = 'test';
|
|
|
|
|
|
|
|
$updated = str_replace('CLASS_NAME', $CLASS_NAME, $template);
|
|
|
|
$updated = str_replace('PROPERTY_LIST', $PROPERTY_LIST, $updated);
|
|
|
|
$updated = str_replace('STATIC_METHOD_LIST', $STATIC_METHOD_LIST, $updated);
|
|
|
|
$updated = str_replace('METHOD_LIST', $METHOD_LIST, $updated);
|
|
|
|
$updates .= $updated;
|
|
|
|
|
|
|
|
return $updates;
|
|
|
|
}
|
|
|
|
|
|
|
|
function generateInherited($file, $restoreTokens)
|
|
|
|
{
|
|
|
|
|
|
|
|
echo "Generating inherited fields for $file\n";
|
|
|
|
|
|
|
|
global $graph;
|
|
|
|
|
|
|
|
$node = $graph->search('file', $file);
|
|
|
|
|
|
|
|
if ($node === null) {
|
|
|
|
/**
|
|
|
|
* This node is not part of R3
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($node->parent === null) {
|
|
|
|
/**
|
|
|
|
* This node has no inherited properties
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($node->parent->name === 'R3') {
|
|
|
|
/**
|
|
|
|
* This is a base class
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$updates = doGetInheritedTemplateUpdates($node->parent, $restoreTokens);
|
|
|
|
|
|
|
|
updateSection($file, 'GENERATE_INHERITED' , $updates);
|
|
|
|
}
|
|
|
|
|
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 = '';
|
|
|
|
|
2021-06-29 10:57:24 +02:00
|
|
|
|
2021-06-28 09:05:07 +02:00
|
|
|
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 = '';
|
2021-06-29 10:57:24 +02:00
|
|
|
$result = preg_match('/\((.*?)\)/', $item, $matches);
|
2021-06-28 09:05:07 +02:00
|
|
|
|
|
|
|
if ($result !== false && sizeof($matches) >= 2) {
|
|
|
|
$args = $matches[1];
|
|
|
|
}
|
|
|
|
|
2021-06-29 10:57:24 +02:00
|
|
|
$params = $args;
|
|
|
|
|
|
|
|
$argsArray = preg_split('/,(\s*)/', $args);
|
|
|
|
|
|
|
|
if (sizeof($argsArray) > 1) {
|
|
|
|
$args = implode(",\n\t\t", $argsArray);
|
|
|
|
$args = "\n\t\t" . $args . "\n\t";
|
|
|
|
$params = implode("\n\t * @param ", $argsArray);
|
|
|
|
$params = "@param " . $params;
|
2021-07-01 16:39:25 +02:00
|
|
|
} else {
|
|
|
|
$params = "\n\t * @param " . $params;
|
2021-06-29 10:57:24 +02:00
|
|
|
}
|
|
|
|
|
2021-06-29 11:09:25 +02:00
|
|
|
$result = preg_match('/.*?\).*?(\w.*$)/', $item, $matches);
|
2021-06-29 10:57:24 +02:00
|
|
|
if ($result !== false && sizeof($matches) >= 2) {
|
|
|
|
$comment = $matches[1];
|
|
|
|
}
|
|
|
|
|
2021-06-29 11:09:25 +02:00
|
|
|
if ($result === 0) {
|
|
|
|
$comment = 'No comment';
|
|
|
|
}
|
|
|
|
|
2021-06-29 10:57:24 +02:00
|
|
|
$returns = preg_split('/@return[s]*\s*/', $comment);
|
|
|
|
|
|
|
|
if (sizeof($returns) > 1) {
|
|
|
|
$comment = $returns[0];
|
|
|
|
$returns = '@returns ' . $returns[1];
|
|
|
|
} else {
|
|
|
|
$returns = '';
|
|
|
|
}
|
|
|
|
|
2021-06-28 10:07:15 +02:00
|
|
|
$methodTokenName = strtoupper(from_camel_case($methodName));
|
|
|
|
|
2021-06-29 10:57:24 +02:00
|
|
|
$spaces = '';
|
|
|
|
$length = strlen($methodName . '() - ');
|
|
|
|
for ($i = 0; $i < $length; $i++) {
|
|
|
|
$spaces = ' ' . $spaces;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$comment = wordwrap($comment, 100, "\n * " . $spaces);
|
|
|
|
|
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);
|
2021-06-29 10:57:24 +02:00
|
|
|
$updated = str_replace('COMMENT', $comment, $updated);
|
|
|
|
$updated = str_replace('PARAMS', $params, $updated);
|
|
|
|
$updated = str_replace('RETURNS', $returns, $updated);
|
2021-06-28 09:05:07 +02:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
2021-07-01 11:04:00 +02:00
|
|
|
$nodeList = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws ErrorException
|
|
|
|
*/
|
2021-07-01 16:39:25 +02:00
|
|
|
function buildNodeList($file)
|
2021-07-01 11:04:00 +02:00
|
|
|
{
|
|
|
|
echo "loading file $file\n";
|
|
|
|
|
|
|
|
global $nodeList;
|
|
|
|
|
|
|
|
$fn = fopen($file, "r");
|
|
|
|
|
|
|
|
$line = '';
|
|
|
|
$found = false;
|
|
|
|
|
|
|
|
while (!feof($fn) && !$found) {
|
|
|
|
$line = fgets($fn);
|
|
|
|
|
|
|
|
if (preg_match('/^class\s+/', $line)) {
|
|
|
|
$found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($found) {
|
|
|
|
// echo $line . "\n";
|
|
|
|
|
|
|
|
$matches = [];
|
|
|
|
|
|
|
|
$result = preg_match('/^class\s+(.*?)(\s+extends\s+(.*?)\s+|\s+?{)/', $line, $matches);
|
|
|
|
|
|
|
|
if ($result === false) {
|
|
|
|
throw new ErrorException('Could not match');
|
|
|
|
}
|
|
|
|
|
|
|
|
$extends = null;
|
|
|
|
|
|
|
|
if (sizeof($matches) === 4) {
|
|
|
|
$class = $matches[1];
|
|
|
|
$extends = $matches[3];
|
|
|
|
} else if (sizeof($matches) === 3) {
|
|
|
|
$class = $matches[1];
|
|
|
|
} else {
|
|
|
|
throw new ErrorException('Unhandled case');
|
|
|
|
}
|
|
|
|
|
|
|
|
$node = new Node(
|
|
|
|
$file,
|
|
|
|
$class,
|
|
|
|
$extends
|
|
|
|
);
|
|
|
|
|
|
|
|
array_push($nodeList, $node);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
echo "not found\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose($fn);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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-07-01 16:39:25 +02:00
|
|
|
try {
|
|
|
|
$tokens = loadSaved($file, $restoreTokens);
|
|
|
|
} catch (ErrorException $e) {
|
|
|
|
echo $e->getMessage();
|
|
|
|
exit(1);
|
|
|
|
}
|
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 {
|
2021-07-01 16:39:25 +02:00
|
|
|
// deleteSavedFile($saveFile);
|
2021-07-01 17:16:15 +02:00
|
|
|
// exit(0);
|
2021-06-28 10:07:15 +02:00
|
|
|
}
|
2021-07-01 11:04:00 +02:00
|
|
|
} else if ($argv[2] == 'build-graph') {
|
2021-07-01 16:39:25 +02:00
|
|
|
buildNodeList($file);
|
2021-06-21 21:21:54 +02:00
|
|
|
}
|
2021-06-19 11:27:32 +02:00
|
|
|
}
|
|
|
|
|
2021-07-01 11:04:00 +02:00
|
|
|
if ($argv[2] == 'build-graph') {
|
2021-07-01 16:39:25 +02:00
|
|
|
|
|
|
|
global $nodeList;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Now generate the graph based on the new classes
|
|
|
|
*/
|
2021-07-01 11:04:00 +02:00
|
|
|
$graph = new Graph(
|
|
|
|
$nodeList
|
|
|
|
);
|
2021-07-01 16:39:25 +02:00
|
|
|
|
|
|
|
$restoreTokens = getTokens('CUSTOM');
|
|
|
|
|
|
|
|
$restoreTokenKeys = array_keys($restoreTokens);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Now start processing the files again - this time generating linked objects / inherited properties / methods
|
|
|
|
*/
|
|
|
|
foreach ($files as $file) {
|
|
|
|
|
|
|
|
$saveFile = $file . '.saved';
|
|
|
|
|
|
|
|
generateInherited($file, $restoreTokens);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($files as $file) {
|
|
|
|
|
|
|
|
$saveFile = $file . '.saved';
|
|
|
|
|
|
|
|
deleteSavedFile($saveFile);
|
|
|
|
}
|
|
|
|
|
2021-07-01 11:04:00 +02:00
|
|
|
}
|
|
|
|
|
2021-07-01 16:39:25 +02:00
|
|
|
|
|
|
|
|
2021-06-19 11:27:32 +02:00
|
|
|
exit(0);
|
|
|
|
|
|
|
|
?>
|