TwigNodeTryCatch.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /**
  3. * @package Grav.Common.Twig
  4. *
  5. * @copyright Copyright (C) 2015 - 2018 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\Twig\Node;
  9. class TwigNodeTryCatch extends \Twig_Node
  10. {
  11. public function __construct(
  12. \Twig_Node $try,
  13. \Twig_Node $catch = null,
  14. $lineno = 0,
  15. $tag = null
  16. )
  17. {
  18. parent::__construct(array('try' => $try, 'catch' => $catch), array(), $lineno, $tag);
  19. }
  20. /**
  21. * Compiles the node to PHP.
  22. *
  23. * @param \Twig_Compiler $compiler A Twig_Compiler instance
  24. * @throws \LogicException
  25. */
  26. public function compile(\Twig_Compiler $compiler)
  27. {
  28. $compiler->addDebugInfo($this);
  29. $compiler
  30. ->write('try {')
  31. ;
  32. $compiler
  33. ->indent()
  34. ->subcompile($this->getNode('try'))
  35. ;
  36. if ($this->hasNode('catch') && null !== $this->getNode('catch')) {
  37. $compiler
  38. ->outdent()
  39. ->write('} catch (\Exception $e) {' . "\n")
  40. ->indent()
  41. ->write('if (isset($context[\'grav\'][\'debugger\'])) $context[\'grav\'][\'debugger\']->addException($e);' . "\n")
  42. ->write('$context[\'e\'] = $e;' . "\n")
  43. ->subcompile($this->getNode('catch'))
  44. ;
  45. }
  46. $compiler
  47. ->outdent()
  48. ->write("}\n");
  49. }
  50. }