|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of PHP-CFG, a Control flow graph implementation for PHP |
| 5 | + * |
| 6 | + * @copyright 2015 Anthony Ferrara. All rights reserved |
| 7 | + * @license MIT See LICENSE at the root of the project for more info |
| 8 | + */ |
| 9 | + |
| 10 | +namespace PHPCfg\Cli; |
| 11 | + |
| 12 | +use Ahc\Cli\Output\Color; |
| 13 | +use PHPCfg\Printer; |
| 14 | + |
| 15 | +class DotCommand extends BaseCommand |
| 16 | +{ |
| 17 | + public function __construct() |
| 18 | + { |
| 19 | + parent::__construct('dot', 'Print GraphViz DOT Representation'); |
| 20 | + |
| 21 | + $help = "Todo"; |
| 22 | + |
| 23 | + $this |
| 24 | + ->argument('<file>', 'File to process') |
| 25 | + ->option('-n|--no-optimize', 'Disable Optimizers') |
| 26 | + ->option('-o|--output', 'Output File', null, '-') |
| 27 | + ; |
| 28 | + } |
| 29 | + |
| 30 | + public function execute($file, $optimize, $output) |
| 31 | + { |
| 32 | + $io = $this->app()->io(); |
| 33 | + $color = new Color(); |
| 34 | + |
| 35 | + if (file_exists($file)) { |
| 36 | + $code = file_get_contents($file); |
| 37 | + } else { |
| 38 | + $io->write($color->error("Unknown file $file")); |
| 39 | + return 1; |
| 40 | + } |
| 41 | + |
| 42 | + $script = $this->exec($file, $code, $optimize); |
| 43 | + |
| 44 | + $dumper = new Printer\GraphViz(); |
| 45 | + $result = $dumper->printScript($script); |
| 46 | + if ($output === '-') { |
| 47 | + $io->write($result); |
| 48 | + } else { |
| 49 | + file_put_contents($output, $result); |
| 50 | + $io->write("Saved to {$output}", true); |
| 51 | + } |
| 52 | + } |
| 53 | +} |
0 commit comments