Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[just testing] add json example #66

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
10 changes: 3 additions & 7 deletions demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
106 changes: 106 additions & 0 deletions lib/PHPCfg/Printer/Json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

declare(strict_types=1);

/**
* This file is part of PHP-CFG, a Control flow graph implementation for PHP
*
* @copyright 2015 Anthony Ferrara. All rights reserved
* @license MIT See LICENSE at the root of the project for more info
*/

namespace PHPCfg\Printer;

use Nette\Utils\FileSystem;
use PHPCfg\Func;
use PHPCfg\Printer;
use PHPCfg\Script;

class Json extends Printer
{
public function printScript(Script $script)
{
// 1. dummy approach
// $json = serialize($script);
// var_dump($json);
//
// /** @var Script $recreateScript */
// $recreateScript = unserialize($json);
// dump($recreateScript->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);

// 1. print
echo $jsonData;

// 2. save to a file
FileSystem::write('result.json', $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;
}
}
16 changes: 16 additions & 0 deletions result.json
Original file line number Diff line number Diff line change
@@ -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"
}
}