Sandbox.php 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2010 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. * Represents a sandbox node.
  12. *
  13. * @package twig
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class Twig_Node_Sandbox extends Twig_Node
  17. {
  18. public function __construct(Twig_NodeInterface $body, $lineno, $tag = null)
  19. {
  20. parent::__construct(array('body' => $body), array(), $lineno, $tag);
  21. }
  22. /**
  23. * Compiles the node to PHP.
  24. *
  25. * @param Twig_Compiler A Twig_Compiler instance
  26. */
  27. public function compile(Twig_Compiler $compiler)
  28. {
  29. $compiler
  30. ->addDebugInfo($this)
  31. ->write("\$sandbox = \$this->env->getExtension('sandbox');\n")
  32. ->write("if (!\$alreadySandboxed = \$sandbox->isSandboxed()) {\n")
  33. ->indent()
  34. ->write("\$sandbox->enableSandbox();\n")
  35. ->outdent()
  36. ->write("}\n")
  37. ->subcompile($this->getNode('body'))
  38. ->write("if (!\$alreadySandboxed) {\n")
  39. ->indent()
  40. ->write("\$sandbox->disableSandbox();\n")
  41. ->outdent()
  42. ->write("}\n")
  43. ;
  44. }
  45. }