diff --git a/core/lib/Drupal/Core/CoreBundle.php b/core/lib/Drupal/Core/CoreBundle.php index 16faeef..b4b4290 100644 --- a/core/lib/Drupal/Core/CoreBundle.php +++ b/core/lib/Drupal/Core/CoreBundle.php @@ -358,7 +358,7 @@ protected function registerTwig(ContainerBuilder $container) { // This is saved / loaded via drupal_php_storage(). // All files can be refreshed by clearing caches. // @todo ensure garbage collection of expired files. - 'cache' => TRUE, + 'cache' => settings()->get('twig_cache', TRUE), 'base_template_class' => 'Drupal\Core\Template\TwigTemplate', // @todo Remove in followup issue // @see http://drupal.org/node/1712444. @@ -366,10 +366,8 @@ protected function registerTwig(ContainerBuilder $container) { // @todo Remove in followup issue // @see http://drupal.org/node/1806538. 'strict_variables' => FALSE, - // @todo Maybe make debug mode dependent on "production mode" setting. - 'debug' => TRUE, - // @todo Make auto reload mode dependent on "production mode" setting. - 'auto_reload' => FALSE, + 'debug' => settings()->get('twig_debug', FALSE), + 'auto_reload' => settings()->get('twig_auto_reload', NULL), )) ->addMethodCall('addExtension', array(new Definition('Drupal\Core\Template\TwigExtension'))) // @todo Figure out what to do about debugging functions. diff --git a/core/modules/system/lib/Drupal/system/Tests/Theme/TwigSettingsTest.php b/core/modules/system/lib/Drupal/system/Tests/Theme/TwigSettingsTest.php new file mode 100644 index 0000000..003d61e --- /dev/null +++ b/core/modules/system/lib/Drupal/system/Tests/Theme/TwigSettingsTest.php @@ -0,0 +1,109 @@ + 'Twig Settings', + 'description' => 'Tests overriding Twig engine settings via settings.php.', + 'group' => 'Theme', + ); + } + + /** + * Ensures Twig template auto reload setting can be overridden. + */ + function testTwigAutoReloadOverride() { + // Enable auto reload and rebuild the service container. + $this->settingsSet('twig_auto_reload', TRUE); + $this->rebuildContainer(); + + // Check isAutoReload() via the Twig service container. + $this->assertTrue(drupal_container()->get('twig')->isAutoReload(), 'Automatic reloading of Twig templates enabled.'); + + // Disable auto reload and check the service container again. + $this->settingsSet('twig_auto_reload', FALSE); + $this->rebuildContainer(); + + $this->assertFalse(drupal_container()->get('twig')->isAutoReload(), 'Automatic reloading of Twig templates disabled.'); + } + + /** + * Ensures Twig engine debug setting can be overridden. + */ + function testTwigDebugOverride() { + // Enable debug and rebuild the service container. + $this->settingsSet('twig_debug', TRUE); + $this->rebuildContainer(); + + // Check isDebug() via the Twig service container. + $this->assertTrue(drupal_container()->get('twig')->isDebug(), 'Twig debug enabled.'); + $this->assertTrue(drupal_container()->get('twig')->isAutoReload(), 'Twig automatic reloading is enabled when debug is enabled.'); + + // Override auto reload when debug is enabled. + $this->settingsSet('twig_auto_reload', FALSE); + $this->rebuildContainer(); + $this->assertFalse(drupal_container()->get('twig')->isAutoReload(), 'Twig automatic reloading can be disabled when debug is enabled.'); + + // Disable debug and check the service container again. + $this->settingsSet('twig_debug', FALSE); + $this->rebuildContainer(); + + $this->assertFalse(drupal_container()->get('twig')->isDebug(), 'Twig debug disabled.'); + } + + /** + * Ensures Twig template cache setting can be overridden. + */ + function testTwigCacheOverride() { + theme_enable(array('test_theme_twig')); + variable_set('theme_default', 'test_theme_twig'); + + $cache = array(); + // Prime the theme cache. + foreach (module_implements('theme') as $module) { + _theme_process_registry($cache, $module, 'module', $module, drupal_get_path('module', $module)); + } + + // Load array of Twig templates. + $templates = drupal_find_theme_templates($cache, '.html.twig', drupal_get_path('theme', 'test_theme_twig')); + + // Get the template filename and the cache filename for + // theme_test_template_test.html.twig. + $template_filename = $templates['theme_test_template_test']['path'] . '/' . $templates['theme_test_template_test']['template'] . '.html.twig'; + $cache_filename = drupal_container()->get('twig')->getCacheFilename($template_filename); + + // Navigate to the page and make sure the template gets cached. + $this->drupalGet('theme-test/template-test'); + $this->assertTrue(PhpStorageFactory::get('twig')->exists($cache_filename), 'Cached Twig template found.'); + + // Disable the Twig cache and rebuild the service container. + $this->settingsSet('twig_cache', FALSE); + $this->rebuildContainer(); + + // This should return false after rebuilding the service container. + $new_cache_filename = drupal_container()->get('twig')->getCacheFilename($template_filename); + $this->assertFalse($new_cache_filename, 'Twig environment does not return cache filename after caching is disabled.'); + } + +} diff --git a/sites/default/default.settings.php b/sites/default/default.settings.php old mode 100644 new mode 100755 index 8dbb5a7..0531e00 --- a/sites/default/default.settings.php +++ b/sites/default/default.settings.php @@ -284,6 +284,41 @@ $settings['update_free_access'] = FALSE; /** + * Twig debugging: + * + * Display debugging information in comments surrounding Twig template output + * and automatically recompile templates whenever the source code changes. + * + * @see http://drupal.org/node/1906392 + * + * Not recommended in production environments (Default: FALSE). + */ +# $settings['twig_debug'] = TRUE; + +/** + * Twig auto-reload: + * + * Automatically recompile Twig templates whenever the source code changes. If + * you don't provide a value for twig_auto_reload, it will be determined based + * on the value of twig_debug. + * + * Not recommended in production environments (Default: NULL). + */ +# $settings['twig_auto_reload'] = TRUE; + +/** + * Twig cache: + * + * By default, Twig templates will be compiled and stored in the filesystem to + * increase performance. Disabling the Twig cache will recompile the templates + * from source each time they are used. In most cases the twig_auto_reload + * setting above should be enabled rather than disabling the Twig cache. + * + * Not recommended in production environments (Default: TRUE). + */ +# $settings['twig_cache'] = FALSE; + +/** * External access proxy settings: * * If your site must access the Internet via a web proxy then you can enter