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