SandboxedModule.php 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2009 Fabien Potencier
  6. * (c) 2009 Armin Ronacher
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. /**
  12. * Represents a module node.
  13. *
  14. * @package twig
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. class Twig_Node_SandboxedModule extends Twig_Node_Module
  18. {
  19. protected $usedFilters;
  20. protected $usedTags;
  21. protected $usedFunctions;
  22. public function __construct(Twig_Node_Module $node, array $usedFilters, array $usedTags, array $usedFunctions)
  23. {
  24. parent::__construct($node->getNode('body'), $node->getNode('parent'), $node->getNode('blocks'), $node->getNode('macros'), $node->getNode('traits'), $node->getAttribute('embedded_templates'), $node->getAttribute('filename'), $node->getLine(), $node->getNodeTag());
  25. $this->setAttribute('index', $node->getAttribute('index'));
  26. $this->usedFilters = $usedFilters;
  27. $this->usedTags = $usedTags;
  28. $this->usedFunctions = $usedFunctions;
  29. }
  30. protected function compileDisplayBody(Twig_Compiler $compiler)
  31. {
  32. $compiler->write("\$this->checkSecurity();\n");
  33. parent::compileDisplayBody($compiler);
  34. }
  35. protected function compileDisplayFooter(Twig_Compiler $compiler)
  36. {
  37. parent::compileDisplayFooter($compiler);
  38. $compiler
  39. ->write("protected function checkSecurity() {\n")
  40. ->indent()
  41. ->write("\$this->env->getExtension('sandbox')->checkSecurity(\n")
  42. ->indent()
  43. ->write(!$this->usedTags ? "array(),\n" : "array('".implode('\', \'', $this->usedTags)."'),\n")
  44. ->write(!$this->usedFilters ? "array(),\n" : "array('".implode('\', \'', $this->usedFilters)."'),\n")
  45. ->write(!$this->usedFunctions ? "array()\n" : "array('".implode('\', \'', $this->usedFunctions)."')\n")
  46. ->outdent()
  47. ->write(");\n")
  48. ->outdent()
  49. ->write("}\n\n")
  50. ;
  51. }
  52. }