Skip to content

Add Dockerfile #89

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

Open
wants to merge 3 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
65 changes: 65 additions & 0 deletions getSingleFileGraph.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?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
*/

// Usage:
// maybe you should install graphviz first
// php getSingleFileGraph.php demo.php > 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], '<?php') === 0) {
return ['command line code', $argv[1]];
}

return [$argv[1], file_get_contents($argv[1])];
}

return [__FILE__, <<<'EOF'
<?php
function foo(array $a) {
$a[] = 1;
}
EOF
];
}