2011-07-12 17:58:59 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class PHPParser_NodeTraverser
|
|
|
|
{
|
2011-08-09 09:27:47 +02:00
|
|
|
/**
|
|
|
|
* @var PHPParser_NodeVisitorInterface[] Visitors
|
|
|
|
*/
|
|
|
|
protected $visitors;
|
2011-07-12 17:58:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructs a node traverser.
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
2011-08-09 09:27:47 +02:00
|
|
|
$this->visitors = array();
|
2011-07-12 17:58:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-08-09 09:27:47 +02:00
|
|
|
* Adds a visitor.
|
2011-07-12 17:58:59 +02:00
|
|
|
*
|
2011-08-09 09:27:47 +02:00
|
|
|
* @param PHPParser_NodeVisitorInterface $visitor Visitor to add
|
2011-07-12 17:58:59 +02:00
|
|
|
*/
|
2011-08-09 09:27:47 +02:00
|
|
|
public function addVisitor(PHPParser_NodeVisitorInterface $visitor) {
|
|
|
|
$this->visitors[] = $visitor;
|
2011-07-12 17:58:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Traverses a node or an array using the registered visitors.
|
|
|
|
*
|
2011-07-13 23:07:05 +02:00
|
|
|
* @param PHPParser_NodeAbstract|array $node Node or array
|
2011-07-12 17:58:59 +02:00
|
|
|
*/
|
2011-08-09 09:27:47 +02:00
|
|
|
public function traverse(&$node) {
|
|
|
|
foreach ($this->visitors as $visitor) {
|
|
|
|
$visitor->beforeTraverse($node);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->_traverse($node);
|
|
|
|
|
|
|
|
foreach ($this->visitors as $visitor) {
|
|
|
|
$visitor->afterTraverse($node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function _traverse(&$node) {
|
2011-07-12 17:58:59 +02:00
|
|
|
if ($node instanceof PHPParser_NodeAbstract) {
|
2011-08-09 09:27:47 +02:00
|
|
|
foreach ($this->visitors as $visitor) {
|
|
|
|
$visitor->enterNode($node);
|
2011-07-12 17:58:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($node) || $node instanceof Traversable) {
|
|
|
|
foreach ($node as &$subNode) {
|
2011-08-09 09:27:47 +02:00
|
|
|
$this->_traverse($subNode);
|
2011-07-12 17:58:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($node instanceof PHPParser_NodeAbstract) {
|
2011-08-09 09:27:47 +02:00
|
|
|
foreach ($this->visitors as $visitor) {
|
|
|
|
$visitor->leaveNode($node);
|
2011-07-12 17:58:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|