annotate core/lib/Drupal/Core/Template/TwigEnvironment.php @ 9:1fc0ff908d1f

Add another data file
author Chris Cannam
date Mon, 05 Feb 2018 12:34:32 +0000
parents 4c8ae668cc8c
children 129ea1e6d783
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\Template;
Chris@0 4
Chris@0 5 use Drupal\Core\Cache\CacheBackendInterface;
Chris@0 6 use Drupal\Core\Render\Markup;
Chris@0 7 use Drupal\Core\State\StateInterface;
Chris@0 8
Chris@0 9 /**
Chris@0 10 * A class that defines a Twig environment for Drupal.
Chris@0 11 *
Chris@0 12 * Instances of this class are used to store the configuration and extensions,
Chris@0 13 * and are used to load templates from the file system or other locations.
Chris@0 14 *
Chris@0 15 * @see core\vendor\twig\twig\lib\Twig\Environment.php
Chris@0 16 */
Chris@0 17 class TwigEnvironment extends \Twig_Environment {
Chris@0 18
Chris@0 19 /**
Chris@0 20 * Static cache of template classes.
Chris@0 21 *
Chris@0 22 * @var array
Chris@0 23 */
Chris@0 24 protected $templateClasses;
Chris@0 25
Chris@0 26 protected $twigCachePrefix = '';
Chris@0 27
Chris@0 28 /**
Chris@0 29 * Constructs a TwigEnvironment object and stores cache and storage
Chris@0 30 * internally.
Chris@0 31 *
Chris@0 32 * @param string $root
Chris@0 33 * The app root.
Chris@0 34 * @param \Drupal\Core\Cache\CacheBackendInterface $cache
Chris@0 35 * The cache bin.
Chris@0 36 * @param string $twig_extension_hash
Chris@0 37 * The Twig extension hash.
Chris@0 38 * @param \Drupal\Core\State\StateInterface $state
Chris@0 39 * The state service.
Chris@0 40 * @param \Twig_LoaderInterface $loader
Chris@0 41 * The Twig loader or loader chain.
Chris@0 42 * @param array $options
Chris@0 43 * The options for the Twig environment.
Chris@0 44 */
Chris@0 45 public function __construct($root, CacheBackendInterface $cache, $twig_extension_hash, StateInterface $state, \Twig_LoaderInterface $loader = NULL, $options = []) {
Chris@0 46 // Ensure that twig.engine is loaded, given that it is needed to render a
Chris@0 47 // template because functions like TwigExtension::escapeFilter() are called.
Chris@0 48 require_once $root . '/core/themes/engines/twig/twig.engine';
Chris@0 49
Chris@0 50 $this->templateClasses = [];
Chris@0 51
Chris@0 52 $options += [
Chris@0 53 // @todo Ensure garbage collection of expired files.
Chris@0 54 'cache' => TRUE,
Chris@0 55 'debug' => FALSE,
Chris@0 56 'auto_reload' => NULL,
Chris@0 57 ];
Chris@0 58 // Ensure autoescaping is always on.
Chris@0 59 $options['autoescape'] = 'html';
Chris@0 60
Chris@0 61 $policy = new TwigSandboxPolicy();
Chris@0 62 $sandbox = new \Twig_Extension_Sandbox($policy, TRUE);
Chris@0 63 $this->addExtension($sandbox);
Chris@0 64
Chris@0 65 if ($options['cache'] === TRUE) {
Chris@0 66 $current = $state->get('twig_extension_hash_prefix', ['twig_extension_hash' => '']);
Chris@0 67 if ($current['twig_extension_hash'] !== $twig_extension_hash || empty($current['twig_cache_prefix'])) {
Chris@0 68 $current = [
Chris@0 69 'twig_extension_hash' => $twig_extension_hash,
Chris@0 70 // Generate a new prefix which invalidates any existing cached files.
Chris@0 71 'twig_cache_prefix' => uniqid(),
Chris@0 72
Chris@0 73 ];
Chris@0 74 $state->set('twig_extension_hash_prefix', $current);
Chris@0 75 }
Chris@0 76 $this->twigCachePrefix = $current['twig_cache_prefix'];
Chris@0 77
Chris@0 78 $options['cache'] = new TwigPhpStorageCache($cache, $this->twigCachePrefix);
Chris@0 79 }
Chris@0 80
Chris@0 81 $this->loader = $loader;
Chris@0 82 parent::__construct($this->loader, $options);
Chris@0 83 }
Chris@0 84
Chris@0 85 /**
Chris@0 86 * Get the cache prefixed used by \Drupal\Core\Template\TwigPhpStorageCache
Chris@0 87 *
Chris@0 88 * @return string
Chris@0 89 * The file cache prefix, or empty string if the cache is disabled.
Chris@0 90 */
Chris@0 91 public function getTwigCachePrefix() {
Chris@0 92 return $this->twigCachePrefix;
Chris@0 93 }
Chris@0 94
Chris@0 95 /**
Chris@0 96 * Gets the template class associated with the given string.
Chris@0 97 *
Chris@0 98 * @param string $name
Chris@0 99 * The name for which to calculate the template class name.
Chris@0 100 * @param int $index
Chris@0 101 * The index if it is an embedded template.
Chris@0 102 *
Chris@0 103 * @return string
Chris@0 104 * The template class name.
Chris@0 105 */
Chris@0 106 public function getTemplateClass($name, $index = NULL) {
Chris@0 107 // We override this method to add caching because it gets called multiple
Chris@0 108 // times when the same template is used more than once. For example, a page
Chris@0 109 // rendering 50 nodes without any node template overrides will use the same
Chris@0 110 // node.html.twig for the output of each node and the same compiled class.
Chris@0 111 $cache_index = $name . (NULL === $index ? '' : '_' . $index);
Chris@0 112 if (!isset($this->templateClasses[$cache_index])) {
Chris@0 113 $this->templateClasses[$cache_index] = $this->templateClassPrefix . hash('sha256', $this->loader->getCacheKey($name)) . (NULL === $index ? '' : '_' . $index);
Chris@0 114 }
Chris@0 115 return $this->templateClasses[$cache_index];
Chris@0 116 }
Chris@0 117
Chris@0 118 /**
Chris@0 119 * Renders a twig string directly.
Chris@0 120 *
Chris@0 121 * Warning: You should use the render element 'inline_template' together with
Chris@0 122 * the #template attribute instead of this method directly.
Chris@0 123 * On top of that you have to ensure that the template string is not dynamic
Chris@0 124 * but just an ordinary static php string, because there may be installations
Chris@0 125 * using read-only PHPStorage that want to generate all possible twig
Chris@0 126 * templates as part of a build step. So it is important that an automated
Chris@0 127 * script can find the templates and extract them. This is only possible if
Chris@0 128 * the template is a regular string.
Chris@0 129 *
Chris@0 130 * @param string $template_string
Chris@0 131 * The template string to render with placeholders.
Chris@0 132 * @param array $context
Chris@0 133 * An array of parameters to pass to the template.
Chris@0 134 *
Chris@0 135 * @return \Drupal\Component\Render\MarkupInterface|string
Chris@0 136 * The rendered inline template as a Markup object.
Chris@0 137 *
Chris@0 138 * @see \Drupal\Core\Template\Loader\StringLoader::exists()
Chris@0 139 */
Chris@0 140 public function renderInline($template_string, array $context = []) {
Chris@0 141 // Prefix all inline templates with a special comment.
Chris@0 142 $template_string = '{# inline_template_start #}' . $template_string;
Chris@0 143 return Markup::create($this->loadTemplate($template_string, NULL)->render($context));
Chris@0 144 }
Chris@0 145
Chris@0 146 }