-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerfbaseSettingsForm.php
More file actions
331 lines (293 loc) · 10.2 KB
/
PerfbaseSettingsForm.php
File metadata and controls
331 lines (293 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php
namespace Perfbase\Drupal\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\ConfigFormBase;
use Perfbase\Drupal\Config\ConfigResolver;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Admin settings form for configuring the Perfbase module.
*
* @phpstan-import-type PerfbaseConfig from \Perfbase\Drupal\Config\ConfigResolver
*/
class PerfbaseSettingsForm extends ConfigFormBase {
private ConfigResolver $configResolver;
public function __construct(
ConfigResolver $configResolver,
ConfigFactoryInterface $configFactory,
TypedConfigManagerInterface $typedConfigManager,
) {
parent::__construct($configFactory, $typedConfigManager);
$this->configResolver = $configResolver;
}
/**
*
*/
public static function create(ContainerInterface $container): self {
$configResolver = $container->get('perfbase.config_resolver');
$configFactory = $container->get('config.factory');
$typedConfigManager = $container->get('config.typed');
if (!$configResolver instanceof ConfigResolver) {
throw new \RuntimeException('Perfbase config resolver service is not available.');
}
if (!$configFactory instanceof ConfigFactoryInterface) {
throw new \RuntimeException('Drupal config factory service is not available.');
}
if (!$typedConfigManager instanceof TypedConfigManagerInterface) {
throw new \RuntimeException('Drupal typed config manager service is not available.');
}
return new self(
$configResolver,
$configFactory,
$typedConfigManager
);
}
/**
*
*/
public function getFormId(): string {
return 'perfbase_settings_form';
}
/**
* @return array<int, string>
*/
protected function getEditableConfigNames(): array {
return ['perfbase.settings'];
}
/**
* @param array<string, mixed> $form
* @return array<string, mixed>
*/
public function buildForm(array $form, FormStateInterface $form_state): array {
$config = $this->configResolver->getConfig();
$form['enabled'] = [
'#type' => 'checkbox',
'#title' => $this->t('Enable Perfbase'),
'#default_value' => !empty($config['enabled']),
];
$form['debug'] = [
'#type' => 'checkbox',
'#title' => $this->t('Debug mode'),
'#default_value' => !empty($config['debug']),
];
$form['log_errors'] = [
'#type' => 'checkbox',
'#title' => $this->t('Log profiling errors'),
'#default_value' => !empty($config['log_errors']),
];
$form['api_key'] = [
'#type' => 'textfield',
'#title' => $this->t('API key'),
'#default_value' => $config['api_key'],
];
$form['api_url'] = [
'#type' => 'url',
'#title' => $this->t('API URL'),
'#default_value' => $config['api_url'],
];
$form['timeout'] = [
'#type' => 'number',
'#title' => $this->t('Timeout'),
'#default_value' => $config['timeout'],
'#min' => 1,
];
$form['proxy'] = [
'#type' => 'textfield',
'#title' => $this->t('Proxy'),
'#default_value' => $config['proxy'],
];
$form['flags'] = [
'#type' => 'number',
'#title' => $this->t('Feature flags bitmask'),
'#default_value' => $config['flags'],
'#min' => 0,
];
$form['sample_rate'] = [
'#type' => 'number',
'#title' => $this->t('Sample rate'),
'#default_value' => $config['sample_rate'],
'#step' => 0.01,
'#min' => 0,
'#max' => 1,
];
$form['environment'] = [
'#type' => 'textfield',
'#title' => $this->t('Environment'),
'#default_value' => $config['environment'],
];
$form['app_version'] = [
'#type' => 'textfield',
'#title' => $this->t('Application version'),
'#default_value' => $config['app_version'],
];
$form['profile_http'] = [
'#type' => 'checkbox',
'#title' => $this->t('Profile HTTP requests'),
'#default_value' => !empty($config['profile_http']),
];
$form['profile_console'] = [
'#type' => 'checkbox',
'#title' => $this->t('Profile console commands'),
'#default_value' => !empty($config['profile_console']),
];
$form['profile_cron'] = [
'#type' => 'checkbox',
'#title' => $this->t('Profile cron'),
'#default_value' => !empty($config['profile_cron']),
];
$form['profile_queue'] = [
'#type' => 'checkbox',
'#title' => $this->t('Profile queue workers'),
'#default_value' => !empty($config['profile_queue']),
];
foreach (['http', 'console', 'cron', 'queue'] as $context) {
$form['include_' . $context] = [
'#type' => 'textarea',
'#title' => $this->t('Include filters for @context', ['@context' => $context]),
'#default_value' => implode("\n", $config['include'][$context]),
];
$form['exclude_' . $context] = [
'#type' => 'textarea',
'#title' => $this->t('Exclude filters for @context', ['@context' => $context]),
'#default_value' => implode("\n", $config['exclude'][$context]),
];
}
return parent::buildForm($form, $form_state);
}
/**
* @param array<mixed> $form
*/
public function validateForm(array &$form, FormStateInterface $form_state): void {
$timeout = $this->intValue($form_state->getValue('timeout'));
if ($timeout < 1) {
$form_state->setErrorByName('timeout', $this->t('Timeout must be at least 1 second.'));
}
$flags = $this->intValue($form_state->getValue('flags'));
if ($flags < 0) {
$form_state->setErrorByName('flags', $this->t('Feature flags bitmask cannot be negative.'));
}
$sampleRate = $this->floatValue($form_state->getValue('sample_rate'));
if ($sampleRate < 0.0 || $sampleRate > 1.0) {
$form_state->setErrorByName('sample_rate', $this->t('Sample rate must be between 0 and 1.'));
}
foreach (['http', 'console', 'cron', 'queue'] as $context) {
$this->validateRegexFilters(
$form_state,
'include_' . $context,
$this->stringValue($form_state->getValue('include_' . $context)),
'Include'
);
$this->validateRegexFilters(
$form_state,
'exclude_' . $context,
$this->stringValue($form_state->getValue('exclude_' . $context)),
'Exclude'
);
}
parent::validateForm($form, $form_state);
}
/**
* @param array<mixed> $form
*/
public function submitForm(array &$form, FormStateInterface $form_state): void {
$config = $this->config('perfbase.settings');
$config
->set('enabled', (bool) $form_state->getValue('enabled'))
->set('debug', (bool) $form_state->getValue('debug'))
->set('log_errors', (bool) $form_state->getValue('log_errors'))
->set('api_key', $this->stringValue($form_state->getValue('api_key')))
->set('api_url', $this->stringValue($form_state->getValue('api_url')))
->set('timeout', $this->intValue($form_state->getValue('timeout')))
->set('proxy', $this->stringValue($form_state->getValue('proxy')))
->set('flags', $this->intValue($form_state->getValue('flags')))
->set('sample_rate', $this->floatValue($form_state->getValue('sample_rate')))
->set('environment', $this->stringValue($form_state->getValue('environment')))
->set('app_version', $this->stringValue($form_state->getValue('app_version')))
->set('profile_http', (bool) $form_state->getValue('profile_http'))
->set('profile_console', (bool) $form_state->getValue('profile_console'))
->set('profile_cron', (bool) $form_state->getValue('profile_cron'))
->set('profile_queue', (bool) $form_state->getValue('profile_queue'))
->set('include', [
'http' => $this->parseFilterLines($this->stringValue($form_state->getValue('include_http'))),
'console' => $this->parseFilterLines($this->stringValue($form_state->getValue('include_console'))),
'cron' => $this->parseFilterLines($this->stringValue($form_state->getValue('include_cron'))),
'queue' => $this->parseFilterLines($this->stringValue($form_state->getValue('include_queue'))),
])
->set('exclude', [
'http' => $this->parseFilterLines($this->stringValue($form_state->getValue('exclude_http'))),
'console' => $this->parseFilterLines($this->stringValue($form_state->getValue('exclude_console'))),
'cron' => $this->parseFilterLines($this->stringValue($form_state->getValue('exclude_cron'))),
'queue' => $this->parseFilterLines($this->stringValue($form_state->getValue('exclude_queue'))),
])
->save();
parent::submitForm($form, $form_state);
}
/**
* @return array<int, string>
*/
private function parseFilterLines(string $value): array {
$lines = preg_split('/\r?\n/', $value) ?: [];
$lines = array_map('trim', $lines);
$lines = array_filter($lines, static function (string $line): bool {
return $line !== '';
});
return array_values($lines);
}
private function validateRegexFilters(FormStateInterface $form_state, string $fieldName, string $value, string $label): void {
foreach ($this->parseFilterLines($value) as $filter) {
if (!preg_match('/^\/.*\/$/', $filter)) {
continue;
}
if (@preg_match($filter, '') === FALSE) {
$form_state->setErrorByName(
$fieldName,
$this->t('@label filter contains an invalid regular expression: @filter', [
'@label' => $label,
'@filter' => $filter,
])
);
return;
}
}
}
/**
* @param mixed $value
*/
private function stringValue($value): string {
if (is_string($value)) {
return $value;
}
if (is_int($value) || is_float($value)) {
return (string) $value;
}
return '';
}
/**
* @param mixed $value
*/
private function intValue($value): int {
if (is_int($value)) {
return $value;
}
if (is_numeric($value)) {
return (int) $value;
}
return 0;
}
/**
* @param mixed $value
*/
private function floatValue($value): float {
if (is_float($value)) {
return $value;
}
if (is_int($value)) {
return (float) $value;
}
if (is_numeric($value)) {
return (float) $value;
}
return 0.0;
}
}