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-09-21 21:43:19 +02:00
|
|
|
* @param array|PHPParser_Node $node Node or array
|
2011-07-12 17:58:59 +02:00
|
|
|
*/
|
2011-08-09 09:27:47 +02:00
|
|
|
public function traverse(&$node) {
|
2011-09-21 21:43:19 +02:00
|
|
|
if (!is_array($node) && !$node instanceof PHPParser_Node) {
|
2011-08-31 21:19:31 +02:00
|
|
|
throw new InvalidArgumentException('Can only traverse nodes and arrays');
|
|
|
|
}
|
|
|
|
|
2011-08-09 09:27:47 +02:00
|
|
|
foreach ($this->visitors as $visitor) {
|
|
|
|
$visitor->beforeTraverse($node);
|
|
|
|
}
|
|
|
|
|
2011-08-20 16:16:59 +02:00
|
|
|
$this->_traverse($node, $this->visitors);
|
2011-08-09 09:27:47 +02:00
|
|
|
|
|
|
|
foreach ($this->visitors as $visitor) {
|
|
|
|
$visitor->afterTraverse($node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-20 16:16:59 +02:00
|
|
|
protected function _traverse(&$node, array $visitors) {
|
2011-08-19 17:19:27 +02:00
|
|
|
$doNodes = array();
|
2011-08-09 12:32:18 +02:00
|
|
|
|
|
|
|
foreach ($node as $subNodeKey => &$subNode) {
|
2011-08-31 21:19:31 +02:00
|
|
|
if (is_array($subNode)) {
|
|
|
|
$this->_traverse($subNode, $visitors);
|
2011-09-21 21:43:19 +02:00
|
|
|
} elseif ($subNode instanceof PHPParser_Node) {
|
2011-08-20 16:16:59 +02:00
|
|
|
foreach ($visitors as $visitor) {
|
2011-08-09 12:32:18 +02:00
|
|
|
$visitor->enterNode($subNode);
|
|
|
|
}
|
|
|
|
|
2011-08-31 21:19:31 +02:00
|
|
|
$this->_traverse($subNode, $visitors);
|
2011-08-09 12:32:18 +02:00
|
|
|
|
2011-08-20 16:16:59 +02:00
|
|
|
foreach ($visitors as $i => $visitor) {
|
2011-08-09 12:32:18 +02:00
|
|
|
$return = $visitor->leaveNode($subNode);
|
|
|
|
|
|
|
|
if (false === $return) {
|
2011-08-19 17:19:27 +02:00
|
|
|
$doNodes[] = array($subNodeKey, array());
|
|
|
|
break;
|
2011-08-09 12:32:18 +02:00
|
|
|
} elseif (is_array($return)) {
|
2011-08-20 16:16:59 +02:00
|
|
|
// traverse replacement nodes using all visitors apart from the one that
|
|
|
|
// did the change
|
|
|
|
$subNodeVisitors = $visitors;
|
|
|
|
unset($subNodeVisitors[$i]);
|
|
|
|
$this->_traverse($return, $subNodeVisitors);
|
|
|
|
|
2011-08-19 17:19:27 +02:00
|
|
|
$doNodes[] = array($subNodeKey, $return);
|
|
|
|
break;
|
2011-08-09 12:32:18 +02:00
|
|
|
}
|
|
|
|
}
|
2011-07-12 17:58:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-19 17:19:27 +02:00
|
|
|
if (!empty($doNodes)) {
|
2011-08-31 21:19:31 +02:00
|
|
|
if (!is_array($node)) {
|
|
|
|
throw new Exception('Nodes can only be merged if the parent is an array');
|
|
|
|
}
|
2011-07-12 17:58:59 +02:00
|
|
|
|
2011-08-31 21:19:31 +02:00
|
|
|
while (list($key, $replace) = array_pop($doNodes)) {
|
|
|
|
array_splice($node, $key, 1, $replace);
|
2011-07-12 17:58:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|