summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2018-05-05 00:19:42 (GMT)
committerAlex Pott2018-05-05 00:19:42 (GMT)
commit74255a0a4921fa8524105dcd0b1868bb88e6aa5e (patch)
treeb24116d82fc98ea013e5c18d403a49c09408adf3
parente2d8c4c060563128773e41790d35a74074da6340 (diff)
Revert "Issue #2752961 by dawehner, Wim Leers, Jo Fitzgerald, bradjones1, chr.fritsch, greg.1.anderson, anavarre, Berdir, moshe weitzman, bkosborne, alexpott, Crell, catch, msonnabaum, lauriii, joelpittet, deviantintegral, cilefen, effulgentsia, xjm: No reliable method exists for clearing the Twig cache"
This reverts commit ada497ac26f833eaa9f227edff6a919ec5da6252.
-rw-r--r--core/includes/common.inc3
-rw-r--r--core/includes/utility.inc3
-rw-r--r--core/lib/Drupal/Core/Template/TwigEnvironment.php31
-rw-r--r--core/tests/Drupal/KernelTests/Core/Theme/TwigEnvironmentTest.php50
4 files changed, 6 insertions, 81 deletions
diff --git a/core/includes/common.inc b/core/includes/common.inc
index 04324f6..ffdd3fa 100644
--- a/core/includes/common.inc
+++ b/core/includes/common.inc
@@ -17,6 +17,7 @@ use Drupal\Core\Cache\Cache;
use Drupal\Core\Render\Element\Link;
use Drupal\Core\Render\Markup;
use Drupal\Core\StringTranslation\TranslatableMarkup;
+use Drupal\Core\PhpStorage\PhpStorageFactory;
use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Render\Element;
@@ -1116,7 +1117,7 @@ function drupal_flush_all_caches() {
\Drupal::service('kernel')->invalidateContainer();
// Wipe the Twig PHP Storage cache.
- \Drupal::service('twig')->invalidate();
+ PhpStorageFactory::get('twig')->deleteAll();
// Rebuild module and theme data.
$module_data = system_rebuild_module_data();
diff --git a/core/includes/utility.inc b/core/includes/utility.inc
index 52c3699..70d74a1 100644
--- a/core/includes/utility.inc
+++ b/core/includes/utility.inc
@@ -5,6 +5,7 @@
* Miscellaneous functions.
*/
+use Drupal\Core\PhpStorage\PhpStorageFactory;
use Drupal\Core\Cache\Cache;
use Drupal\Core\DrupalKernel;
use Symfony\Component\HttpFoundation\Request;
@@ -29,7 +30,7 @@ function drupal_rebuild($class_loader, Request $request) {
restore_exception_handler();
// Force kernel to rebuild php cache.
- \Drupal::service('twig')->invalidate();
+ PhpStorageFactory::get('twig')->deleteAll();
// Bootstrap up to where caches exist and clear them.
$kernel = new DrupalKernel('prod', $class_loader);
diff --git a/core/lib/Drupal/Core/Template/TwigEnvironment.php b/core/lib/Drupal/Core/Template/TwigEnvironment.php
index a3a8304..21755a5 100644
--- a/core/lib/Drupal/Core/Template/TwigEnvironment.php
+++ b/core/lib/Drupal/Core/Template/TwigEnvironment.php
@@ -3,7 +3,6 @@
namespace Drupal\Core\Template;
use Drupal\Core\Cache\CacheBackendInterface;
-use Drupal\Core\PhpStorage\PhpStorageFactory;
use Drupal\Core\Render\Markup;
use Drupal\Core\State\StateInterface;
@@ -18,18 +17,6 @@ use Drupal\Core\State\StateInterface;
class TwigEnvironment extends \Twig_Environment {
/**
- * Key name of the Twig cache prefix metadata key-value pair in State.
- */
- const CACHE_PREFIX_METADATA_KEY = 'twig_extension_hash_prefix';
-
- /**
- * The state service.
- *
- * @var \Drupal\Core\State\StateInterface
- */
- protected $state;
-
- /**
* Static cache of template classes.
*
* @var array
@@ -56,8 +43,6 @@ class TwigEnvironment extends \Twig_Environment {
* The options for the Twig environment.
*/
public function __construct($root, CacheBackendInterface $cache, $twig_extension_hash, StateInterface $state, \Twig_LoaderInterface $loader = NULL, $options = []) {
- $this->state = $state;
-
// Ensure that twig.engine is loaded, given that it is needed to render a
// template because functions like TwigExtension::escapeFilter() are called.
require_once $root . '/core/themes/engines/twig/twig.engine';
@@ -78,7 +63,7 @@ class TwigEnvironment extends \Twig_Environment {
$this->addExtension($sandbox);
if ($options['cache'] === TRUE) {
- $current = $state->get(static::CACHE_PREFIX_METADATA_KEY, ['twig_extension_hash' => '']);
+ $current = $state->get('twig_extension_hash_prefix', ['twig_extension_hash' => '']);
if ($current['twig_extension_hash'] !== $twig_extension_hash || empty($current['twig_cache_prefix'])) {
$current = [
'twig_extension_hash' => $twig_extension_hash,
@@ -86,7 +71,7 @@ class TwigEnvironment extends \Twig_Environment {
'twig_cache_prefix' => uniqid(),
];
- $state->set(static::CACHE_PREFIX_METADATA_KEY, $current);
+ $state->set('twig_extension_hash_prefix', $current);
}
$this->twigCachePrefix = $current['twig_cache_prefix'];
@@ -98,18 +83,6 @@ class TwigEnvironment extends \Twig_Environment {
}
/**
- * Invalidates all compiled Twig templates.
- *
- * @see \drupal_flush_all_caches
- */
- public function invalidate() {
- PhpStorageFactory::get('twig')->deleteAll();
- $this->templateClasses = [];
- $this->loadedTemplates = [];
- $this->state->delete(static::CACHE_PREFIX_METADATA_KEY);
- }
-
- /**
* Get the cache prefixed used by \Drupal\Core\Template\TwigPhpStorageCache
*
* @return string
diff --git a/core/tests/Drupal/KernelTests/Core/Theme/TwigEnvironmentTest.php b/core/tests/Drupal/KernelTests/Core/Theme/TwigEnvironmentTest.php
index 854c25c..5d6f61f 100644
--- a/core/tests/Drupal/KernelTests/Core/Theme/TwigEnvironmentTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Theme/TwigEnvironmentTest.php
@@ -4,11 +4,9 @@ namespace Drupal\KernelTests\Core\Theme;
use Drupal\Component\Utility\Crypt;
use Drupal\Component\Utility\Html;
-use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Site\Settings;
use Drupal\Core\Template\TwigPhpStorageCache;
use Drupal\KernelTests\KernelTestBase;
-use Symfony\Component\DependencyInjection\Definition;
/**
* Tests the twig environment.
@@ -145,52 +143,4 @@ class TwigEnvironmentTest extends KernelTestBase {
$this->assertNotEqual($new_extension_filename, $original_filename);
}
- /**
- * {@inheritdoc}
- */
- public function register(ContainerBuilder $container) {
- parent::register($container);
-
- $container->setDefinition('twig_loader__file_system', new Definition('Twig_Loader_Filesystem', [[sys_get_temp_dir()]]))
- ->addTag('twig.loader');
- }
-
-
- /**
- * Test template invalidation.
- */
- public function testTemplateInvalidation() {
- $template_before = <<<TWIG
-<div>Hello before</div>
-TWIG;
- $template_after = <<<TWIG
-<div>Hello after</div>
-TWIG;
-
- $tempfile = tempnam(sys_get_temp_dir(), '__METHOD__') . '.html.twig';
- file_put_contents($tempfile, $template_before);
-
- /** @var \Drupal\Core\Template\TwigEnvironment $environment */
- $environment = \Drupal::service('twig');
-
- $output = $environment->load(basename($tempfile))->render();
- $this->assertEquals($template_before, $output);
-
- file_put_contents($tempfile, $template_after);
- $output = $environment->load(basename($tempfile))->render();
- $this->assertEquals($template_before, $output);
-
- $environment->invalidate();
- // Manually change $templateClassPrefix to force a different template
- // classname, as the other class is still loaded. This wouldn't be a problem
- // on a real site where you reload the page.
- $reflection = new \ReflectionClass($environment);
- $property_reflection = $reflection->getProperty('templateClassPrefix');
- $property_reflection->setAccessible(TRUE);
- $property_reflection->setValue($environment, 'otherPrefix');
-
- $output = $environment->load(basename($tempfile))->render();
- $this->assertEquals($template_after, $output);
- }
-
}