Sandbox.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. * @author Fabien Potencier <fabien@symfony.com>
  14. */
  15. class Twig_Node_Sandbox extends Twig_Node
  16. {
  17. public function __construct(Twig_NodeInterface $body, $lineno, $tag = null)
  18. {
  19. parent::__construct(array('body' => $body), array(), $lineno, $tag);
  20. }
  21. public function compile(Twig_Compiler $compiler)
  22. {
  23. $compiler
  24. ->addDebugInfo($this)
  25. ->write("\$sandbox = \$this->env->getExtension('sandbox');\n")
  26. ->write("if (!\$alreadySandboxed = \$sandbox->isSandboxed()) {\n")
  27. ->indent()
  28. ->write("\$sandbox->enableSandbox();\n")
  29. ->outdent()
  30. ->write("}\n")
  31. ->subcompile($this->getNode('body'))
  32. ->write("if (!\$alreadySandboxed) {\n")
  33. ->indent()
  34. ->write("\$sandbox->disableSandbox();\n")
  35. ->outdent()
  36. ->write("}\n")
  37. ;
  38. }
  39. }