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