-
Notifications
You must be signed in to change notification settings - Fork 611
/
Environment.php
66 lines (60 loc) · 1.78 KB
/
Environment.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\web\twig;
use Craft;
use Twig\Environment as TwigEnvironment;
use Twig\Extension\EscaperExtension;
use Twig\Loader\LoaderInterface;
use Twig\Source;
/**
* Base Twig template class.
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0.0
*/
class Environment extends TwigEnvironment
{
/**
* @inheritdoc
*/
public function __construct(LoaderInterface $loader, array $options = [])
{
parent::__construct($loader, $options);
$this->setDefaultEscaperStrategy();
}
/**
* @inheritdoc
*/
public function compileSource(Source $source): string
{
Craft::beginProfile($source->getName(), __METHOD__);
$result = parent::compileSource($source);
Craft::endProfile($source->getName(), __METHOD__);
return $result;
}
/**
* @param mixed $strategy The escaper strategy to set. If null, it will be determined based on the template name.
*/
public function setDefaultEscaperStrategy(mixed $strategy = null): void
{
// don't have Twig escape HTML by default
/** @var EscaperExtension $ext */
$ext = $this->getExtension(EscaperExtension::class);
$ext->setDefaultStrategy($strategy ?? [$this, 'getDefaultEscaperStrategy']);
}
/**
* Returns the default escaper strategy to use based on the template name.
*
* @param string $name
* @return string|false
*/
public function getDefaultEscaperStrategy(string $name): string|false
{
$ext = strtolower(pathinfo($name, PATHINFO_EXTENSION));
return in_array($ext, ['txt', 'text'], true) ? false : 'html';
}
}