15 use Magento\PageCache\Model\VclGeneratorInterfaceFactory;
19 use Symfony\Component\Console\Command\Command;
20 use Symfony\Component\Console\Input\InputInterface;
21 use Symfony\Component\Console\Input\InputOption;
22 use Symfony\Component\Console\Output\OutputInterface;
62 private $writeFactory;
67 private $vclGeneratorFactory;
72 private $inputToVclMap = [
73 self::ACCESS_LIST_OPTION =>
'accessList',
74 self::BACKEND_PORT_OPTION =>
'backendPort',
75 self::BACKEND_HOST_OPTION =>
'backendHost',
76 self::GRACE_PERIOD_OPTION =>
'gracePeriod',
94 $this->setName(
'varnish:vcl:generate')
95 ->setDescription(
'Generates Varnish VCL and echos it to the command line')
96 ->setDefinition($this->getOptionList());
106 VclGeneratorInterfaceFactory $vclGeneratorFactory,
111 parent::__construct();
112 $this->writeFactory = $writeFactory;
113 $this->vclGeneratorFactory = $vclGeneratorFactory;
114 $this->scopeConfig = $scopeConfig;
123 $errors = $this->validate($input);
126 $output->writeln(
'<error>'.$error.
'</error>');
133 $outputFile = $input->getOption(self::OUTPUT_FILE_OPTION);
134 $varnishVersion = $input->getOption(self::EXPORT_VERSION_OPTION);
135 $vclParameters = array_merge($this->inputToVclParameters($input), [
136 'sslOffloadedHeader' => $this->getSslOffloadedHeader(),
137 'designExceptions' => $this->getDesignExceptions(),
139 $vclGenerator = $this->vclGeneratorFactory->create($vclParameters);
140 $vcl = $vclGenerator->generateVcl($varnishVersion);
144 $writer->write($vcl);
151 }
catch (\Exception $e) {
152 $output->writeln(
'<error>'.$e->getMessage().
'</error>');
153 if (
$output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
154 $output->writeln($e->getTraceAsString());
166 private function getOptionList()
170 self::ACCESS_LIST_OPTION,
172 InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
173 'IPs access list that can purge Varnish',
177 self::BACKEND_HOST_OPTION,
179 InputOption::VALUE_REQUIRED,
180 'Host of the web backend',
184 self::BACKEND_PORT_OPTION,
186 InputOption::VALUE_REQUIRED,
187 'Port of the web backend',
191 self::EXPORT_VERSION_OPTION,
193 InputOption::VALUE_REQUIRED,
194 'The version of Varnish file',
198 self::GRACE_PERIOD_OPTION,
200 InputOption::VALUE_REQUIRED,
201 'Grace period in seconds',
205 self::OUTPUT_FILE_OPTION,
207 InputOption::VALUE_REQUIRED,
208 'Path to the file to write vcl' 217 private function inputToVclParameters(InputInterface $input)
221 foreach ($this->inputToVclMap as $inputKey => $vclKey) {
222 $parameters[$vclKey] = $input->getOption($inputKey);
234 private function validate(InputInterface $input)
238 if ($input->hasOption(self::BACKEND_PORT_OPTION)
239 && ($input->getOption(self::BACKEND_PORT_OPTION) < 0
240 || $input->getOption(self::BACKEND_PORT_OPTION) > 65535)
242 $errors[] =
'Invalid backend port value';
245 if ($input->hasOption(self::GRACE_PERIOD_OPTION)
246 && $input->getOption(self::GRACE_PERIOD_OPTION) < 0
248 $errors[] =
'Grace period can\'t be lower than 0';
259 private function getSslOffloadedHeader()
269 private function getDesignExceptions()
const XML_PATH_OFFLOADER_HEADER
__construct(VclGeneratorInterfaceFactory $vclGeneratorFactory, WriteFactory $writeFactory, ScopeConfigInterface $scopeConfig, Json $serializer)
const BACKEND_HOST_OPTION
const EXPORT_VERSION_OPTION
const GRACE_PERIOD_OPTION
const XML_VARNISH_PAGECACHE_DESIGN_THEME_REGEX
execute(InputInterface $input, OutputInterface $output)
const VARNISH_SUPPORTED_VERSION_4
const BACKEND_PORT_OPTION