comparison core/lib/Drupal/Core/Template/TwigEnvironment.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents 4c8ae668cc8c
children af1871eacc83
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
1 <?php 1 <?php
2 2
3 namespace Drupal\Core\Template; 3 namespace Drupal\Core\Template;
4 4
5 use Drupal\Core\Cache\CacheBackendInterface; 5 use Drupal\Core\Cache\CacheBackendInterface;
6 use Drupal\Core\PhpStorage\PhpStorageFactory;
6 use Drupal\Core\Render\Markup; 7 use Drupal\Core\Render\Markup;
7 use Drupal\Core\State\StateInterface; 8 use Drupal\Core\State\StateInterface;
8 9
9 /** 10 /**
10 * A class that defines a Twig environment for Drupal. 11 * A class that defines a Twig environment for Drupal.
13 * and are used to load templates from the file system or other locations. 14 * and are used to load templates from the file system or other locations.
14 * 15 *
15 * @see core\vendor\twig\twig\lib\Twig\Environment.php 16 * @see core\vendor\twig\twig\lib\Twig\Environment.php
16 */ 17 */
17 class TwigEnvironment extends \Twig_Environment { 18 class TwigEnvironment extends \Twig_Environment {
19
20 /**
21 * Key name of the Twig cache prefix metadata key-value pair in State.
22 */
23 const CACHE_PREFIX_METADATA_KEY = 'twig_extension_hash_prefix';
24
25 /**
26 * The state service.
27 *
28 * @var \Drupal\Core\State\StateInterface
29 */
30 protected $state;
18 31
19 /** 32 /**
20 * Static cache of template classes. 33 * Static cache of template classes.
21 * 34 *
22 * @var array 35 * @var array
41 * The Twig loader or loader chain. 54 * The Twig loader or loader chain.
42 * @param array $options 55 * @param array $options
43 * The options for the Twig environment. 56 * The options for the Twig environment.
44 */ 57 */
45 public function __construct($root, CacheBackendInterface $cache, $twig_extension_hash, StateInterface $state, \Twig_LoaderInterface $loader = NULL, $options = []) { 58 public function __construct($root, CacheBackendInterface $cache, $twig_extension_hash, StateInterface $state, \Twig_LoaderInterface $loader = NULL, $options = []) {
59 $this->state = $state;
60
46 // Ensure that twig.engine is loaded, given that it is needed to render a 61 // Ensure that twig.engine is loaded, given that it is needed to render a
47 // template because functions like TwigExtension::escapeFilter() are called. 62 // template because functions like TwigExtension::escapeFilter() are called.
48 require_once $root . '/core/themes/engines/twig/twig.engine'; 63 require_once $root . '/core/themes/engines/twig/twig.engine';
49 64
50 $this->templateClasses = []; 65 $this->templateClasses = [];
61 $policy = new TwigSandboxPolicy(); 76 $policy = new TwigSandboxPolicy();
62 $sandbox = new \Twig_Extension_Sandbox($policy, TRUE); 77 $sandbox = new \Twig_Extension_Sandbox($policy, TRUE);
63 $this->addExtension($sandbox); 78 $this->addExtension($sandbox);
64 79
65 if ($options['cache'] === TRUE) { 80 if ($options['cache'] === TRUE) {
66 $current = $state->get('twig_extension_hash_prefix', ['twig_extension_hash' => '']); 81 $current = $state->get(static::CACHE_PREFIX_METADATA_KEY, ['twig_extension_hash' => '']);
67 if ($current['twig_extension_hash'] !== $twig_extension_hash || empty($current['twig_cache_prefix'])) { 82 if ($current['twig_extension_hash'] !== $twig_extension_hash || empty($current['twig_cache_prefix'])) {
68 $current = [ 83 $current = [
69 'twig_extension_hash' => $twig_extension_hash, 84 'twig_extension_hash' => $twig_extension_hash,
70 // Generate a new prefix which invalidates any existing cached files. 85 // Generate a new prefix which invalidates any existing cached files.
71 'twig_cache_prefix' => uniqid(), 86 'twig_cache_prefix' => uniqid(),
72 87
73 ]; 88 ];
74 $state->set('twig_extension_hash_prefix', $current); 89 $state->set(static::CACHE_PREFIX_METADATA_KEY, $current);
75 } 90 }
76 $this->twigCachePrefix = $current['twig_cache_prefix']; 91 $this->twigCachePrefix = $current['twig_cache_prefix'];
77 92
78 $options['cache'] = new TwigPhpStorageCache($cache, $this->twigCachePrefix); 93 $options['cache'] = new TwigPhpStorageCache($cache, $this->twigCachePrefix);
79 } 94 }
80 95
81 $this->loader = $loader; 96 $this->loader = $loader;
82 parent::__construct($this->loader, $options); 97 parent::__construct($this->loader, $options);
98 }
99
100 /**
101 * Invalidates all compiled Twig templates.
102 *
103 * @see \drupal_flush_all_caches
104 */
105 public function invalidate() {
106 PhpStorageFactory::get('twig')->deleteAll();
107 $this->templateClasses = [];
108 $this->loadedTemplates = [];
109 $this->state->delete(static::CACHE_PREFIX_METADATA_KEY);
83 } 110 }
84 111
85 /** 112 /**
86 * Get the cache prefixed used by \Drupal\Core\Template\TwigPhpStorageCache 113 * Get the cache prefixed used by \Drupal\Core\Template\TwigPhpStorageCache
87 * 114 *