Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
GenerateVclCommand.php
Go to the documentation of this file.
1 <?php
8 
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;
23 
27 class GenerateVclCommand extends Command
28 {
32  const ACCESS_LIST_OPTION = 'access-list';
33 
37  const BACKEND_HOST_OPTION = 'backend-host';
38 
42  const BACKEND_PORT_OPTION = 'backend-port';
43 
47  const EXPORT_VERSION_OPTION = 'export-version';
48 
52  const GRACE_PERIOD_OPTION = 'grace-period';
53 
57  const OUTPUT_FILE_OPTION = 'output-file';
58 
62  private $writeFactory;
63 
67  private $vclGeneratorFactory;
68 
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',
77  ];
78 
82  private $scopeConfig;
83 
87  private $serializer;
88 
92  protected function configure()
93  {
94  $this->setName('varnish:vcl:generate')
95  ->setDescription('Generates Varnish VCL and echos it to the command line')
96  ->setDefinition($this->getOptionList());
97  }
98 
105  public function __construct(
106  VclGeneratorInterfaceFactory $vclGeneratorFactory,
107  WriteFactory $writeFactory,
108  ScopeConfigInterface $scopeConfig,
109  Json $serializer
110  ) {
111  parent::__construct();
112  $this->writeFactory = $writeFactory;
113  $this->vclGeneratorFactory = $vclGeneratorFactory;
114  $this->scopeConfig = $scopeConfig;
115  $this->serializer = $serializer;
116  }
117 
121  protected function execute(InputInterface $input, OutputInterface $output)
122  {
123  $errors = $this->validate($input);
124  if ($errors) {
125  foreach ($errors as $error) {
126  $output->writeln('<error>'.$error.'</error>');
127 
128  return Cli::RETURN_FAILURE;
129  }
130  }
131 
132  try {
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(),
138  ]);
139  $vclGenerator = $this->vclGeneratorFactory->create($vclParameters);
140  $vcl = $vclGenerator->generateVcl($varnishVersion);
141 
142  if ($outputFile) {
143  $writer = $this->writeFactory->create($outputFile, DriverPool::FILE, 'w+');
144  $writer->write($vcl);
145  $writer->close();
146  } else {
147  $output->writeln($vcl);
148  }
149 
150  return Cli::RETURN_SUCCESS;
151  } catch (\Exception $e) {
152  $output->writeln('<error>'.$e->getMessage().'</error>');
153  if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
154  $output->writeln($e->getTraceAsString());
155  }
156 
157  return Cli::RETURN_FAILURE;
158  }
159  }
160 
166  private function getOptionList()
167  {
168  return [
169  new InputOption(
170  self::ACCESS_LIST_OPTION,
171  null,
172  InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
173  'IPs access list that can purge Varnish',
174  ['localhost']
175  ),
176  new InputOption(
177  self::BACKEND_HOST_OPTION,
178  null,
179  InputOption::VALUE_REQUIRED,
180  'Host of the web backend',
181  'localhost'
182  ),
183  new InputOption(
184  self::BACKEND_PORT_OPTION,
185  null,
186  InputOption::VALUE_REQUIRED,
187  'Port of the web backend',
188  8080
189  ),
190  new InputOption(
191  self::EXPORT_VERSION_OPTION,
192  null,
193  InputOption::VALUE_REQUIRED,
194  'The version of Varnish file',
196  ),
197  new InputOption(
198  self::GRACE_PERIOD_OPTION,
199  null,
200  InputOption::VALUE_REQUIRED,
201  'Grace period in seconds',
202  300
203  ),
204  new InputOption(
205  self::OUTPUT_FILE_OPTION,
206  null,
207  InputOption::VALUE_REQUIRED,
208  'Path to the file to write vcl'
209  ),
210  ];
211  }
212 
217  private function inputToVclParameters(InputInterface $input)
218  {
219  $parameters = [];
220 
221  foreach ($this->inputToVclMap as $inputKey => $vclKey) {
222  $parameters[$vclKey] = $input->getOption($inputKey);
223  }
224 
225  return $parameters;
226  }
227 
234  private function validate(InputInterface $input)
235  {
236  $errors = [];
237 
238  if ($input->hasOption(self::BACKEND_PORT_OPTION)
239  && ($input->getOption(self::BACKEND_PORT_OPTION) < 0
240  || $input->getOption(self::BACKEND_PORT_OPTION) > 65535)
241  ) {
242  $errors[] = 'Invalid backend port value';
243  }
244 
245  if ($input->hasOption(self::GRACE_PERIOD_OPTION)
246  && $input->getOption(self::GRACE_PERIOD_OPTION) < 0
247  ) {
248  $errors[] = 'Grace period can\'t be lower than 0';
249  }
250 
251  return $errors;
252  }
253 
259  private function getSslOffloadedHeader()
260  {
261  return $this->scopeConfig->getValue(Request::XML_PATH_OFFLOADER_HEADER);
262  }
263 
269  private function getDesignExceptions()
270  {
271  $expressions = $this->scopeConfig->getValue(
274  );
275 
276  return $expressions ? $this->serializer->unserialize($expressions) : [];
277  }
278 }
$expressions
Definition: side-menu.phtml:10
__construct(VclGeneratorInterfaceFactory $vclGeneratorFactory, WriteFactory $writeFactory, ScopeConfigInterface $scopeConfig, Json $serializer)
const XML_VARNISH_PAGECACHE_DESIGN_THEME_REGEX
Definition: Config.php:45
execute(InputInterface $input, OutputInterface $output)
$errors
Definition: overview.phtml:9