Skip to content

Instantly share code, notes, and snippets.

@nikic
Created March 24, 2014 13:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikic/9740273 to your computer and use it in GitHub Desktop.
Save nikic/9740273 to your computer and use it in GitHub Desktop.
<?php
use PhpParser\Node;
$IN_DIR = __DIR__ . '/in_dir';
$OUT_FILE = __DIR__ . '/out_file.php';
require __DIR__ . '/../lib/bootstrap.php';
class CompressVisitor extends PhpParser\NodeVisitorAbstract {
public function leaveNode(Node $node) {
if ($node instanceof Node\Stmt\Use_) {
return false;
}
}
}
$parser = new PhpParser\Parser(new PhpParser\Lexer\Emulative);
$traverser = new PhpParser\NodeTraverser;
$prettyPrinter = new PhpParser\PrettyPrinter\Standard;
$traverser->addVisitor(new PhpParser\NodeVisitor\NameResolver);
$traverser->addVisitor(new CompressVisitor);
// iterate over all .php files in the directory
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($IN_DIR));
$files = new RegexIterator($files, '/\.php$/');
$allStmts = [];
foreach ($files as $file) {
try {
$code = file_get_contents($file);
$stmts = $parser->parse($code);
$stmts = $traverser->traverse($stmts);
$allStmts = array_merge($allStmts, $stmts);
} catch (PhpParser\Error $e) {
die('Parse Error: ' . $e->getMessage());
}
}
file_put_contents($OUT_FILE, $prettyPrinter->prettyPrintFile($allStmts));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment