public function filter() { $sourceFileContent = trim(file_get_contents($this->sourceFile)); //first, remove all comments in file content $sourceFileContent = $this->removeAllComments($sourceFileContent); $sourceFileContent = $this->putBracketsInNewLine($sourceFileContent); $sourceFileContent = $this->removeBlankLines($sourceFileContent); $parser = new \PhpParser\Parser(new \PhpParser\Lexer()); $serializer = new \PhpParser\Serializer\XML(); try { //load all converters, order is very important $converterClasses = array("\\PHPtoCExt\\Converter\\TraitMergingConverter", "\\PHPtoCExt\\Converter\\ForLoopToWhileLoopConverter", "\\PHPtoCExt\\Converter\\PrintToEchoConverter", "\\PHPtoCExt\\Converter\\ModuloCastingConverter", "\\PHPtoCExt\\Converter\\IssetToNotEmptyConverter", "\\PHPtoCExt\\Converter\\ClassHierarchyFlatterningConverter", "\\PHPtoCExt\\Converter\\StaticVarAutoDefineConverter", "\\PHPtoCExt\\Converter\\SelfStaticConverter", "\\PHPtoCExt\\Converter\\CodeReformatConverter", "\\PHPtoCExt\\Converter\\CFunctionAutoConverter", "\\PHPtoCExt\\Converter\\CFunctionCallConverter"); $searches = array(); $replaces = array(); $postSearches = array(); $postReplaces = array(); //go through all converters to convert the source code foreach ($converterClasses as $converterClass) { $stmts = $parser->parse($sourceFileContent); $codeLines = explode("\n", $sourceFileContent); $codeASTXML = $serializer->serialize($stmts); $codeASTXMLLines = explode("\n", $codeASTXML); $this->codeLines = $codeLines; $this->codeASTXMLLines = $codeASTXMLLines; $this->converter = new $converterClass($codeLines, $codeASTXMLLines, $this->inputDir); $this->converter->convert(); $searches = $this->converter->getSearches(); $replaces = $this->converter->getReplaces(); $sourceFileContent = str_replace($searches, $replaces, $sourceFileContent); $postSearches = array_merge($postSearches, $this->converter->getPostSearches()); $postReplaces = array_merge($postReplaces, $this->converter->getPostReplaces()); } file_put_contents($this->targetFile, $sourceFileContent); //add post searches and replaces $this->postSearches = $postSearches; $this->postReplaces = $postReplaces; } catch (\PhpParser\Error $e) { throw new PHPtoCExtException("PHP Parser Error: " . $e->getMessage()); } }
} try { $stmts = $parser->parse($code); } catch (PhpParser\Error $e) { die("==> Parse Error: {$e->getMessage()}\n"); } foreach ($operations as $operation) { if ('dump' === $operation) { echo "==> Node dump:\n"; echo $dumper->dump($stmts), "\n"; } elseif ('pretty-print' === $operation) { echo "==> Pretty print:\n"; echo $prettyPrinter->prettyPrintFile($stmts), "\n"; } elseif ('serialize-xml' === $operation) { echo "==> Serialized XML:\n"; echo $serializer->serialize($stmts), "\n"; } elseif ('var-dump' === $operation) { echo "==> var_dump():\n"; var_dump($stmts); } elseif ('resolve-names' === $operation) { echo "==> Resolved names.\n"; $stmts = $traverser->traverse($stmts); } } } function showHelp($error) { die($error . "\n\n" . <<<OUTPUT Usage: php php-parse.php [operations] file1.php [file2.php ...]