Yaffs site version 1.1
[yaffs-website] / vendor / twig / twig / lib / Twig / Extension / Escaper.php
1 <?php
2
3 /*
4  * This file is part of Twig.
5  *
6  * (c) Fabien Potencier
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 /**
13  * @final
14  */
15 class Twig_Extension_Escaper extends Twig_Extension
16 {
17     protected $defaultStrategy;
18
19     /**
20      * @param string|false|callable $defaultStrategy An escaping strategy
21      *
22      * @see setDefaultStrategy()
23      */
24     public function __construct($defaultStrategy = 'html')
25     {
26         $this->setDefaultStrategy($defaultStrategy);
27     }
28
29     public function getTokenParsers()
30     {
31         return array(new Twig_TokenParser_AutoEscape());
32     }
33
34     public function getNodeVisitors()
35     {
36         return array(new Twig_NodeVisitor_Escaper());
37     }
38
39     public function getFilters()
40     {
41         return array(
42             new Twig_SimpleFilter('raw', 'twig_raw_filter', array('is_safe' => array('all'))),
43         );
44     }
45
46     /**
47      * Sets the default strategy to use when not defined by the user.
48      *
49      * The strategy can be a valid PHP callback that takes the template
50      * name as an argument and returns the strategy to use.
51      *
52      * @param string|false|callable $defaultStrategy An escaping strategy
53      */
54     public function setDefaultStrategy($defaultStrategy)
55     {
56         // for BC
57         if (true === $defaultStrategy) {
58             @trigger_error('Using "true" as the default strategy is deprecated since version 1.21. Use "html" instead.', E_USER_DEPRECATED);
59
60             $defaultStrategy = 'html';
61         }
62
63         if ('filename' === $defaultStrategy) {
64             @trigger_error('Using "filename" as the default strategy is deprecated since version 1.27. Use "name" instead.', E_USER_DEPRECATED);
65
66             $defaultStrategy = 'name';
67         }
68
69         if ('name' === $defaultStrategy) {
70             $defaultStrategy = array('Twig_FileExtensionEscapingStrategy', 'guess');
71         }
72
73         $this->defaultStrategy = $defaultStrategy;
74     }
75
76     /**
77      * Gets the default strategy to use when not defined by the user.
78      *
79      * @param string $name The template name
80      *
81      * @return string|false The default strategy to use for the template
82      */
83     public function getDefaultStrategy($name)
84     {
85         // disable string callables to avoid calling a function named html or js,
86         // or any other upcoming escaping strategy
87         if (!is_string($this->defaultStrategy) && false !== $this->defaultStrategy) {
88             return call_user_func($this->defaultStrategy, $name);
89         }
90
91         return $this->defaultStrategy;
92     }
93
94     public function getName()
95     {
96         return 'escaper';
97     }
98 }
99
100 /**
101  * Marks a variable as being safe.
102  *
103  * @param string $string A PHP variable
104  *
105  * @return string
106  */
107 function twig_raw_filter($string)
108 {
109     return $string;
110 }
111
112 class_alias('Twig_Extension_Escaper', 'Twig\Extension\EscaperExtension', false);