PageRenderTime 27ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/system/src/Form/PerformanceForm.php

https://gitlab.com/reasonat/test8
PHP | 193 lines | 108 code | 26 blank | 59 comment | 2 complexity | 4fd3590ae85004043c3a6d69ec0003d9 MD5 | raw file
  1. <?php
  2. namespace Drupal\system\Form;
  3. use Drupal\Core\Asset\AssetCollectionOptimizerInterface;
  4. use Drupal\Core\Form\ConfigFormBase;
  5. use Drupal\Core\Config\ConfigFactoryInterface;
  6. use Drupal\Core\Cache\CacheBackendInterface;
  7. use Drupal\Core\Datetime\DateFormatterInterface;
  8. use Drupal\Core\Form\FormStateInterface;
  9. use Symfony\Component\DependencyInjection\ContainerInterface;
  10. /**
  11. * Configure performance settings for this site.
  12. */
  13. class PerformanceForm extends ConfigFormBase {
  14. /**
  15. * The render cache bin.
  16. *
  17. * @var \Drupal\Core\Cache\CacheBackendInterface
  18. */
  19. protected $renderCache;
  20. /**
  21. * The date formatter service.
  22. *
  23. * @var \Drupal\Core\Datetime\DateFormatterInterface
  24. */
  25. protected $dateFormatter;
  26. /**
  27. * The CSS asset collection optimizer service.
  28. *
  29. * @var \Drupal\Core\Asset\AssetCollectionOptimizerInterface
  30. */
  31. protected $cssCollectionOptimizer;
  32. /**
  33. * The JavaScript asset collection optimizer service.
  34. *
  35. * @var \Drupal\Core\Asset\AssetCollectionOptimizerInterface
  36. */
  37. protected $jsCollectionOptimizer;
  38. /**
  39. * Constructs a PerformanceForm object.
  40. *
  41. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
  42. * The factory for configuration objects.
  43. * @param \Drupal\Core\Cache\CacheBackendInterface $render_cache
  44. * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
  45. * The date formatter service.
  46. * @param \Drupal\Core\Asset\AssetCollectionOptimizerInterface $css_collection_optimizer
  47. * The CSS asset collection optimizer service.
  48. * @param \Drupal\Core\Asset\AssetCollectionOptimizerInterface $js_collection_optimizer
  49. * The JavaScript asset collection optimizer service.
  50. */
  51. public function __construct(ConfigFactoryInterface $config_factory, CacheBackendInterface $render_cache, DateFormatterInterface $date_formatter, AssetCollectionOptimizerInterface $css_collection_optimizer, AssetCollectionOptimizerInterface $js_collection_optimizer) {
  52. parent::__construct($config_factory);
  53. $this->renderCache = $render_cache;
  54. $this->dateFormatter = $date_formatter;
  55. $this->cssCollectionOptimizer = $css_collection_optimizer;
  56. $this->jsCollectionOptimizer = $js_collection_optimizer;
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. public static function create(ContainerInterface $container) {
  62. return new static(
  63. $container->get('config.factory'),
  64. $container->get('cache.render'),
  65. $container->get('date.formatter'),
  66. $container->get('asset.css.collection_optimizer'),
  67. $container->get('asset.js.collection_optimizer')
  68. );
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function getFormId() {
  74. return 'system_performance_settings';
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. protected function getEditableConfigNames() {
  80. return ['system.performance'];
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function buildForm(array $form, FormStateInterface $form_state) {
  86. $form['#attached']['library'][] = 'system/drupal.system';
  87. $config = $this->config('system.performance');
  88. $form['clear_cache'] = array(
  89. '#type' => 'details',
  90. '#title' => t('Clear cache'),
  91. '#open' => TRUE,
  92. );
  93. $form['clear_cache']['clear'] = array(
  94. '#type' => 'submit',
  95. '#value' => t('Clear all caches'),
  96. '#submit' => array('::submitCacheClear'),
  97. );
  98. $form['caching'] = array(
  99. '#type' => 'details',
  100. '#title' => t('Caching'),
  101. '#open' => TRUE,
  102. '#description' => $this->t('Note: Drupal provides an internal page cache module that is recommended for small to medium-sized websites.'),
  103. );
  104. // Identical options to the ones for block caching.
  105. // @see \Drupal\Core\Block\BlockBase::buildConfigurationForm()
  106. $period = array(0, 60, 180, 300, 600, 900, 1800, 2700, 3600, 10800, 21600, 32400, 43200, 86400);
  107. $period = array_map(array($this->dateFormatter, 'formatInterval'), array_combine($period, $period));
  108. $period[0] = '<' . t('no caching') . '>';
  109. $form['caching']['page_cache_maximum_age'] = array(
  110. '#type' => 'select',
  111. '#title' => t('Page cache maximum age'),
  112. '#default_value' => $config->get('cache.page.max_age'),
  113. '#options' => $period,
  114. '#description' => t('The maximum time a page can be cached by browsers and proxies. This is used as the value for max-age in Cache-Control headers.'),
  115. );
  116. $directory = 'public://';
  117. $is_writable = is_dir($directory) && is_writable($directory);
  118. $disabled = !$is_writable;
  119. $disabled_message = '';
  120. if (!$is_writable) {
  121. $disabled_message = ' ' . t('<strong class="error">Set up the <a href=":file-system">public files directory</a> to make these optimizations available.</strong>', array(':file-system' => $this->url('system.file_system_settings')));
  122. }
  123. $form['bandwidth_optimization'] = array(
  124. '#type' => 'details',
  125. '#title' => t('Bandwidth optimization'),
  126. '#open' => TRUE,
  127. '#description' => t('External resources can be optimized automatically, which can reduce both the size and number of requests made to your website.') . $disabled_message,
  128. );
  129. $form['bandwidth_optimization']['preprocess_css'] = array(
  130. '#type' => 'checkbox',
  131. '#title' => t('Aggregate CSS files'),
  132. '#default_value' => $config->get('css.preprocess'),
  133. '#disabled' => $disabled,
  134. );
  135. $form['bandwidth_optimization']['preprocess_js'] = array(
  136. '#type' => 'checkbox',
  137. '#title' => t('Aggregate JavaScript files'),
  138. '#default_value' => $config->get('js.preprocess'),
  139. '#disabled' => $disabled,
  140. );
  141. return parent::buildForm($form, $form_state);
  142. }
  143. /**
  144. * {@inheritdoc}
  145. */
  146. public function submitForm(array &$form, FormStateInterface $form_state) {
  147. $this->cssCollectionOptimizer->deleteAll();
  148. $this->jsCollectionOptimizer->deleteAll();
  149. // This form allows page compression settings to be changed, which can
  150. // invalidate cached pages in the render cache, so it needs to be cleared on
  151. // form submit.
  152. $this->renderCache->deleteAll();
  153. $this->config('system.performance')
  154. ->set('cache.page.max_age', $form_state->getValue('page_cache_maximum_age'))
  155. ->set('css.preprocess', $form_state->getValue('preprocess_css'))
  156. ->set('js.preprocess', $form_state->getValue('preprocess_js'))
  157. ->save();
  158. parent::submitForm($form, $form_state);
  159. }
  160. /**
  161. * Clears the caches.
  162. */
  163. public function submitCacheClear(array &$form, FormStateInterface $form_state) {
  164. drupal_flush_all_caches();
  165. drupal_set_message(t('Caches cleared.'));
  166. }
  167. }