1665421b3e05d95987a67bbca9c14db5e0c58a0b
[yaffs-website] / web / modules / contrib / advagg / advagg_js_minify / src / Form / SettingsForm.php
1 <?php
2
3 namespace Drupal\advagg_js_minify\Form;
4
5 use Drupal\Component\Utility\Xss;
6 use Drupal\Core\Asset\AssetCollectionOptimizerInterface;
7 use Drupal\Core\Asset\AssetOptimizerInterface;
8 use Drupal\Core\Cache\Cache;
9 use Drupal\Core\Config\ConfigFactoryInterface;
10 use Drupal\Core\Form\ConfigFormBase;
11 use Drupal\Core\Form\FormStateInterface;
12 use Drupal\Core\State\StateInterface;
13 use Drupal\Core\Url;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
15
16 /**
17  * Configure advagg_js_minify settings for this site.
18  */
19 class SettingsForm extends ConfigFormBase {
20
21   /**
22    * The JavaScript asset collection optimizer service.
23    *
24    * @var \Drupal\Core\Asset\AssetCollectionOptimizerInterface
25    */
26   protected $jsCollectionOptimizer;
27
28   /**
29    * The AdvAgg file status state information storage service.
30    *
31    * @var \Drupal\Core\State\StateInterface
32    */
33   protected $advaggFiles;
34
35   /**
36    * A JS asset optimizer.
37    *
38    * @var \Drupal\advagg_js_minify\Asset\CssOptimizer
39    */
40   protected $optimizer;
41
42   /**
43    * Constructs a SettingsForm object.
44    *
45    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
46    *   The factory for configuration objects.
47    * @param \Drupal\Core\Asset\AssetCollectionOptimizerInterface $js_collection_optimizer
48    *   The JavaScript asset collection optimizer service.
49    * @param \Drupal\Core\State\StateInterface $advagg_files
50    *   The AdvAgg file status state information storage service.
51    * @param \Drupal\Core\Asset\AssetOptimizerInterface $optimizer
52    *   The optimizer for a single JS asset.
53    */
54   public function __construct(ConfigFactoryInterface $config_factory, AssetCollectionOptimizerInterface $js_collection_optimizer, StateInterface $advagg_files, AssetOptimizerInterface $optimizer) {
55     parent::__construct($config_factory);
56     $this->jsCollectionOptimizer = $js_collection_optimizer;
57     $this->advaggFiles = $advagg_files;
58     $this->optimizer = $optimizer;
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public static function create(ContainerInterface $container) {
65     return new static(
66       $container->get('config.factory'),
67       $container->get('asset.js.collection_optimizer'),
68       $container->get('state.advagg.files'),
69       $container->get('asset.js.optimizer')
70     );
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function getFormId() {
77     return 'advagg_js_minify_settings';
78   }
79
80   /**
81    * {@inheritdoc}
82    */
83   protected function getEditableConfigNames() {
84     return ['advagg_js_minify.settings'];
85   }
86
87   /**
88    * {@inheritdoc}
89    */
90   public function buildForm(array $form, FormStateInterface $form_state) {
91
92     $config = $this->config('advagg_js_minify.settings');
93     $form = [];
94     if ($this->config('advagg.settings')->get('cache_level') < 0) {
95       $form['advagg_devel_msg'] = [
96         '#markup' => '<p>' . $this->t('The settings below will not have any effect because AdvAgg is currently in <a href="@devel">development mode</a>. Once the cache settings have been set to normal or aggressive, JS minification will take place.', [
97           '@devel' => Url::fromRoute('advagg.settings', [], [
98             'fragment' => 'edit-advagg-cache-level',
99           ]),
100         ]) . '</p>',
101       ];
102     }
103
104     list($options, $description) = $this->optimizer->getConfiguration();
105
106     $form['minifier'] = [
107       '#type' => 'radios',
108       '#title' => $this->t('Minification: Select a minifier'),
109       '#default_value' => $config->get('minifier'),
110       '#options' => $options,
111       '#description' => Xss::filter($description),
112     ];
113     $form['add_license'] = [
114       '#type' => 'checkbox',
115       '#title' => $this->t('Add licensing comments'),
116       '#default_value' => $config->get('add_license'),
117       '#description' => $this->t("If unchecked, the Advanced Aggregation module's licensing comments
118       will be omitted from the aggregated files. Omitting the comments will produce somewhat better scores in
119       some automated security scans but otherwise should not affect your site. These are included by default in order to better follow the spirit of the GPL by providing the source for javascript files."),
120     ];
121
122     $options[-1] = $this->t('Default');
123     ksort($options);
124
125     $form['per_file_settings'] = [
126       '#type' => 'details',
127       '#title' => $this->t('Per File Settings'),
128     ];
129     $files = $this->advaggFiles->getAll();
130     $file_settings = $config->get('file_settings');
131     if ($file_settings) {
132       $file_settings = array_column($file_settings, 'minifier', 'path');
133     }
134     foreach ($files as $name => $info) {
135       if ($info['fileext'] !== 'js') {
136         continue;
137       }
138       $dir = dirname($name);
139       if (!isset($form['per_file_settings'][$dir])) {
140         $form['per_file_settings'][$dir] = [
141           '#type' => 'details',
142           '#title' => $dir,
143         ];
144       }
145       $form_api_filename = str_replace(['/', '.'], ['__', '--'], $name);
146       $form['per_file_settings'][$dir]['advagg_js_minify_file_settings_' . $form_api_filename] = [
147         '#type' => 'radios',
148         '#title' => $this->t('%filename: Select a minifier', ['%filename' => $name]),
149         '#default_value' => isset($file_settings[$name]) ? $file_settings[$name] : -1,
150         '#options' => $options,
151       ];
152       if ($form['per_file_settings'][$dir]['advagg_js_minify_file_settings_' . $form_api_filename]['#default_value'] != -1) {
153         $form['per_file_settings'][$dir]['#open'] = TRUE;
154         $form['per_file_settings']['#open'] = TRUE;
155       }
156     }
157
158     // No js files are found.
159     if (empty($files)) {
160       $form['per_file_settings']['#description'] = $this->t('No JS files have been aggregated. You need to enable aggregation. No js files where found in the advagg_files table.');
161     }
162
163     return parent::buildForm($form, $form_state);
164   }
165
166   /**
167    * {@inheritdoc}
168    */
169   public function submitForm(array &$form, FormStateInterface $form_state) {
170     $config = $this->config('advagg_js_minify.settings');
171
172     // Extract/combine per file settings.
173     if ($file_settings = $config->get('file_settings')) {
174       $file_settings = array_column($file_settings, NULL, 'path');
175     }
176     else {
177       $file_settings = [];
178     }
179     foreach ($form_state->getValues() as $key => $value) {
180       // Skip if not a advagg_js_minify_file_settings form item.
181       if (strpos($key, 'advagg_js_minify_file_settings_') === FALSE) {
182         continue;
183       }
184       $path = str_replace(['__', '--'], ['/', '.'], substr($key, 31));
185       if ($value == -1) {
186         unset($file_settings[$path]);
187         continue;
188       }
189       else {
190         $file_settings[$path] = [
191           'path' => $path,
192           'minifier' => $value,
193         ];
194       }
195     }
196
197     // Clear Caches.
198     $this->jsCollectionOptimizer->deleteAll();
199     Cache::invalidateTags(['library_info', 'advagg_js']);
200
201     // Save settings.
202     $config->set('add_license', $form_state->getValue('add_license'))
203       ->set('minifier', $form_state->getValue('minifier'))
204       ->set('file_settings', array_values($file_settings))
205       ->save();
206     parent::submitForm($form, $form_state);
207   }
208
209 }