ConfigForm.php 7.2 KB

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