From 4caeb0cb17f2e8c508617d06583b6e84070138f8 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Wed, 22 Apr 2020 13:35:19 +0200 Subject: [PATCH 1/2] add json example --- composer.json | 4 +- demo.php | 10 ++-- lib/PHPCfg/Printer/Json.php | 100 ++++++++++++++++++++++++++++++++++++ 3 files changed, 106 insertions(+), 8 deletions(-) create mode 100755 lib/PHPCfg/Printer/Json.php diff --git a/composer.json b/composer.json index 5fb47bb..89aba31 100755 --- a/composer.json +++ b/composer.json @@ -15,7 +15,9 @@ }, "require-dev": { "phpunit/phpunit": "^7.0", - "friendsofphp/php-cs-fixer": "*" + "friendsofphp/php-cs-fixer": "*", + "tracy/tracy": "^2.7", + "nette/utils": "^3.1" }, "autoload": { "psr-4": { diff --git a/demo.php b/demo.php index efe9f4b..e3b09ad 100755 --- a/demo.php +++ b/demo.php @@ -32,13 +32,9 @@ $script = $parser->parse($code, __FILE__); $traverser->traverse($script); -if ($graphviz) { - $dumper = new PHPCfg\Printer\GraphViz(); - echo $dumper->printScript($script); -} else { - $dumper = new PHPCfg\Printer\Text(); - echo $dumper->printScript($script); -} +$dumper = new PHPCfg\Printer\Json(); +//$dumper = new PHPCfg\Printer\Text(); +$data = $dumper->printScript($script); function getCode($argc, $argv) { diff --git a/lib/PHPCfg/Printer/Json.php b/lib/PHPCfg/Printer/Json.php new file mode 100755 index 0000000..4b36d55 --- /dev/null +++ b/lib/PHPCfg/Printer/Json.php @@ -0,0 +1,100 @@ +main->cfg); +// die; + + $output = []; + $output[] = $this->printFunc($script->main); + foreach ($script->functions as $func) { + $name = $func->getScopedName(); + $output[] = "\nFunction ${name}():"; + $output[] = ' ' . $this->renderType($func->returnType); + $output[] = $this->printFunc($func); + $output['label'] = $this->printFunc($func); + } + + // before (strings) + // $output[] = 'key: value'; + + // after (array) + // $output['key'] = 'value'; + + // array data + $jsonData = \Nette\Utils\Json::encode($output, \Nette\Utils\Json::PRETTY); + echo $jsonData; + } + + public function printFunc(Func $func): array + { + $rendered = $this->render($func); + $output = []; + foreach ($rendered['blocks'] as $block) { + $ops = $rendered['blocks'][$block]; + $output[] = "Block#".$rendered['blockIds'][$block]; + foreach ($block->parents as $prev) { + if ($rendered['blockIds']->contains($prev)) { + $output['Parent'] = "Block#".$rendered['blockIds'][$prev]; + } + } + foreach ($ops as $op) { + $output['label'] = $op['label']; + foreach ($op['childBlocks'] as $child) { + $output[] = $child['name'].': Block#'.$rendered['blockIds'][$child['block']]; + } + } + } + + return $output; + } + + public function printVars(Func $func) + { + $rendered = $this->render($func); + $output = ''; + foreach ($rendered['varIds'] as $var) { + $id = $rendered['varIds'][$var]; + $output[] = "\nVar#${id}"; + $output[] = $this->indent("\n".'WriteOps:'); + foreach ($var->ops as $writeOp) { + if ($rendered['ops']->contains($writeOp)) { + $output[] = $rendered['ops'][$writeOp]['label']; + } + } + + $output[] = 'ReadOps:'; + foreach ($var->usages as $usage) { + if ($rendered['ops']->contains($usage)) { + $output[] = $this->indent("\n".$rendered['ops'][$usage]['label'], 2); + } + } + $output[] = "\n"; + } + + return $output; + } +} From 75a7b7ae3d8fe88d903c2a647b77c2f6c6627393 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Wed, 22 Apr 2020 13:37:48 +0200 Subject: [PATCH 2/2] add file save --- lib/PHPCfg/Printer/Json.php | 6 ++++++ result.json | 16 ++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 result.json diff --git a/lib/PHPCfg/Printer/Json.php b/lib/PHPCfg/Printer/Json.php index 4b36d55..92b0d2f 100755 --- a/lib/PHPCfg/Printer/Json.php +++ b/lib/PHPCfg/Printer/Json.php @@ -11,6 +11,7 @@ namespace PHPCfg\Printer; +use Nette\Utils\FileSystem; use PHPCfg\Func; use PHPCfg\Printer; use PHPCfg\Script; @@ -46,7 +47,12 @@ public function printScript(Script $script) // array data $jsonData = \Nette\Utils\Json::encode($output, \Nette\Utils\Json::PRETTY); + + // 1. print echo $jsonData; + + // 2. save to a file + FileSystem::write('result.json', $jsonData); } public function printFunc(Func $func): array diff --git a/result.json b/result.json new file mode 100644 index 0000000..74559db --- /dev/null +++ b/result.json @@ -0,0 +1,16 @@ +{ + "0": { + "0": "Block#1", + "label": "Terminal_Return" + }, + "1": "\nFunction foo():", + "2": " mixed", + "3": { + "0": "Block#1", + "label": "Terminal_Return" + }, + "label": { + "0": "Block#1", + "label": "Terminal_Return" + } +} \ No newline at end of file