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());
     }
 }
Beispiel #2
0
// Disable XDebug var_dump() output truncation
ini_set('xdebug.var_display_max_children', -1);
ini_set('xdebug.var_display_max_data', -1);
ini_set('xdebug.var_display_max_depth', -1);
list($operations, $files) = parseArgs($argv);
/* Dump nodes by default */
if (empty($operations)) {
    $operations[] = 'dump';
}
if (empty($files)) {
    showHelp("Must specify at least one file.");
}
$parser = new PhpParser\Parser(new PhpParser\Lexer\Emulative());
$dumper = new PhpParser\NodeDumper();
$prettyPrinter = new PhpParser\PrettyPrinter\Standard();
$serializer = new PhpParser\Serializer\XML();
$traverser = new PhpParser\NodeTraverser();
$traverser->addVisitor(new PhpParser\NodeVisitor\NameResolver());
foreach ($files as $file) {
    if (strpos($file, '<?php') === 0) {
        $code = $file;
        echo "====> Code {$code}\n";
    } else {
        if (!file_exists($file)) {
            die("File {$file} does not exist.\n");
        }
        $code = file_get_contents($file);
        echo "====> File {$file}:\n";
    }
    try {
        $stmts = $parser->parse($code);