Version 1
[yaffs-website] / web / modules / contrib / devel / webprofiler / src / Form / ConfigForm.php
1 <?php
2
3 namespace Drupal\webprofiler\Form;
4
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\Form\ConfigFormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\webprofiler\Profiler\ProfilerStorageManager;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10 use Symfony\Component\HttpKernel\Profiler\Profiler;
11
12 /**
13  * Class ConfigForm
14  */
15 class ConfigForm extends ConfigFormBase {
16
17   /**
18    * @var \Symfony\Component\HttpKernel\Profiler\Profiler
19    */
20   private $profiler;
21
22   /**
23    * @var array
24    */
25   private $templates;
26
27   /**
28    * @var \Drupal\webprofiler\Profiler\ProfilerStorageManager
29    */
30   private $storageManager;
31
32   /**
33    * {@inheritdoc}
34    */
35   public static function create(ContainerInterface $container) {
36     return new static(
37       $container->get('config.factory'),
38       $container->get('profiler'),
39       $container->get('profiler.storage_manager'),
40       $container->getParameter('data_collector.templates')
41     );
42   }
43
44   /**
45    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
46    * @param Profiler $profiler
47    * @param \Drupal\webprofiler\Profiler\ProfilerStorageManager $storageManager
48    * @param array $templates
49    */
50   public function __construct(ConfigFactoryInterface $config_factory, Profiler $profiler, ProfilerStorageManager $storageManager, $templates) {
51     parent::__construct($config_factory);
52
53     $this->profiler = $profiler;
54     $this->templates = $templates;
55     $this->storageManager = $storageManager;
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   public function getFormId() {
62     return 'webprofiler_config';
63   }
64
65   /**
66    * {@inheritdoc}
67    */
68   public function buildForm(array $form, FormStateInterface $form_state) {
69     $config = $this->config('webprofiler.config');
70
71     $form['purge_on_cache_clear'] = [
72       '#type' => 'checkbox',
73       '#title' => $this->t('Purge on cache clear'),
74       '#description' => $this->t('Deletes all profiler files during cache clear.'),
75       '#default_value' => $config->get('purge_on_cache_clear'),
76     ];
77
78     $storages = $this->storageManager->getStorages();
79
80     $form['storage'] = [
81       '#type' => 'select',
82       '#title' => $this->t('Storage backend'),
83       '#description' => $this->t('Choose were to store profiler data.'),
84       '#options' => $storages,
85       '#default_value' => $config->get('storage'),
86     ];
87
88     $form['exclude'] = [
89       '#type' => 'textarea',
90       '#title' => $this->t('Exclude'),
91       '#default_value' => $config->get('exclude'),
92       '#description' => $this->t('Paths to exclude for profiling. One path per line.'),
93     ];
94
95     $form['active_toolbar_items'] = [
96       '#type' => 'checkboxes',
97       '#title' => $this->t('Active toolbar items'),
98       '#options' => $this->getCollectors(),
99       '#description' => $this->t('Choose which items to show into the toolbar.'),
100       '#default_value' => $config->get('active_toolbar_items'),
101     ];
102
103     $form['ide_settings'] = [
104       '#type' => 'details',
105       '#title' => $this->t('IDE settings'),
106       '#open' => FALSE,
107     ];
108
109     $form['ide_settings']['ide_link'] = [
110       '#type' => 'textfield',
111       '#title' => $this->t('IDE link'),
112       '#description' => $this->t('IDE link for open files.'),
113       '#default_value' => $config->get('ide_link'),
114     ];
115
116     $form['ide_settings']['ide_link_remote'] = [
117       '#type' => 'textfield',
118       '#title' => $this->t('IDE link remote path'),
119       '#description' => $this->t('The path of the remote docroot. Leave blank if the docroot is on the same machine of the IDE. No trailing slash.'),
120       '#default_value' => $config->get('ide_link_remote'),
121     ];
122
123     $form['ide_settings']['ide_link_local'] = [
124       '#type' => 'textfield',
125       '#title' => $this->t('IDE link local path'),
126       '#description' => $this->t('The path of the local docroot. Leave blank if the docroot is on the same machine of IDE. No trailing slash.'),
127       '#default_value' => $config->get('ide_link_local'),
128     ];
129
130     $form['database'] = [
131       '#type' => 'details',
132       '#title' => $this->t('Database settings'),
133       '#open' => FALSE,
134       '#states' => array(
135         'visible' => array(
136           array(
137             ':input[name="active_toolbar_items[database]' => array('checked' => TRUE),
138           ),
139         ),
140       ),
141     ];
142
143     $form['database']['query_sort'] = [
144       '#type' => 'radios',
145       '#title' => $this->t('Sort query log'),
146       '#options' => ['source' => 'by source', 'duration' => 'by duration'],
147       '#description' => $this->t('The query table can be sorted in the order that the queries were executed or by descending duration.'),
148       '#default_value' => $config->get('query_sort'),
149     ];
150
151     $form['database']['query_highlight'] = [
152       '#type' => 'number',
153       '#title' => $this->t('Slow query highlighting'),
154       '#description' => $this->t('Enter an integer in milliseconds. Any query which takes longer than this many milliseconds will be highlighted in the query log. This indicates a possibly inefficient query, or a candidate for caching.'),
155       '#default_value' => $config->get('query_highlight'),
156       '#min' => 0,
157     ];
158
159     $storageId = $this->config('webprofiler.config')->get('storage');
160     $storage = $this->storageManager->getStorage($storageId);
161
162     $form['purge'] = [
163       '#type' => 'details',
164       '#title' => $this->t('Purge profiles'),
165       '#open' => FALSE,
166     ];
167
168     $form['purge']['purge'] = [
169       '#type' => 'submit',
170       '#value' => $this->t('Purge'),
171       '#submit' => [[$this, 'purge']],
172     ];
173
174     $form['purge']['purge-help'] = [
175       '#type' => 'inline_template',
176       '#template' => '<div class="form-item">{{ message }}</div>',
177       '#context' => [
178         'message' => $this->t('Purge %storage profiles.', ['%storage' => $storage['title']]),
179       ],
180     ];
181
182     return parent::buildForm($form, $form_state);
183   }
184
185   /**
186    * {@inheritdoc}
187    */
188   public function submitForm(array &$form, FormStateInterface $form_state) {
189     $this->config('webprofiler.config')
190       ->set('purge_on_cache_clear', $form_state->getValue('purge_on_cache_clear'))
191       ->set('storage', $form_state->getValue('storage'))
192       ->set('exclude', $form_state->getValue('exclude'))
193       ->set('active_toolbar_items', $form_state->getValue('active_toolbar_items'))
194       ->set('ide_link', $form_state->getValue('ide_link'))
195       ->set('ide_link_remote', $form_state->getValue('ide_link_remote'))
196       ->set('ide_link_local', $form_state->getValue('ide_link_local'))
197       ->set('query_sort', $form_state->getValue('query_sort'))
198       ->set('query_highlight', $form_state->getValue('query_highlight'))
199       ->save();
200
201     parent::submitForm($form, $form_state);
202   }
203
204   /**
205    * Purges profiles.
206    */
207   public function purge(array &$form, FormStateInterface $form_state) {
208     $this->profiler->purge();
209     drupal_set_message($this->t('Profiles purged'));
210   }
211
212   /**
213    * @return array
214    */
215   private function getCollectors() {
216     $options = [];
217     foreach ($this->templates as $template) {
218       // drupal collector should not be disabled
219       if ($template[0] != 'drupal') {
220         $options[$template[0]] = $template[2];
221       }
222     }
223
224     asort($options);
225
226     return $options;
227   }
228
229   /**
230    * {@inheritdoc}
231    */
232   public function getEditableConfigNames() {
233     return [
234       'webprofiler.config',
235     ];
236   }
237 }