| 1 |
<?php |
| 2 |
/** |
| 3 |
* This file is part of the Peast package |
| 4 |
* |
| 5 |
* (c) Marco Marchiò <[email protected]> |
| 6 |
* |
| 7 |
* For the full copyright and license information refer to the LICENSE file |
| 8 |
* distributed with this source code |
| 9 |
*/ |
| 10 |
namespace Peast; |
| 11 |
|
| 12 |
/** |
| 13 |
* Nodes traverser class |
| 14 |
* |
| 15 |
* @author Marco Marchiò <[email protected]> |
| 16 |
*/ |
| 17 |
class Traverser |
| 18 |
{ |
| 19 |
/** |
| 20 |
* If a function return this value, the current node will be removed |
| 21 |
*/ |
| 22 |
const REMOVE_NODE = 1; |
| 23 |
|
| 24 |
/** |
| 25 |
* If a function return this value, the current node's children won't be |
| 26 |
* traversed |
| 27 |
*/ |
| 28 |
const DONT_TRAVERSE_CHILD_NODES = 2; |
| 29 |
|
| 30 |
/** |
| 31 |
* If a function return this value, the traverser will stop running |
| 32 |
*/ |
| 33 |
const STOP_TRAVERSING = 4; |
| 34 |
|
| 35 |
/** |
| 36 |
* Array of functions to call on each node |
| 37 |
* |
| 38 |
* @var array |
| 39 |
*/ |
| 40 |
protected $functions = array(); |
| 41 |
|
| 42 |
/** |
| 43 |
* Pass parent node flag |
| 44 |
* |
| 45 |
* @var bool |
| 46 |
*/ |
| 47 |
protected $passParentNode = false; |
| 48 |
|
| 49 |
/** |
| 50 |
* Skip starting node flag |
| 51 |
* |
| 52 |
* @var bool |
| 53 |
*/ |
| 54 |
protected $skipStartingNode = false; |
| 55 |
|
| 56 |
/** |
| 57 |
* Class constructor. Available options are: |
| 58 |
* - skipStartingNode: if true the starting node will be skipped |
| 59 |
* - passParentNode: if true the parent node of each node will be |
| 60 |
* passed as second argument when the functions are called. Note |
| 61 |
* that the parent node is calculated during traversing, so for |
| 62 |
* the starting node it will always be null. |
| 63 |
* |
| 64 |
* @param array $options Options array |
| 65 |
*/ |
| 66 |
public function __construct($options = array()) |
| 67 |
{ |
| 68 |
if (isset($options["passParentNode"])) { |
| 69 |
$this->passParentNode = (bool) $options["passParentNode"]; |
| 70 |
} |
| 71 |
if (isset($options["skipStartingNode"])) { |
| 72 |
$this->skipStartingNode = (bool) $options["skipStartingNode"]; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Adds a function that will be called for each node in the tree. The |
| 78 |
* function will receive the current node as argument. The action that will |
| 79 |
* be executed on the node by the traverser depends on the returned value |
| 80 |
* of the function: |
| 81 |
* - a node: it will replace the node with the returned one |
| 82 |
* - a numeric value that is a combination of the constants defined in this |
| 83 |
* class: it will execute the function related to each constant |
| 84 |
* - an array where the first element is a node and the second element is a |
| 85 |
* numeric value that is a combination of the constants defined in this |
| 86 |
* class: it will replace the node with the returned one and it will |
| 87 |
* execute the function related to each constant (REMOVE_NODE will be |
| 88 |
* ignored since it does not make any sense in this case) |
| 89 |
* - other: nothing |
| 90 |
* |
| 91 |
* @param callable $fn Function to add |
| 92 |
* |
| 93 |
* @return $this |
| 94 |
*/ |
| 95 |
public function addFunction(callable $fn) |
| 96 |
{ |
| 97 |
$this->functions[] = $fn; |
| 98 |
return $this; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Starts the traversing |
| 103 |
* |
| 104 |
* @param Syntax\Node\Node $node Starting node |
| 105 |
* |
| 106 |
* @return Syntax\Node\Node |
| 107 |
*/ |
| 108 |
public function traverse(Syntax\Node\Node $node) |
| 109 |
{ |
| 110 |
if ($this->skipStartingNode) { |
| 111 |
$this->traverseChildren($node); |
| 112 |
} else { |
| 113 |
$this->execFunctions($node); |
| 114 |
} |
| 115 |
return $node; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Executes all functions on the given node and, if required, starts |
| 120 |
* traversing its children. The returned value is an array where the first |
| 121 |
* value is the node or null if it has been removed and the second value is |
| 122 |
* a boolean indicating if the traverser must continue the traversing or not |
| 123 |
* |
| 124 |
* @param Syntax\Node\Node $node Node |
| 125 |
* @param Syntax\Node\Node|null $parent Parent node |
| 126 |
* |
| 127 |
* @return array |
| 128 |
*/ |
| 129 |
protected function execFunctions($node, $parent = null) |
| 130 |
{ |
| 131 |
$traverseChildren = true; |
| 132 |
$continueTraversing = true; |
| 133 |
|
| 134 |
foreach ($this->functions as $fn) { |
| 135 |
$ret = $this->passParentNode ? $fn($node, $parent) : $fn($node); |
| 136 |
if ($ret) { |
| 137 |
if (is_array($ret) && $ret[0] instanceof Syntax\Node\Node) { |
| 138 |
$node = $ret[0]; |
| 139 |
if (isset($ret[1]) && is_numeric($ret[1])) { |
| 140 |
if ($ret[1] & self::DONT_TRAVERSE_CHILD_NODES) { |
| 141 |
$traverseChildren = false; |
| 142 |
} |
| 143 |
if ($ret[1] & self::STOP_TRAVERSING) { |
| 144 |
$continueTraversing = false; |
| 145 |
} |
| 146 |
} |
| 147 |
} elseif ($ret instanceof Syntax\Node\Node) { |
| 148 |
$node = $ret; |
| 149 |
} elseif (is_numeric($ret)) { |
| 150 |
if ($ret & self::DONT_TRAVERSE_CHILD_NODES) { |
| 151 |
$traverseChildren = false; |
| 152 |
} |
| 153 |
if ($ret & self::STOP_TRAVERSING) { |
| 154 |
$continueTraversing = false; |
| 155 |
} |
| 156 |
if ($ret & self::REMOVE_NODE) { |
| 157 |
$node = null; |
| 158 |
$traverseChildren = false; |
| 159 |
break; |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
if ($traverseChildren && $continueTraversing) { |
| 166 |
$continueTraversing = $this->traverseChildren($node); |
| 167 |
} |
| 168 |
|
| 169 |
return array($node, $continueTraversing); |
| 170 |
} |
| 171 |
|
| 172 |
/** |
| 173 |
* Traverses node children. It returns a boolean indicating if the |
| 174 |
* traversing must continue or not |
| 175 |
* |
| 176 |
* @param Syntax\Node\Node $node Node |
| 177 |
* |
| 178 |
* @return bool |
| 179 |
*/ |
| 180 |
protected function traverseChildren(Syntax\Node\Node $node) |
| 181 |
{ |
| 182 |
$continue = true; |
| 183 |
|
| 184 |
foreach (Syntax\Utils::getNodeProperties($node, true) as $prop) { |
| 185 |
$getter = $prop["getter"]; |
| 186 |
$setter = $prop["setter"]; |
| 187 |
$child = $node->$getter(); |
| 188 |
if (!$child) { |
| 189 |
continue; |
| 190 |
} elseif (is_array($child)) { |
| 191 |
$newChildren = array(); |
| 192 |
foreach ($child as $c) { |
| 193 |
if (!$c || !$continue) { |
| 194 |
$newChildren[] = $c; |
| 195 |
} else { |
| 196 |
list($c, $continue) = $this->execFunctions($c, $node); |
| 197 |
if ($c) { |
| 198 |
$newChildren[] = $c; |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
$node->$setter($newChildren); |
| 203 |
} else { |
| 204 |
list($child, $continue) = $this->execFunctions($child, $node); |
| 205 |
$node->$setter($child); |
| 206 |
} |
| 207 |
|
| 208 |
if (!$continue) { |
| 209 |
break; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
return $continue; |
| 214 |
} |
| 215 |
} |