BaseNodeVisitor.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * Twig_BaseNodeVisitor can be used to make node visitors compatible with Twig 1.x and 2.x.
  12. *
  13. * @author Fabien Potencier <fabien@symfony.com>
  14. */
  15. abstract class Twig_BaseNodeVisitor implements Twig_NodeVisitorInterface
  16. {
  17. /**
  18. * {@inheritdoc}
  19. */
  20. final public function enterNode(Twig_NodeInterface $node, Twig_Environment $env)
  21. {
  22. if (!$node instanceof Twig_Node) {
  23. throw new LogicException('Twig_BaseNodeVisitor only supports Twig_Node instances.');
  24. }
  25. return $this->doEnterNode($node, $env);
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. final public function leaveNode(Twig_NodeInterface $node, Twig_Environment $env)
  31. {
  32. if (!$node instanceof Twig_Node) {
  33. throw new LogicException('Twig_BaseNodeVisitor only supports Twig_Node instances.');
  34. }
  35. return $this->doLeaveNode($node, $env);
  36. }
  37. /**
  38. * Called before child nodes are visited.
  39. *
  40. * @param Twig_Node $node The node to visit
  41. * @param Twig_Environment $env The Twig environment instance
  42. *
  43. * @return Twig_Node The modified node
  44. */
  45. abstract protected function doEnterNode(Twig_Node $node, Twig_Environment $env);
  46. /**
  47. * Called after child nodes are visited.
  48. *
  49. * @param Twig_Node $node The node to visit
  50. * @param Twig_Environment $env The Twig environment instance
  51. *
  52. * @return Twig_Node|false The modified node or false if the node must be removed
  53. */
  54. abstract protected function doLeaveNode(Twig_Node $node, Twig_Environment $env);
  55. }