Project

General

Profile

1
<?php
2

    
3
/*
4
 * This file is part of Twig.
5
 *
6
 * (c) 2015 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
 * Default autoescaping strategy based on file names.
14
 *
15
 * This strategy sets the HTML as the default autoescaping strategy,
16
 * but changes it based on the filename.
17
 *
18
 * Note that there is no runtime performance impact as the
19
 * default autoescaping strategy is set at compilation time.
20
 *
21
 * @author Fabien Potencier <fabien@symfony.com>
22
 */
23
class Twig_FileExtensionEscapingStrategy
24
{
25
    /**
26
     * Guesses the best autoescaping strategy based on the file name.
27
     *
28
     * @param string $filename The template file name
29
     *
30
     * @return string|false The escaping strategy name to use or false to disable
31
     */
32
    public static function guess($filename)
33
    {
34
        if (in_array(substr($filename, -1), array('/', '\\'))) {
35
            return 'html'; // return html for directories
36
        }
37

    
38
        if ('.twig' === substr($filename, -5)) {
39
            $filename = substr($filename, 0, -5);
40
        }
41

    
42
        $extension = pathinfo($filename, PATHINFO_EXTENSION);
43

    
44
        switch ($extension) {
45
            case 'js':
46
                return 'js';
47

    
48
            case 'css':
49
                return 'css';
50

    
51
            case 'txt':
52
                return false;
53

    
54
            default:
55
                return 'html';
56
        }
57
    }
58
}
(12-12/43)