NodeTraverser.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. class PHPParser_NodeTraverser implements PHPParser_NodeTraverserInterface
  3. {
  4. /**
  5. * @var PHPParser_NodeVisitor[] Visitors
  6. */
  7. protected $visitors;
  8. /**
  9. * Constructs a node traverser.
  10. */
  11. public function __construct() {
  12. $this->visitors = array();
  13. }
  14. /**
  15. * Adds a visitor.
  16. *
  17. * @param PHPParser_NodeVisitor $visitor Visitor to add
  18. */
  19. public function addVisitor(PHPParser_NodeVisitor $visitor) {
  20. $this->visitors[] = $visitor;
  21. }
  22. /**
  23. * Traverses an array of nodes using the registered visitors.
  24. *
  25. * @param PHPParser_Node[] $nodes Array of nodes
  26. *
  27. * @return PHPParser_Node[] Traversed array of nodes
  28. */
  29. public function traverse(array $nodes) {
  30. foreach ($this->visitors as $visitor) {
  31. if (null !== $return = $visitor->beforeTraverse($nodes)) {
  32. $nodes = $return;
  33. }
  34. }
  35. $nodes = $this->traverseArray($nodes);
  36. foreach ($this->visitors as $visitor) {
  37. if (null !== $return = $visitor->afterTraverse($nodes)) {
  38. $nodes = $return;
  39. }
  40. }
  41. return $nodes;
  42. }
  43. protected function traverseNode(PHPParser_Node $node) {
  44. $node = clone $node;
  45. foreach ($node->getSubNodeNames() as $name) {
  46. $subNode =& $node->$name;
  47. if (is_array($subNode)) {
  48. $subNode = $this->traverseArray($subNode);
  49. } elseif ($subNode instanceof PHPParser_Node) {
  50. foreach ($this->visitors as $visitor) {
  51. if (null !== $return = $visitor->enterNode($subNode)) {
  52. $subNode = $return;
  53. }
  54. }
  55. $subNode = $this->traverseNode($subNode);
  56. foreach ($this->visitors as $visitor) {
  57. if (null !== $return = $visitor->leaveNode($subNode)) {
  58. $subNode = $return;
  59. }
  60. }
  61. }
  62. }
  63. return $node;
  64. }
  65. protected function traverseArray(array $nodes) {
  66. $doNodes = array();
  67. foreach ($nodes as $i => &$node) {
  68. if (is_array($node)) {
  69. $node = $this->traverseArray($node);
  70. } elseif ($node instanceof PHPParser_Node) {
  71. foreach ($this->visitors as $visitor) {
  72. if (null !== $return = $visitor->enterNode($node)) {
  73. $node = $return;
  74. }
  75. }
  76. $node = $this->traverseNode($node);
  77. foreach ($this->visitors as $visitor) {
  78. $return = $visitor->leaveNode($node);
  79. if (false === $return) {
  80. $doNodes[] = array($i, array());
  81. break;
  82. } elseif (is_array($return)) {
  83. $doNodes[] = array($i, $return);
  84. break;
  85. } elseif (null !== $return) {
  86. $node = $return;
  87. }
  88. }
  89. }
  90. }
  91. if (!empty($doNodes)) {
  92. while (list($i, $replace) = array_pop($doNodes)) {
  93. array_splice($nodes, $i, 1, $replace);
  94. }
  95. }
  96. return $nodes;
  97. }
  98. }