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