Created
March 24, 2014 13:41
-
-
Save nikic/9740273 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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