From 36c1d90bdd1bdaf282d1497748f8909600cf6db1 Mon Sep 17 00:00:00 2001 From: z3r0yu Date: Sat, 2 Dec 2023 20:20:12 +0800 Subject: [PATCH 1/3] Add Dockerfile for php-cfg --- Dockerfile | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..a136be1 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,22 @@ +FROM php:7.4-cli + +RUN apt-get update && apt-get install -y \ + git \ + curl \ + libzip-dev \ + unzip \ + && rm -rf /var/lib/apt/lists/* + +RUN docker-php-ext-install zip + +RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer + +WORKDIR /usr/src/php-cfg + +RUN git clone https://github.com/ircmaxell/php-cfg.git . + +RUN chmod -R 777 /usr/src/php-cfg + +RUN composer install + +CMD ["/bin/bash"] \ No newline at end of file From 205d97d4c35c78e8ab00b3240c7fae8b4e1c40f2 Mon Sep 17 00:00:00 2001 From: z3r0yu Date: Sat, 2 Dec 2023 20:20:40 +0800 Subject: [PATCH 2/3] Upadte Readme for docker usage --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 3d6ba04..012f3c2 100644 --- a/README.md +++ b/README.md @@ -28,3 +28,12 @@ To dump the graph, simply use the built-in dumper: $dumper = new PHPCfg\Printer\Text(); echo $dumper->printScript($script); ``` + +Or you can use php-cfg by building the docker +```bash +# Building an image +docker build -t php-cfg . + +# Build the container using the image and go directly to bash to use the +docker run -it php-cfg +``` From 586b99af365a13f58ed09a655562963d1545029b Mon Sep 17 00:00:00 2001 From: z3r0yu Date: Sat, 2 Dec 2023 20:27:59 +0800 Subject: [PATCH 3/3] Upadte a usage example for getSingleFileGraph.php --- getSingleFileGraph.php | 65 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 getSingleFileGraph.php diff --git a/getSingleFileGraph.php b/getSingleFileGraph.php new file mode 100644 index 0000000..0203d7c --- /dev/null +++ b/getSingleFileGraph.php @@ -0,0 +1,65 @@ + demo.dot +// dot -Tpng demo.dot > demo.png + +use PhpParser\ParserFactory; + +require __DIR__.'/vendor/autoload.php'; + +$graphviz = false; +list($fileName, $code) = getCode($argc, $argv); + +$parser = new PHPCfg\Parser((new ParserFactory())->create(ParserFactory::PREFER_PHP7)); + +$declarations = new PHPCfg\Visitor\DeclarationFinder(); +$calls = new PHPCfg\Visitor\CallFinder(); +$variables = new PHPCfg\Visitor\VariableFinder(); + +$traverser = new PHPCfg\Traverser(); + +$traverser->addVisitor($declarations); +$traverser->addVisitor($calls); +$traverser->addVisitor(new PHPCfg\Visitor\Simplifier()); +$traverser->addVisitor($variables); + +$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); +} + +function getCode($argc, $argv) +{ + if ($argc >= 2) { + if (strpos($argv[1], '