Escaper.php 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) 2009 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. class Twig_Extension_Escaper extends Twig_Extension
  11. {
  12. protected $autoescape;
  13. public function __construct($autoescape = true)
  14. {
  15. $this->autoescape = $autoescape;
  16. }
  17. /**
  18. * Returns the token parser instances to add to the existing list.
  19. *
  20. * @return array An array of Twig_TokenParserInterface or Twig_TokenParserBrokerInterface instances
  21. */
  22. public function getTokenParsers()
  23. {
  24. return array(new Twig_TokenParser_AutoEscape());
  25. }
  26. /**
  27. * Returns the node visitor instances to add to the existing list.
  28. *
  29. * @return array An array of Twig_NodeVisitorInterface instances
  30. */
  31. public function getNodeVisitors()
  32. {
  33. return array(new Twig_NodeVisitor_Escaper());
  34. }
  35. /**
  36. * Returns a list of filters to add to the existing list.
  37. *
  38. * @return array An array of filters
  39. */
  40. public function getFilters()
  41. {
  42. return array(
  43. 'raw' => new Twig_Filter_Function('twig_raw_filter', array('is_safe' => array('all'))),
  44. );
  45. }
  46. public function isGlobal()
  47. {
  48. return $this->autoescape;
  49. }
  50. /**
  51. * Returns the name of the extension.
  52. *
  53. * @return string The extension name
  54. */
  55. public function getName()
  56. {
  57. return 'escaper';
  58. }
  59. }
  60. /**
  61. * Marks a variable as being safe.
  62. *
  63. * @param string $string A PHP variable
  64. */
  65. function twig_raw_filter($string)
  66. {
  67. return $string;
  68. }