-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathBackendConfigForm.php
More file actions
173 lines (148 loc) · 7.3 KB
/
BackendConfigForm.php
File metadata and controls
173 lines (148 loc) · 7.3 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
<?php
// SPDX-FileCopyrightText: 2019 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-3.0-or-later
namespace Icinga\Module\Pdfexport\Forms;
use Exception;
use Icinga\Module\Pdfexport\Backend\Chromedriver;
use Icinga\Module\Pdfexport\Backend\Geckodriver;
use Icinga\Module\Pdfexport\Backend\HeadlessChromeBackend;
use Icinga\Web\Form\ConfigSectionForm;
use ipl\Validator\CallbackValidator;
class BackendConfigForm extends ConfigSectionForm
{
public function assemble(): void
{
$this->addSectionNameElement();
$this->addElement('number', 'priority', [
'label' => $this->translate('Priority'),
'required' => true,
'placeholder' => 100,
'min' => 0,
'description' => $this->translate('The priority of the backend. A lower priority will be used first.'),
]);
$this->addElement('select', 'type', [
'label' => $this->translate('Type'),
'multiOptions' => [
'' => sprintf(' - %s - ', t('Please choose')),
'chrome_webdriver' => t('Chrome WebDriver'),
'firefox_webdriver' => t('Firefox WebDriver'),
'remote_chrome' => t('Headless Chrome (Remote)'),
'local_chrome' => t('Headless Chrome (Local)'),
],
'required' => true,
'class' => 'autosubmit',
]);
$type = $this->getPopulatedValue('type') ?? $this->getConfigValue('type');
switch ($type) {
case 'remote_chrome':
$this->addElement('text', 'host', [
'label' => $this->translate('Host'),
'description' => $this->translate('Host address of the server with the running web browser.'),
'required' => true,
'validators' => [
new CallbackValidator(function ($value, CallbackValidator $validator) {
$port = $this->getValue('port') ?: 9222;
try {
$chrome = HeadlessChromeBackend::createRemote($value, $port);
$version = $chrome->getVersion();
} catch (Exception $e) {
$validator->addMessage($e->getMessage());
return false;
}
if ($version < HeadlessChromeBackend::MIN_SUPPORTED_CHROME_VERSION) {
$validator->addMessage(t(
'Chrome/Chromium supporting headless mode required'
. ' which is provided since version %s. Version detected: %s',
));
return false;
}
return true;
}),
],
]);
$this->addElement('number', 'port', [
'label' => $this->translate('Port'),
'description' => $this->translate('Port of the chrome developer tools. (Default: 9222)'),
'placeholder' => 9222,
'min' => 1,
'max' => 65535,
]);
break;
case 'local_chrome':
$this->addElement('text', 'binary', [
'label' => $this->translate('Binary'),
'placeholder' => '/usr/bin/google-chrome',
'description' => $this->translate('Path to the binary of the web browser.'),
'validators' => [
new CallbackValidator(function ($value, CallbackValidator $validator) {
if (empty($value)) {
return true;
}
try {
$chrome = (HeadlessChromeBackend::createLocal($value));
$version = $chrome->getVersion();
} catch (Exception $e) {
$validator->addMessage($e->getMessage());
return false;
}
if ($version < HeadlessChromeBackend::MIN_SUPPORTED_CHROME_VERSION) {
$validator->addMessage(t(
'Chrome/Chromium supporting headless mode required'
. ' which is provided since version %s. Version detected: %s',
));
}
return true;
}),
],
]);
$this->addElement('checkbox', 'force_temp_storage', [
'label' => $this->translate('Use temp storage'),
'description' => $this->translate(
'Use temp storage to transfer the html to the local chrome instance.'
),
'checkedValue' => '1',
'uncheckedValue' => '0',
]);
break;
case 'firefox_webdriver':
case 'chrome_webdriver':
$this->addElement('text', 'host', [
'label' => $this->translate('Host'),
'description' => $this->translate('Host address of the webdriver server'),
'required' => true,
'validators' => [
new CallbackValidator(function ($value, CallbackValidator $validator) use ($type) {
$port = $this->getValue('port') ?: 4444;
try {
$url = "$value:$port";
$backend = match ($type) {
'chrome_webdriver' => new Chromedriver($url),
'firefox_webdriver' => new Geckodriver($url),
default => throw new Exception("Invalid webdriver type $type"),
};
if (! $backend->isSupported()) {
$validator->addMessage(
t('The webdriver server reports that it is unable to generate PDFs'),
);
return false;
}
} catch (Exception $e) {
$validator->addMessage($e->getMessage());
return false;
}
return true;
}),
],
]);
$this->addElement('number', 'port', [
'label' => $this->translate('Port'),
'description' => $this->translate('Port of the webdriver instance. (Default: 4444)'),
'placeholder' => 4444,
'min' => 1,
'max' => 65535,
]);
break;
}
$this->addButtonElements();
}
}