Skip to content

Commit

Permalink
Make parser work with v2
Browse files Browse the repository at this point in the history
  • Loading branch information
gossi committed Nov 16, 2016
1 parent 77ceeef commit abe060a
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/parser/FileParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
use phootwork\collection\Set;
use phootwork\file\exception\FileNotFoundException;
use phootwork\file\File;
use PhpParser\Lexer\Emulative;
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
use PhpParser\Parser;

class FileParser extends NodeVisitorAbstract {

Expand Down Expand Up @@ -47,12 +45,21 @@ public function parse() {
throw new FileNotFoundException(sprintf('File (%s) does not exist.', $this->filename));
}

$parser = new Parser(new Emulative());
$parser = $this->getParser();
$traverser = new NodeTraverser();
$traverser->addVisitor($this);
$traverser->traverse($parser->parse($file->read()));
}

private function getParser() {
if (class_exists('\\PhpParser\\ParserFactory')) {
$factory = new \PhpParser\ParserFactory();
return $factory->create(\PhpParser\ParserFactory::PREFER_PHP7);
} else {
return new \PhpParser\Parser(new \PhpParser\Lexer\Emulative());
}
}

public function enterNode(Node $node) {
foreach ($this->visitors as $visitor) {
switch ($node->getType()) {
Expand Down

0 comments on commit abe060a

Please sign in to comment.