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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\PageCache\Console\Command;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Console\Cli;
use Magento\Framework\Filesystem\DriverPool;
use Magento\Framework\Filesystem\File\WriteFactory;
use Magento\Framework\HTTP\PhpEnvironment\Request;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\PageCache\Model\VclGeneratorInterfaceFactory;
use Magento\PageCache\Model\Config;
use Magento\PageCache\Model\Varnish\VclTemplateLocator;
use Magento\Store\Model\ScopeInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class GenerateVclCommand extends Command
{
/**
* Access list option name
*/
const ACCESS_LIST_OPTION = 'access-list';
/**
* Backend host option name
*/
const BACKEND_HOST_OPTION = 'backend-host';
/**
* Backend port option name
*/
const BACKEND_PORT_OPTION = 'backend-port';
/**
* Varnish version option name
*/
const EXPORT_VERSION_OPTION = 'export-version';
/**
* Grace period option name
*/
const GRACE_PERIOD_OPTION = 'grace-period';
/**
* Output file option name
*/
const OUTPUT_FILE_OPTION = 'output-file';
/**
* @var \Magento\Framework\Filesystem\Directory\WriteFactory
*/
private $writeFactory;
/**
* @var VclGeneratorInterfaceFactory
*/
private $vclGeneratorFactory;
/**
* @var array
*/
private $inputToVclMap = [
self::ACCESS_LIST_OPTION => 'accessList',
self::BACKEND_PORT_OPTION => 'backendPort',
self::BACKEND_HOST_OPTION => 'backendHost',
self::GRACE_PERIOD_OPTION => 'gracePeriod',
];
/**
* @var ScopeConfigInterface
*/
private $scopeConfig;
/**
* @var Json
*/
private $serializer;
/**
* @inheritdoc
*/
protected function configure()
{
$this->setName('varnish:vcl:generate')
->setDescription('Generates Varnish VCL and echos it to the command line')
->setDefinition($this->getOptionList());
}
/**
* @param VclGeneratorInterfaceFactory $vclGeneratorFactory
* @param WriteFactory $writeFactory
* @param ScopeConfigInterface $scopeConfig
* @param Json $serializer
*/
public function __construct(
VclGeneratorInterfaceFactory $vclGeneratorFactory,
WriteFactory $writeFactory,
ScopeConfigInterface $scopeConfig,
Json $serializer
) {
parent::__construct();
$this->writeFactory = $writeFactory;
$this->vclGeneratorFactory = $vclGeneratorFactory;
$this->scopeConfig = $scopeConfig;
$this->serializer = $serializer;
}
/**
* @inheritdoc
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$errors = $this->validate($input);
if ($errors) {
foreach ($errors as $error) {
$output->writeln('<error>'.$error.'</error>');
return Cli::RETURN_FAILURE;
}
}
try {
$outputFile = $input->getOption(self::OUTPUT_FILE_OPTION);
$varnishVersion = $input->getOption(self::EXPORT_VERSION_OPTION);
$vclParameters = array_merge($this->inputToVclParameters($input), [
'sslOffloadedHeader' => $this->getSslOffloadedHeader(),
'designExceptions' => $this->getDesignExceptions(),
]);
$vclGenerator = $this->vclGeneratorFactory->create($vclParameters);
$vcl = $vclGenerator->generateVcl($varnishVersion);
if ($outputFile) {
$writer = $this->writeFactory->create($outputFile, DriverPool::FILE, 'w+');
$writer->write($vcl);
$writer->close();
} else {
$output->writeln($vcl);
}
return Cli::RETURN_SUCCESS;
} catch (\Exception $e) {
$output->writeln('<error>'.$e->getMessage().'</error>');
if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
$output->writeln($e->getTraceAsString());
}
return Cli::RETURN_FAILURE;
}
}
/**
* Get list of options for the command
*
* @return InputOption[]
*/
private function getOptionList()
{
return [
new InputOption(
self::ACCESS_LIST_OPTION,
null,
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
'IPs access list that can purge Varnish',
['localhost']
),
new InputOption(
self::BACKEND_HOST_OPTION,
null,
InputOption::VALUE_REQUIRED,
'Host of the web backend',
'localhost'
),
new InputOption(
self::BACKEND_PORT_OPTION,
null,
InputOption::VALUE_REQUIRED,
'Port of the web backend',
8080
),
new InputOption(
self::EXPORT_VERSION_OPTION,
null,
InputOption::VALUE_REQUIRED,
'The version of Varnish file',
VclTemplateLocator::VARNISH_SUPPORTED_VERSION_4
),
new InputOption(
self::GRACE_PERIOD_OPTION,
null,
InputOption::VALUE_REQUIRED,
'Grace period in seconds',
300
),
new InputOption(
self::OUTPUT_FILE_OPTION,
null,
InputOption::VALUE_REQUIRED,
'Path to the file to write vcl'
),
];
}
/**
* @param InputInterface $input
* @return array
*/
private function inputToVclParameters(InputInterface $input)
{
$parameters = [];
foreach ($this->inputToVclMap as $inputKey => $vclKey) {
$parameters[$vclKey] = $input->getOption($inputKey);
}
return $parameters;
}
/**
* Input validation
*
* @param InputInterface $input
* @return array
*/
private function validate(InputInterface $input)
{
$errors = [];
if ($input->hasOption(self::BACKEND_PORT_OPTION)
&& ($input->getOption(self::BACKEND_PORT_OPTION) < 0
|| $input->getOption(self::BACKEND_PORT_OPTION) > 65535)
) {
$errors[] = 'Invalid backend port value';
}
if ($input->hasOption(self::GRACE_PERIOD_OPTION)
&& $input->getOption(self::GRACE_PERIOD_OPTION) < 0
) {
$errors[] = 'Grace period can\'t be lower than 0';
}
return $errors;
}
/**
* Get ssl Offloaded header
*
* @return mixed
*/
private function getSslOffloadedHeader()
{
return $this->scopeConfig->getValue(Request::XML_PATH_OFFLOADER_HEADER);
}
/**
* Get design exceptions
*
* @return array
*/
private function getDesignExceptions()
{
$expressions = $this->scopeConfig->getValue(
Config::XML_VARNISH_PAGECACHE_DESIGN_THEME_REGEX,
ScopeInterface::SCOPE_STORE
);
return $expressions ? $this->serializer->unserialize($expressions) : [];
}
}