restore functions and options

master
Theunis J. Botha 2021-06-28 09:05:07 +02:00
parent 69154f9d38
commit 9107d31d22
27 changed files with 925 additions and 536 deletions

View File

@ -3,4 +3,10 @@ r3 create R3Object extends Event
r3 create Utils normal
r3 create Component extends R3Object
r3 create Project extends R3Object
r3 create System static ./r3-system/
r3 create A normal
r3 create B extends A
r3 create C normal ./r3-system/
r3 create D system C
r3 create E static ./
r3 create T extends B
r3 create System normal ./r3-system/

View File

@ -3,28 +3,89 @@ const Utils = require('.././r3-utils');
/**
STATIC_METHODS_START
Start()
Stop()
STATIC_METHODS_END
CUSTOM_OPTIONS_START
started=false
CUSTOM_OPTIONS_END
CUSTOM_METHODS_START
start(options, anotherOption)
stop(options)
CUSTOM_METHODS_END
CUSTOM_STATIC_METHODS_START
Start(options)
Stop(options)
CUSTOM_STATIC_METHODS_END
**/
class System {
//STATIC_METHODS_DEFINITION_START
static Start() {
//CUSTOM_START()_METHOD_START
//CUSTOM_START()_METHOD_END
}
static Stop() {
//CUSTOM_STOP()_METHOD_START
//CUSTOM_STOP()_METHOD_END
}
//STATIC_METHODS_DEFINITION_END
//GENERATE_CONSTRUCTOR_START
constructor(options) {
//CUSTOM_IMPLEMENTATION_START
//CUSTOM_IMPLEMENTATION_END
Event.Emit(Event.OBJECT_CREATED, this);
//GENERATE_OPTIONS_INIT_START
if (typeof options === 'undefined') {
options = {};
}
if (Utils.UndefinedOrNull(options.started)) {
options.started = false;
}
//GENERATE_OPTIONS_INIT_END
//CUSTOM_OPTIONS_INIT_START
//CUSTOM_OPTIONS_INIT_END
Object.assign(this, options);
//CUSTOM_BEFORE_INIT_START
//CUSTOM_BEFORE_INIT_END
Event.Emit(Event.OBJECT_INITIALIZED, this);
//CUSTOM_AFTER_INIT_START
//CUSTOM_AFTER_INIT_END
}
//GENERATE_CONSTRUCTOR_END
//GENERATE_METHODS_START
start(options, anotherOption) {
//CUSTOM_START_METHOD_START
System.Start(options);
/**
* Now do something else
*/
console.log('something else');
//CUSTOM_START_METHOD_END
}
stop(options) {
//CUSTOM_STOP_METHOD_START
System.Stop(options);
//CUSTOM_STOP_METHOD_END
}
//GENERATE_METHODS_END
//GENERATE_STATIC_METHODS_START
static Start(options) {
//CUSTOM_STATIC_START_METHOD_START
console.log('Starting system X');
//CUSTOM_STATIC_START_METHOD_END
}
static Stop(options) {
//CUSTOM_STATIC_STOP_METHOD_START
console.log('Stopping system X');
//CUSTOM_STATIC_STOP_METHOD_END
}
//GENERATE_STATIC_METHODS_END
//CUSTOM_IMPLEMENTATION_START
//CUSTOM_IMPLEMENTATION_END
}

View File

@ -2,8 +2,8 @@
Event.Emit(Event.OBJECT_CREATED, this);
//OPTIONS_INIT_START
//OPTIONS_INIT_END
//GENERATE_OPTIONS_INIT_START
//GENERATE_OPTIONS_INIT_END
//CUSTOM_OPTIONS_INIT_START
//CUSTOM_OPTIONS_INIT_END
@ -14,4 +14,7 @@
//CUSTOM_BEFORE_INIT_END
Event.Emit(Event.OBJECT_INITIALIZED, this);
//CUSTOM_AFTER_INIT_START
//CUSTOM_AFTER_INIT_END
}

View File

@ -8,8 +8,8 @@
this.emit(Event.OBJECT_CREATED, this);
//OPTIONS_INIT_START
//OPTIONS_INIT_END
//GENERATE_OPTIONS_INIT_START
//GENERATE_OPTIONS_INIT_END
//CUSTOM_OPTIONS_INIT_START
//CUSTOM_OPTIONS_INIT_END
@ -20,4 +20,7 @@
//CUSTOM_BEFORE_INIT_END
this.emit(Event.OBJECT_INITIALIZED, this);
//CUSTOM_AFTER_INIT_START
//CUSTOM_AFTER_INIT_END
}

View File

@ -1,12 +1,12 @@
createInstance() {
//CREATE_INSTANCE_BEFORE_START
//CREATE_INSTANCE_BEFORE_END
//GENERATE_CREATE_INSTANCE_BEFORE_START
//GENERATE_CREATE_INSTANCE_BEFORE_END
//CUSTOM_CREATE_INSTANCE_START
//CUSTOM_CREATE_INSTANCE_END
//CREATE_INSTANCE_AFTER_START
//CREATE_INSTANCE_AFTER_END
//GENERATE_CREATE_INSTANCE_AFTER_START
//GENERATE_CREATE_INSTANCE_AFTER_END
}

View File

@ -1,9 +1,8 @@
this[this.runtime].createInstance(
this,
{
//CREATE_INSTANCE_OPTIONS_START
//CREATE_INSTANCE_OPTIONS_END
//GENERATE_CREATE_INSTANCE_OPTIONS_START
//GENERATE_CREATE_INSTANCE_OPTIONS_END
}
)
this.emit(Event.INSTANCE_CREATED, this);

View File

@ -1,12 +1,12 @@
dispose() {
//DISPOSE_BEFORE_START
//DISPOSE_BEFORE_END
//GENERATE_DISPOSE_BEFORE_START
//GENERATE_DISPOSE_BEFORE_END
//CUSTOM_DISPOSE_START
//CUSTOM_DISPOSE_END
//DISPOSE_AFTER_START
//DISPOSE_AFTER_END
//GENERATE_DISPOSE_AFTER_START
//GENERATE_DISPOSE_AFTER_END
}

View File

@ -1,12 +1,12 @@
disposeInstance() {
//DISPOSE_INSTANCE_BEFORE_START
//DISPOSE_INSTANCE_BEFORE_END
//GENERATE_DISPOSE_INSTANCE_BEFORE_START
//GENERATE_DISPOSE_INSTANCE_BEFORE_END
//CUSTOM_DISPOSE_INSTANCE_START
//CUSTOM_DISPOSE_INSTANCE_END
//DISPOSE_INSTANCE_AFTER_START
//DISPOSE_INSTANCE_AFTER_END
//GENERATE_DISPOSE_INSTANCE_AFTER_START
//GENERATE_DISPOSE_INSTANCE_AFTER_END
}

View File

@ -1,46 +1,50 @@
const Event = require('r3-event');
const Utils = require('r3-utils');
const Event = require('INCLUDE_PATH/r3-event');
const Utils = require('INCLUDE_PATH/r3-utils');
const EXTEND_CLASS = require('EXTEND_CLASS_FILE_NAME');
/**
OPTIONS_START
OPTIONS_END
GENERATE_INHERITED_START
GENERATE_INHERITED_END
INSTANCE_OPTIONS_MAPPING_START
INSTANCE_OPTIONS_MAPPING_END
Of the form x=<value>
CUSTOM_OPTIONS_START
CUSTOM_OPTIONS_END
LINKED_OBJECTS_START
LINKED_OBJECTS_END
Of the form x=<instance.property>
CUSTOM_INSTANCE_OPTIONS_MAPPING_START
CUSTOM_INSTANCE_OPTIONS_MAPPING_END
EXCLUDED_FROM_INSTANCE_OPTIONS_START
EXCLUDED_FROM_INSTANCE_OPTIONS_END
CUSTOM_LINKED_OBJECTS_START
CUSTOM_LINKED_OBJECTS_END
CUSTOM_EXCLUDED_FROM_INSTANCE_OPTIONS_START
CUSTOM_EXCLUDED_FROM_INSTANCE_OPTIONS_END
**/
class CLASS_NAME extends EXTEND_CLASS {
//CONSTRUCTOR_EXTENDS_TEMPLATE_START
//CONSTRUCTOR_EXTENDS_TEMPLATE_END
//GENERATE_CONSTRUCTOR_EXTENDS_START
//GENERATE_CONSTRUCTOR_EXTENDS_END
//CREATE_INSTANCE_TEMPLATE_START
//CREATE_INSTANCE_TEMPLATE_END
//GENERATE_CREATE_INSTANCE_START
//GENERATE_CREATE_INSTANCE_END
//UPDATE_INSTANCE_TEMPLATE_START
//UPDATE_INSTANCE_TEMPLATE_END
//GENERATE_UPDATE_INSTANCE_START
//GENERATE_UPDATE_INSTANCE_END
//UPDATE_FROM_INSTANCE_TEMPLATE_START
//UPDATE_FROM_INSTANCE_TEMPLATE_END
//GENERATE_UPDATE_FROM_INSTANCE_START
//GENERATE_UPDATE_FROM_INSTANCE_END
//DISPOSE_TEMPLATE_START
//DISPOSE_TEMPLATE_END
//GENERATE_DISPOSE_START
//GENERATE_DISPOSE_END
//DISPOSE_INSTANCE_TEMPLATE_START
//DISPOSE_INSTANCE_TEMPLATE_END
//GENERATE_DISPOSE_INSTANCE_START
//GENERATE_DISPOSE_INSTANCE_END
//CUSTOM_IMPLEMENTATION_START
//CUSTOM_IMPLEMENTATION_END
}
//CUSTOM_OUT_OF_CLASS_IMPLEMENTATION_START

View File

@ -1,3 +1,3 @@
if (typeof options === 'undefined') {
options = {};
options = {};
}

View File

@ -0,0 +1,4 @@
METHOD_NAME(METHOD_ARGS) {
//CUSTOM_METHOD_NAME_UPPERCASE_METHOD_START
//CUSTOM_METHOD_NAME_UPPERCASE_METHOD_END
}

View File

@ -0,0 +1,4 @@
static METHOD_NAME(METHOD_ARGS) {
//CUSTOM_STATIC_METHOD_NAME_UPPERCASE_METHOD_START
//CUSTOM_STATIC_METHOD_NAME_UPPERCASE_METHOD_END
}

View File

@ -1,6 +1,7 @@
if (property === 'KEY' || property === 'all') {
this.KEY = this.instance.INSTANCE_KEY;
if (property !== 'all') {
this.emit(Event.UPDATE_FROM_INSTANCE_AFTER, this);
return;
}
}

View File

@ -0,0 +1,7 @@
if (property === 'KEY') {
this.instance.INSTANCE_KEY = this.KEY;
if (property !== 'all') {
this.emit(Event.UPDATE_INSTANCE_AFTER, this);
return;
}
}

View File

@ -3,15 +3,27 @@ const Utils = require('INCLUDE_PATH/r3-utils');
/**
OPTIONS_START
OPTIONS_END
CUSTOM_OPTIONS_START
CUSTOM_OPTIONS_END
CUSTOM_METHODS_START
CUSTOM_METHODS_END
CUSTOM_STATIC_METHODS_START
CUSTOM_STATIC_METHODS_END
**/
class CLASS_NAME {
//CONSTRUCTOR_TEMPLATE_START
//CONSTRUCTOR_TEMPLATE_END
//GENERATE_CONSTRUCTOR_START
//GENERATE_CONSTRUCTOR_END
//GENERATE_METHODS_START
//GENERATE_METHODS_END
//GENERATE_STATIC_METHODS_START
//GENERATE_STATIC_METHODS_END
//CUSTOM_IMPLEMENTATION_START
//CUSTOM_IMPLEMENTATION_END

View File

@ -3,15 +3,15 @@ const Utils = require('INCLUDE_PATH/r3-utils');
/**
STATIC_METHODS_START
STATIC_METHODS_END
CUSTOM_STATIC_METHODS_START
CUSTOM_STATIC_METHODS_END
**/
class CLASS_NAME {
//STATIC_METHODS_DEFINITION_START
//STATIC_METHODS_DEFINITION_END
//GENERATE_STATIC_METHODS_START
//GENERATE_STATIC_METHODS_END
//CUSTOM_IMPLEMENTATION_START
//CUSTOM_IMPLEMENTATION_END

View File

@ -1,4 +0,0 @@
static METHOD_NAME {
//CUSTOM_METHOD_NAME_UPPERCASE_METHOD_START
//CUSTOM_METHOD_NAME_UPPERCASE_METHOD_END
}

View File

@ -1,17 +1,27 @@
const Event = require('r3-event');
const Utils = require('r3-utils');
const Event = require('INCLUDE_PATH/r3-event');
const Utils = require('INCLUDE_PATH/r3-utils');
const EXTEND_CLASS = require('EXTEND_CLASS_FILE_NAME');
/**
OPTIONS_START
OPTIONS_END
GENERATE_INHERITED_START
GENERATE_INHERITED_END
CUSTOM_OPTIONS_START
CUSTOM_OPTIONS_END
CUSTOM_EVENT_LISTENERS_START
CUSTOM_EVENT_LISTENERS_END
**/
class CLASS_NAME {
class CLASS_NAME extends EXTEND_CLASS {
//CONSTRUCTOR_TEMPLATE_START
//CONSTRUCTOR_TEMPLATE_END
//GENERATE_CONSTRUCTOR_EXTENDS_START
//GENERATE_CONSTRUCTOR_EXTENDS_END
//GENERATE_EVENT_LISTENERS_START
//GENERATE_EVENT_LISTENERS_END
//CUSTOM_IMPLEMENTATION_START
//CUSTOM_IMPLEMENTATION_END

48
src/templates/token.db Normal file
View File

@ -0,0 +1,48 @@
GENERATE_CONSTRUCTOR
GENERATE_CONSTRUCTOR_EXTENDS
GENERATE_CREATE_INSTANCE
GENERATE_CREATE_INSTANCE_AFTER
GENERATE_CREATE_INSTANCE_BEFORE
GENERATE_CREATE_INSTANCE_OPTIONS
GENERATE_DISPOSE
GENERATE_DISPOSE_AFTER
GENERATE_DISPOSE_BEFORE
GENERATE_DISPOSE_INSTANCE
GENERATE_DISPOSE_INSTANCE_AFTER
GENERATE_DISPOSE_INSTANCE_BEFORE
GENERATE_EVENT_LISTENERS
GENERATE_INHERITED
GENERATE_METHODS
GENERATE_OPTIONS_INIT
GENERATE_STATIC_METHODS
GENERATE_UPDATE_FROM_INSTANCE
GENERATE_UPDATE_FROM_INSTANCE_AFTER
GENERATE_UPDATE_FROM_INSTANCE_BEFORE
GENERATE_UPDATE_FROM_INSTANCE_OPTIONS
GENERATE_UPDATE_INSTANCE
GENERATE_UPDATE_INSTANCE_AFTER
GENERATE_UPDATE_INSTANCE_BEFORE
GENERATE_UPDATE_INSTANCE_OPTIONS
CUSTOM_AFTER_INIT
CUSTOM_BEFORE_INIT
CUSTOM_CREATE_INSTANCE
CUSTOM_DISPOSE
CUSTOM_DISPOSE_INSTANCE
CUSTOM_EVENT_LISTENERS
CUSTOM_EXCLUDED_FROM_INSTANCE_OPTIONS
CUSTOM_IMPLEMENTATION
CUSTOM_INSTANCE_OPTIONS_MAPPING
CUSTOM_LINKED_OBJECTS
CUSTOM_METHOD_NAME_UPPERCASE_METHOD
CUSTOM_METHODS
CUSTOM_OPTIONS
CUSTOM_OPTIONS_INIT
CUSTOM_OUT_OF_CLASS_IMPLEMENTATION
CUSTOM_START_METHOD
CUSTOM_STATIC_METHOD_NAME_UPPERCASE_METHOD
CUSTOM_STATIC_METHODS
CUSTOM_STATIC_START_METHOD
CUSTOM_STATIC_STOP_METHOD
CUSTOM_STOP_METHOD
CUSTOM_UPDATE_FROM_INSTANCE
CUSTOM_UPDATE_INSTANCE

View File

@ -1,15 +1,15 @@
updateFromInstance(property) {
//UPDATE_FROM_INSTANCE_BEFORE_START
//UPDATE_FROM_INSTANCE_BEFORE_END
//GENERATE_UPDATE_FROM_INSTANCE_BEFORE_START
//GENERATE_UPDATE_FROM_INSTANCE_BEFORE_END
//UPDATE_FROM_INSTANCE_OPTIONS_START
//UPDATE_FROM_INSTANCE_OPTIONS_END
//GENERATE_UPDATE_FROM_INSTANCE_OPTIONS_START
//GENERATE_UPDATE_FROM_INSTANCE_OPTIONS_END
//CUSTOM_UPDATE_FROM_INSTANCE_START
//CUSTOM_UPDATE_FROM_INSTANCE_END
//UPDATE_FROM_INSTANCE_AFTER_START
//UPDATE_FROM_INSTANCE_AFTER_END
//GENERATE_UPDATE_FROM_INSTANCE_AFTER_START
//GENERATE_UPDATE_FROM_INSTANCE_AFTER_END
}

View File

@ -1,15 +1,15 @@
updateInstance(property) {
//UPDATE_INSTANCE_BEFORE_START
//UPDATE_INSTANCE_BEFORE_END
//GENERATE_UPDATE_INSTANCE_BEFORE_START
//GENERATE_UPDATE_INSTANCE_BEFORE_END
//UPDATE_INSTANCE_OPTIONS_START
//UPDATE_INSTANCE_OPTIONS_END
//GENERATE_UPDATE_INSTANCE_OPTIONS_START
//GENERATE_UPDATE_INSTANCE_OPTIONS_END
//CUSTOM_UPDATE_INSTANCE_START
//CUSTOM_UPDATE_INSTANCE_END
//UPDATE_INSTANCE_AFTER_START
//UPDATE_INSTANCE_AFTER_END
//GENERATE_UPDATE_INSTANCE_AFTER_START
//GENERATE_UPDATE_INSTANCE_AFTER_END
}

View File

@ -1,4 +0,0 @@
if (property === 'KEY') {
this.instance.INSTANCE_KEY = this.KEY;
return;
}

View File

View File

@ -1,266 +0,0 @@
#!/usr/bin/php
<?php
include "utils.php";
global $files;
foreach ($files as $file) {
echo $file . "\n";
echo "processing file " . $file . "\n";
$fn = fopen($file, "r");
$saveLine = true;
$options = [];
$instanceMappings = [];
$excludeOptions = [];
$saveOption = false;
$saveInstanceMappings = false;
$saveExcludeOptions = false;
$lines = [];
while (!feof($fn)) {
$line = fgets($fn);
/**
* if exiting an options definition block - stop saving that definition
*/
if (preg_match('/\bOPTIONS_END\b/', $line)) {
$saveOption = false;
}
if (preg_match('/\bINSTANCE_OPTIONS_MAPPING_END\b/', $line)) {
$saveInstanceMappings = false;
}
if (preg_match('/\bEXCLUDED_FROM_INSTANCE_OPTIONS_END\b/', $line)) {
$saveExcludeOptions = false;
}
/**
* if exiting an options implementation block - save the line for later
*/
if (preg_match('/\bOPTIONS_INIT_END\b/', $line)) {
$saveLine = true;
}
if (preg_match('/\bCREATE_INSTANCE_OPTIONS_END\b/', $line)) {
$saveLine = true;
}
if (preg_match('/\bUPDATE_INSTANCE_OPTIONS_END\b/', $line)) {
$saveLine = true;
}
if (preg_match('/\bUPDATE_FROM_INSTANCE_OPTIONS_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('/\bOPTIONS_INIT_START\b/', $line)) {
$saveLine = false;
}
if (preg_match('/\bCREATE_INSTANCE_OPTIONS_START\b/', $line)) {
$saveLine = false;
}
if (preg_match('/\bUPDATE_INSTANCE_OPTIONS_START\b/', $line)) {
$saveLine = false;
}
if (preg_match('/\bUPDATE_FROM_INSTANCE_OPTIONS_START\b/', $line)) {
$saveLine = false;
}
if ($saveOption) {
$line = trim($line);
$key_value = preg_split('/=/', $line);
if ($key_value === false) {
continue;
}
$options[$key_value[0]] = $key_value[1];
}
if ($saveInstanceMappings) {
$line = trim($line);
$key_value = preg_split('/=/', $line);
if ($key_value === false) {
continue;
}
$instanceMappings[$key_value[0]] = $key_value[1];
}
if ($saveExcludeOptions) {
$line = trim($line);
array_push($excludeOptions, $line);
print_r($excludeOptions);
}
/**
* If entering an options definition block - start saving that definition
*/
if (preg_match('/\bOPTIONS_START\b/', $line)) {
$saveOption = true;
}
if (preg_match('/\bINSTANCE_OPTIONS_MAPPING_START\b/', $line)) {
$saveInstanceMappings = true;
}
if (preg_match('/\bEXCLUDED_FROM_INSTANCE_OPTIONS_START\b/', $line)) {
$saveExcludeOptions = true;
}
}
fclose($fn);
file_put_contents($file, $lines);
/**
* Process the constructor options
*/
$header = file_get_contents('src/templates/options_init_header.template');
$template = file_get_contents('src/templates/options_init.template');
$newOptions = $header;
foreach ($options as $key => $value) {
$updated = str_replace('KEY', $key, $template);
$updated = str_replace('VALUE', $value, $updated);
$newOptions .= $updated;
}
$contents = file_get_contents($file);
$contents = preg_replace(
'/\/\/\bOPTIONS_INIT_START\b.*?\/\/\bOPTIONS_INIT_END\b/s',
"//OPTIONS_INIT_START\n" . $newOptions . "\t\t//OPTIONS_INIT_END",
$contents
);
file_put_contents($file, $contents);
/**
* Process createInstance options
*/
$template = file_get_contents('src/templates/create_instance_options.template');
$newOptions = [];
foreach ($options as $key => $value) {
if (in_array($key, $excludeOptions)) {
continue;
}
if (array_key_exists($key, $instanceMappings)) {
$value = $instanceMappings[$key];
} else {
$value = $key;
}
$updated = str_replace('INSTANCE_KEY', $value, $template);
$updated = str_replace('KEY', $key, $updated);
array_push($newOptions, $updated);
}
if (sizeof($newOptions) > 0) {
$contents = file_get_contents($file);
$contents = preg_replace(
'/\/\/\bCREATE_INSTANCE_OPTIONS_START\b.*?\/\/\bCREATE_INSTANCE_OPTIONS_END\b/s',
"//CREATE_INSTANCE_OPTIONS_START\n" . implode(",\n", $newOptions) . "\n\t\t\t\t//CREATE_INSTANCE_OPTIONS_END",
$contents
);
file_put_contents($file, $contents);
}
/**
* Process updateInstance options
*/
$template = file_get_contents('src/templates/update_instance_options.template');
$newOptions = '';
foreach ($options as $key => $value) {
if (in_array($key, $excludeOptions)) {
continue;
}
if (array_key_exists($key, $instanceMappings)) {
$value = $instanceMappings[$key];
} else {
$value = $key;
}
$updated = str_replace('INSTANCE_KEY', $value, $template);
$updated = str_replace('KEY', $key, $updated);
$newOptions .= $updated;
}
$contents = file_get_contents($file);
$contents = preg_replace(
'/\/\/\bUPDATE_INSTANCE_OPTIONS_START\b.*?\/\/\bUPDATE_INSTANCE_OPTIONS_END\b/s',
"//UPDATE_INSTANCE_OPTIONS_START\n" . $newOptions. "\t\t//UPDATE_INSTANCE_OPTIONS_END",
$contents
);
file_put_contents($file, $contents);
/**
* Process updateFromInstance options
*/
$template = file_get_contents('src/templates/update_from_instance_options.template');
$newOptions = '';
foreach ($options as $key => $value) {
if (in_array($key, $excludeOptions)) {
continue;
}
if (array_key_exists($key, $instanceMappings)) {
$value = $instanceMappings[$key];
} else {
$value = $key;
}
$updated = str_replace('INSTANCE_KEY', $value, $template);
$updated = str_replace('KEY', $key, $updated);
$newOptions .= $updated;
}
$contents = file_get_contents($file);
$contents = preg_replace(
'/\/\/\bUPDATE_FROM_INSTANCE_OPTIONS_START\b.*?\/\/\bUPDATE_FROM_INSTANCE_OPTIONS_END\b/s',
"//UPDATE_FROM_INSTANCE_OPTIONS_START\n" . $newOptions. "\t\t//UPDATE_FROM_INSTANCE_OPTIONS_END",
$contents
);
file_put_contents($file, $contents);
}
exit(0);
?>

View File

@ -35,6 +35,10 @@ function loadSavedData($file) {
}
while ($saved[sizeof($saved) - 1] === false && sizeof($saved) > 0) {
array_pop($saved);
}
fclose($sh);
return $saved;
@ -44,257 +48,754 @@ function restore($file, $tokens, $saved) {
$fh = fopen($file, "r");
$merged = [];
$data = [];
$restoreTokenPair = [];
$startRestoring = false;
$saveLine = true;
$restoreDepth = 0;
while (!feof($fh)) {
$line = fgets($fh);
foreach ($tokens as $startToken => $endToken) {
if (preg_match('/\b' . $startToken . '\b/', $line)) {
$startRestoring = true;
$restoreTokenPair = [$startToken, $endToken];
$restoreDepth++;
if ($restoreDepth === 1) {
$restoreTokenPair[0] = $startToken;
$restoreTokenPair[1] = $endToken;
}
break;
}
}
foreach ($tokens as $startToken => $endToken) {
if (preg_match('/\b' . $endToken . '\b/', $line)) {
$startRestoring = false;
$saveLine = false;
$restoreDepth--;
break;
}
}
if ($startRestoring) {
if ($restoreDepth === 1) {
$startToken = $restoreTokenPair[0];
$endToken = $restoreTokenPair[1];
$startIndex = getIndex($restoreTokenPair[0], $saved);
$endIndex = getIndex($restoreTokenPair[1], $saved);
$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);
$saveLine = true;
} else {
$restored = array_splice($saved, $startIndex, ($endIndex - $startIndex + 1));
$merged = array_merge($merged, $restored);
$saveLine = false;
for ($i = $startIndex; $i <= $endIndex; $i++) {
array_push($data, $saved[$i]);
}
}
if ($saveLine) {
array_push($merged, $line);
/**
* Seek to the end token in the file
*/
while (!feof($fh) && !(preg_match('/\b' . $restoreTokenPair[1] . '\b/', $line))){
$line = fgets($fh);
}
$restoreDepth--;
} else {
$saveLine = true;
array_push($data, $line);
}
}
fclose($fh);
file_put_contents($file, $merged);
file_put_contents($file, $data);
}
function install_method_templates($file)
//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)
{
$fn = fopen($file, "r");
$fn = fopen('src/templates/token.db', "r");
$saveLine = true;
$methods = [];
$saveMethod = false;
$lines = [];
$tokens = [];
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) {
$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;
$matches = [];
if (preg_match('/^(' . $type . '.*$)/', $line, $matches)) {
$tokens[$matches[1]] = [];
}
}
fclose($fn);
file_put_contents($file, $lines);
return $tokens;
}
/**
* Process the methods
*/
//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;
//
//}
if (sizeof($methods) > 0) {
/**
* @throws ErrorException
*/
function loadSaved($file, $tokens) {
$template = file_get_contents('src/templates/static_method.template');
$saveFile = $file . '.saved';
$newOptions = '';
echo "loading file " . $saveFile . "\n";
foreach ($methods as $method) {
$updated = str_replace('METHOD_NAME_UPPERCASE', strtoupper($method), $template);
$updated = str_replace('METHOD_NAME', $method, $updated);
$newOptions .= $updated;
$loadedTokens = [];
$fn = fopen($saveFile, "r");
if (!$fn) {
throw new ErrorException('Could not open save file: ' . $saveFile . "\n");
}
$currentTokenKey = null;
while (!feof($fn)) {
$line = fgets($fn);
$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;
}
}
$contents = file_get_contents($file);
if (!$tokenFound && $line != false) {
array_push($loadedTokens[$currentTokenKey], $line);
}
$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);
}
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;
}
}
$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;
}
}
}
fclose($fn);
$saveFile =$file . '.saved';
$saved = [];
$stores = [];
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);
}
}
}
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
];
}
return null;
}
}
function updateSection($file, $token, $updates, $separator = "") {
if (getType($updates) === 'array') {
$updates = implode($separator, $updates);
}
if (strlen($updates) <= 0) {
echo "No contents to be updated for token $token\n";
return;
}
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
*/
echo "Skipping token $token because it was not found\n";
return;
}
$endToken = $token . "_END";
if ($whiteSpaceObject['inline-comment']) {
$endToken = '//' . $endToken;
}
$whiteSpace = $whiteSpaceObject['white-space'];
/**
* 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
*/
// if (!preg_match('/^(\s)+/', $updates)) {
// $updates = $whiteSpace . $updates;
// }
$contents = preg_replace(
'/\b' . $token . '_START\b.*?\b' . $token . '_END\b/s',
$token . "_START\n" . $updates . $whiteSpace . $endToken,
$contents
);
file_put_contents($file, $contents);
}
function generateInitOptions($file, $tokens)
{
$token = 'CUSTOM_OPTIONS';
$store = getTokenStore($token, $tokens);
if (sizeof($store) <= 0) {
return;
}
echo "Will be building options for $token\n";
$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;
}
$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 = [];
foreach (getTokenStore('CUSTOM_INSTANCE_OPTIONS_MAPPING', $tokens) as $mapping) {
$mapping = trim($mapping);
$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;
}
}
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];
}
$updated = str_replace('METHOD_ARGS', $args, $template);
$updated = str_replace('METHOD_NAME_UPPERCASE', strtoupper($methodName), $updated);
$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";
}
}
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'
];
$saveFile = null;
if ($argv[2] == 'save') {
echo "saving file " . $file . "\n";
$tokens = getTokens('CUSTOM');
$saved = [];
$startSaving = false;
$result = save($file, $tokens);
$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;
}
}
if ($startSaving) {
array_push($saved, $line);
}
foreach ($tokens as $startToken => $endToken) {
if (preg_match('/\b' . $endToken . '\b/', $line)) {
$startSaving = false;
break;
}
}
}
fclose($fn);
file_put_contents($file . '.saved', $saved);
echo "saved file " . $file . ".saved\n";
exit(0);
} else if ($argv[2] == 'restore') {
$saved = loadSavedData($file);
$restoreTokens = getTokens('CUSTOM');
restore($file, $tokens, $saved);
$restoreTokenKeys = array_keys($restoreTokens);
/**
* 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[]
*/
$tokens = loadSaved($file, $restoreTokens);
install_method_templates($file);
foreach ($tokens as $token => $store) {
if (in_array($token, $restoreTokenKeys)) {
updateSection($file, $token, $store);
}
}
$methodTokens = [
"CUSTOM_START_METHOD_START" => "CUSTOM_START_METHOD_END",
"CUSTOM_STOP_METHOD_START" => "CUSTOM_STOP_METHOD_END"
];
generateInitOptions($file, $tokens);
restore($file, $methodTokens, $saved);
generateCreateInstanceOptions($file, $tokens);
echo "Restored file " . $file;
generateUpdateInstanceOptions($file, $tokens);
// unlink($saveFile);
} else if ($argv[2] == 'install-methods') {
generateUpdateFromInstanceOptions($file, $tokens);
install_method_templates($file);
generateMethods($file, $tokens);
echo "Installed method templates for file " . $file;
generateStaticMethods($file, $tokens);
echo `r3 update-token-db`;
$restoreTokens = getTokens('CUSTOM');
$restoreTokenKeys = array_keys($restoreTokens);
$tokens = loadSaved($file, $restoreTokens);
foreach ($tokens as $token => $store) {
if (in_array($token, $restoreTokenKeys)) {
updateSection($file, $token, $store);
}
}
exit(0);
// 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[]
// */
// $methodTokens = install_method_templates($file);
//
// restore($file, $methodTokens, $saved);
//
// echo "Restored file " . $file;
// deleteSavedFile($saveFile);
} else if ($argv[2] == 'update-methods') {
$restoreTokens = getTokens('CUSTOM');
$restoreTokenKeys = array_keys($restoreTokens);
$tokens = loadSaved($file, $restoreTokens);
foreach ($tokens as $token => $store) {
if (in_array($token, $restoreTokenKeys)) {
updateSection($file, $token, $store, "\n ");
}
}
generateMethods($file, $tokens);
generateStaticMethods($file, $tokens);
echo `r3 update-token-db`;
} else if ($argv[2] == 'update-options') {
$tokens = getTokens('CUSTOM');
$result = save($file, $tokens);
$saveFile = $result[0];
$tokens = $result[1];
try {
generateInitOptions($file, $tokens);
generateCreateInstanceOptions($file, $tokens);
generateUpdateInstanceOptions($file, $tokens);
generateUpdateFromInstanceOptions($file, $tokens);
generateMethods($file, $tokens);
generateStaticMethods($file, $tokens);
echo `r3 update-token-db`;
// 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";
}
// unlink($saveFile);
}
}