Yaffs site version 1.1
[yaffs-website] / web / modules / contrib / advagg / advagg_ext_minify / src / Form / SettingsForm.php
1 <?php
2
3 namespace Drupal\advagg_ext_minify\Form;
4
5 use Drupal\Core\Asset\AssetCollectionOptimizerInterface;
6 use Drupal\Core\Cache\Cache;
7 use Drupal\Core\Config\ConfigFactoryInterface;
8 use Drupal\Core\Form\ConfigFormBase;
9 use Drupal\Core\Form\FormStateInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Configure advagg_ext_minify settings for this site.
14  */
15 class SettingsForm extends ConfigFormBase {
16
17   /**
18    * The CSS asset collection optimizer service.
19    *
20    * @var \Drupal\Core\Asset\AssetCollectionOptimizerInterface
21    */
22   protected $cssCollectionOptimizer;
23
24   /**
25    * The JavaScript asset collection optimizer service.
26    *
27    * @var \Drupal\Core\Asset\AssetCollectionOptimizerInterface
28    */
29   protected $jsCollectionOptimizer;
30
31   /**
32    * Constructs a SettingsForm object.
33    *
34    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
35    *   The factory for configuration objects.
36    * @param \Drupal\Core\Asset\AssetCollectionOptimizerInterface $css_collection_optimizer
37    *   The CSS asset collection optimizer service.
38    * @param \Drupal\Core\Asset\AssetCollectionOptimizerInterface $js_collection_optimizer
39    *   The JavaScript asset collection optimizer service.
40    */
41   public function __construct(ConfigFactoryInterface $config_factory, AssetCollectionOptimizerInterface $css_collection_optimizer, AssetCollectionOptimizerInterface $js_collection_optimizer) {
42     parent::__construct($config_factory);
43
44     $this->cssCollectionOptimizer = $css_collection_optimizer;
45     $this->jsCollectionOptimizer = $js_collection_optimizer;
46   }
47
48   /**
49    * {@inheritdoc}
50    */
51   public static function create(ContainerInterface $container) {
52     return new static(
53       $container->get('config.factory'),
54       $container->get('asset.css.collection_optimizer'),
55       $container->get('asset.js.collection_optimizer')
56     );
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public function getFormId() {
63     return 'advagg_ext_minify_settings';
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   protected function getEditableConfigNames() {
70     return ['advagg_ext_minify.settings'];
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function buildForm(array $form, FormStateInterface $form_state) {
77     $form = [];
78
79     // CSS command line.
80     $this->generateForm($form, ['css', $this->t('CSS')]);
81
82     // JS command line.
83     $this->generateForm($form, ['js', $this->t('JavaScript')]);
84     return parent::buildForm($form, $form_state);
85   }
86
87   /**
88    * Generate a form for css or js depending on the input.
89    *
90    * @param array $form
91    *   The form array to add to.
92    * @param array $params
93    *   An array where:
94    *     key 0 is the machine name
95    *     key 1 is the title
96    *     key 2 is the state.
97    */
98   private function generateForm(array &$form, array $params) {
99     $form[$params[0]] = [
100       '#type' => 'fieldset',
101       '#title' => $this->t('@title', ['@title' => $params[1]]),
102     ];
103     $form[$params[0]]['cmd'] = [
104       '#type' => 'fieldset',
105       '#title' => $this->t('Command Line'),
106     ];
107
108     $description = $this->t('{%CWD%} = \Drupal::root(). <br /> {%IN%} = input file. <br /> {%IN_URL_ENC%} = url pointing to the input file that has been url encoded. <br /> {%OUT%} = output file. <br /><br />');
109     if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
110       $description .= ' ' . $this->t('Example using the <a href="@link1">Microsoft Ajax Minifier</a>. <p><code>@code1</code></p>', [
111         '@link1' => 'http://ajaxmin.codeplex.com/',
112         '@code1' => 'AjaxMinifier {%IN%} -o {%OUT%}',
113       ]);
114     }
115
116     if ($params[0] === 'js') {
117       $description .= ' ' . $this->t('Example using the <a href="@link1">Google Closure Compiler</a>. <p><code>@code1</code></p>', [
118         '@link1' => 'https://developers.google.com/closure/compiler/docs/gettingstarted_app',
119         '@code1' => 'java -jar compiler.jar --js {%CWD%}/{%IN%} --js_output_file {%OUT%}',
120       ]);
121
122       $description .= ' ' . $this->t('Example using curl to minify via the <a href="@link1">Online Google Closure Compiler</a>. <p><code>@code1</code></p>', [
123         '@link1' => 'https://developers.google.com/closure/compiler/docs/api-ref',
124         '@code1' => 'curl -o {%OUT%} -d output_info=compiled_code -d code_url={%IN_URL_ENC%} http://closure-compiler.appspot.com/compile',
125       ]);
126     }
127     if ($params[0] === 'css') {
128       $description .= ' ' . $this->t('Example using the <a href="@link1">YUI Compressor</a>. <p><code>@code1</code></p>', [
129         '@link1' => 'http://yui.github.io/yuicompressor/',
130         '@code1' => 'java -jar yuicompressor-x.y.z.jar --type css --line-break 4096 {%CWD%}/{%IN%} -o {%OUT%}',
131       ]);
132
133       $description .= ' ' . $this->t('Example using curl to minify via an online <a href="@link1">CSS Minifier</a>. <p><code>@code1</code></p>', [
134         '@link1' => 'http://cnvyr.io/',
135         '@code1' => 'curl -o {%OUT%} -F \'files0=@{%IN%}\' http://srv.cnvyr.io/v1?min=css',
136       ]);
137     }
138
139     $form[$params[0]]['cmd'][$params[0] . '_cmd'] = [
140       '#type' => 'textfield',
141       '#title' => $this->t('Command to run'),
142       '#default_value' => $this->config('advagg_ext_minify.settings')->get($params[0] . '_cmd'),
143       '#description' => $description,
144     ];
145
146   }
147
148   /**
149    * {@inheritdoc}
150    */
151   public function submitForm(array &$form, FormStateInterface $form_state) {
152     $this->config('advagg_ext_minify.settings')
153       ->set('js_cmd', $form_state->getValue('js_cmd'))
154       ->set('css_cmd', $form_state->getValue('css_cmd'))
155       ->save();
156     parent::submitForm($form, $form_state);
157
158     // Clear relevant caches.
159     $this->cssCollectionOptimizer->deleteAll();
160     $this->jsCollectionOptimizer->deleteAll();
161     Cache::invalidateTags([
162       'library_info',
163       'advagg_css',
164       'advagg_js',
165     ]);
166   }
167
168 }