Mercurial > hg > isophonics-drupal-site
comparison core/lib/Drupal/Core/Template/TwigEnvironment.php @ 18:af1871eacc83
Update to Drupal core 8.7.1
author | Chris Cannam |
---|---|
date | Thu, 09 May 2019 15:33:08 +0100 |
parents | 129ea1e6d783 |
children |
comparison
equal
deleted
inserted
replaced
17:129ea1e6d783 | 18:af1871eacc83 |
---|---|
34 * | 34 * |
35 * @var array | 35 * @var array |
36 */ | 36 */ |
37 protected $templateClasses; | 37 protected $templateClasses; |
38 | 38 |
39 /** | |
40 * The template cache filename prefix. | |
41 * | |
42 * @var string | |
43 */ | |
39 protected $twigCachePrefix = ''; | 44 protected $twigCachePrefix = ''; |
40 | 45 |
41 /** | 46 /** |
42 * Constructs a TwigEnvironment object and stores cache and storage | 47 * Constructs a TwigEnvironment object and stores cache and storage |
43 * internally. | 48 * internally. |
53 * @param \Twig_LoaderInterface $loader | 58 * @param \Twig_LoaderInterface $loader |
54 * The Twig loader or loader chain. | 59 * The Twig loader or loader chain. |
55 * @param array $options | 60 * @param array $options |
56 * The options for the Twig environment. | 61 * The options for the Twig environment. |
57 */ | 62 */ |
58 public function __construct($root, CacheBackendInterface $cache, $twig_extension_hash, StateInterface $state, \Twig_LoaderInterface $loader = NULL, $options = []) { | 63 public function __construct($root, CacheBackendInterface $cache, $twig_extension_hash, StateInterface $state, \Twig_LoaderInterface $loader = NULL, array $options = []) { |
59 $this->state = $state; | 64 $this->state = $state; |
60 | 65 |
61 // Ensure that twig.engine is loaded, given that it is needed to render a | 66 // Ensure that twig.engine is loaded, given that it is needed to render a |
62 // template because functions like TwigExtension::escapeFilter() are called. | 67 // template because functions like TwigExtension::escapeFilter() are called. |
68 // @todo remove in Drupal 9.0.0 https://www.drupal.org/node/3011393. | |
63 require_once $root . '/core/themes/engines/twig/twig.engine'; | 69 require_once $root . '/core/themes/engines/twig/twig.engine'; |
64 | 70 |
65 $this->templateClasses = []; | 71 $this->templateClasses = []; |
66 | 72 |
67 $options += [ | 73 $options += [ |
70 'debug' => FALSE, | 76 'debug' => FALSE, |
71 'auto_reload' => NULL, | 77 'auto_reload' => NULL, |
72 ]; | 78 ]; |
73 // Ensure autoescaping is always on. | 79 // Ensure autoescaping is always on. |
74 $options['autoescape'] = 'html'; | 80 $options['autoescape'] = 'html'; |
75 | |
76 $policy = new TwigSandboxPolicy(); | |
77 $sandbox = new \Twig_Extension_Sandbox($policy, TRUE); | |
78 $this->addExtension($sandbox); | |
79 | |
80 if ($options['cache'] === TRUE) { | 81 if ($options['cache'] === TRUE) { |
81 $current = $state->get(static::CACHE_PREFIX_METADATA_KEY, ['twig_extension_hash' => '']); | 82 $current = $state->get(static::CACHE_PREFIX_METADATA_KEY, ['twig_extension_hash' => '']); |
82 if ($current['twig_extension_hash'] !== $twig_extension_hash || empty($current['twig_cache_prefix'])) { | 83 if ($current['twig_extension_hash'] !== $twig_extension_hash || empty($current['twig_cache_prefix'])) { |
83 $current = [ | 84 $current = [ |
84 'twig_extension_hash' => $twig_extension_hash, | 85 'twig_extension_hash' => $twig_extension_hash, |
91 $this->twigCachePrefix = $current['twig_cache_prefix']; | 92 $this->twigCachePrefix = $current['twig_cache_prefix']; |
92 | 93 |
93 $options['cache'] = new TwigPhpStorageCache($cache, $this->twigCachePrefix); | 94 $options['cache'] = new TwigPhpStorageCache($cache, $this->twigCachePrefix); |
94 } | 95 } |
95 | 96 |
96 $this->loader = $loader; | 97 $this->setLoader($loader); |
97 parent::__construct($this->loader, $options); | 98 parent::__construct($this->getLoader(), $options); |
99 $policy = new TwigSandboxPolicy(); | |
100 $sandbox = new \Twig_Extension_Sandbox($policy, TRUE); | |
101 $this->addExtension($sandbox); | |
98 } | 102 } |
99 | 103 |
100 /** | 104 /** |
101 * Invalidates all compiled Twig templates. | 105 * Invalidates all compiled Twig templates. |
102 * | 106 * |
103 * @see \drupal_flush_all_caches | 107 * @see \drupal_flush_all_caches |
104 */ | 108 */ |
105 public function invalidate() { | 109 public function invalidate() { |
106 PhpStorageFactory::get('twig')->deleteAll(); | 110 PhpStorageFactory::get('twig')->deleteAll(); |
107 $this->templateClasses = []; | 111 $this->templateClasses = []; |
108 $this->loadedTemplates = []; | |
109 $this->state->delete(static::CACHE_PREFIX_METADATA_KEY); | 112 $this->state->delete(static::CACHE_PREFIX_METADATA_KEY); |
110 } | 113 } |
111 | 114 |
112 /** | 115 /** |
113 * Get the cache prefixed used by \Drupal\Core\Template\TwigPhpStorageCache | 116 * Get the cache prefixed used by \Drupal\Core\Template\TwigPhpStorageCache |
135 // times when the same template is used more than once. For example, a page | 138 // times when the same template is used more than once. For example, a page |
136 // rendering 50 nodes without any node template overrides will use the same | 139 // rendering 50 nodes without any node template overrides will use the same |
137 // node.html.twig for the output of each node and the same compiled class. | 140 // node.html.twig for the output of each node and the same compiled class. |
138 $cache_index = $name . (NULL === $index ? '' : '_' . $index); | 141 $cache_index = $name . (NULL === $index ? '' : '_' . $index); |
139 if (!isset($this->templateClasses[$cache_index])) { | 142 if (!isset($this->templateClasses[$cache_index])) { |
140 $this->templateClasses[$cache_index] = $this->templateClassPrefix . hash('sha256', $this->loader->getCacheKey($name)) . (NULL === $index ? '' : '_' . $index); | 143 $this->templateClasses[$cache_index] = parent::getTemplateClass($name, $index); |
141 } | 144 } |
142 return $this->templateClasses[$cache_index]; | 145 return $this->templateClasses[$cache_index]; |
143 } | 146 } |
144 | 147 |
145 /** | 148 /** |
165 * @see \Drupal\Core\Template\Loader\StringLoader::exists() | 168 * @see \Drupal\Core\Template\Loader\StringLoader::exists() |
166 */ | 169 */ |
167 public function renderInline($template_string, array $context = []) { | 170 public function renderInline($template_string, array $context = []) { |
168 // Prefix all inline templates with a special comment. | 171 // Prefix all inline templates with a special comment. |
169 $template_string = '{# inline_template_start #}' . $template_string; | 172 $template_string = '{# inline_template_start #}' . $template_string; |
170 return Markup::create($this->loadTemplate($template_string, NULL)->render($context)); | 173 return Markup::create($this->createTemplate($template_string)->render($context)); |
171 } | 174 } |
172 | 175 |
173 } | 176 } |