Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
VclGenerator.php
Go to the documentation of this file.
1 <?php
8 
11 
13 {
17  private $backendHost;
18 
22  private $backendPort;
23 
27  private $accessList;
28 
32  private $gracePeriod;
33 
37  private $vclTemplateLocator;
38 
42  private $sslOffloadedHeader;
43 
47  private $designExceptions;
48 
60  public function __construct(
61  VclTemplateLocatorInterface $vclTemplateLocator,
62  $backendHost,
63  $backendPort,
64  $accessList,
65  $gracePeriod,
66  $sslOffloadedHeader,
67  $designExceptions = []
68  ) {
69  $this->backendHost = $backendHost;
70  $this->backendPort = $backendPort;
71  $this->accessList = $accessList;
72  $this->gracePeriod = $gracePeriod;
73  $this->vclTemplateLocator = $vclTemplateLocator;
74  $this->sslOffloadedHeader = $sslOffloadedHeader;
75  $this->designExceptions = $designExceptions;
76  }
77 
85  public function generateVcl($version)
86  {
87  $template = $this->vclTemplateLocator->getTemplate($version);
88  return strtr($template, $this->getReplacements());
89  }
90 
96  private function getReplacements()
97  {
98  return [
99  '/* {{ host }} */' => $this->getBackendHost(),
100  '/* {{ port }} */' => $this->getBackendPort(),
101  '/* {{ ips }} */' => $this->getTransformedAccessList(),
102  '/* {{ design_exceptions_code }} */' => $this->getRegexForDesignExceptions(),
103  // http headers get transformed by php `X-Forwarded-Proto: https`
104  // becomes $SERVER['HTTP_X_FORWARDED_PROTO'] = 'https'
105  // Apache and Nginx drop all headers with underlines by default.
106  '/* {{ ssl_offloaded_header }} */' => str_replace('_', '-', $this->getSslOffloadedHeader()),
107  '/* {{ grace_period }} */' => $this->getGracePeriod(),
108  ];
109  }
110 
119  private function getRegexForDesignExceptions()
120  {
121  $result = '';
122  $tpl = "%s (req.http.user-agent ~ \"%s\") {\n"." hash_data(\"%s\");\n"." }";
123 
124  $expressions = $this->getDesignExceptions();
125 
126  if ($expressions) {
127  $rules = array_values($expressions);
128  foreach ($rules as $i => $rule) {
129  if (preg_match('/^[\W]{1}(.*)[\W]{1}(\w+)?$/', $rule['regexp'], $matches)) {
130  if (!empty($matches[2])) {
131  $pattern = sprintf("(?%s)%s", $matches[2], $matches[1]);
132  } else {
133  $pattern = $matches[1];
134  }
135  $if = $i == 0 ? 'if' : ' elsif';
136  $result .= sprintf($tpl, $if, $pattern, $rule['value']);
137  }
138  }
139  }
140 
141  return $result;
142  }
143 
154  private function getTransformedAccessList()
155  {
156  $tpl = " \"%s\";";
157  $result = array_reduce(
158  $this->getAccessList(),
159  function ($ips, $ip) use ($tpl) {
160  return $ips.sprintf($tpl, trim($ip)) . "\n";
161  },
162  ''
163  );
164  $result = rtrim($result, "\n");
165  return $result;
166  }
167 
173  private function getAccessList()
174  {
175  return $this->accessList;
176  }
177 
183  private function getBackendHost()
184  {
185  return $this->backendHost;
186  }
187 
193  private function getBackendPort()
194  {
195  return $this->backendPort;
196  }
197 
203  private function getGracePeriod()
204  {
205  return $this->gracePeriod;
206  }
207 
213  private function getSslOffloadedHeader()
214  {
215  return $this->sslOffloadedHeader;
216  }
217 
221  private function getDesignExceptions()
222  {
223  return $this->designExceptions;
224  }
225 }
$pattern
Definition: website.php:22
$expressions
Definition: side-menu.phtml:10
__construct(VclTemplateLocatorInterface $vclTemplateLocator, $backendHost, $backendPort, $accessList, $gracePeriod, $sslOffloadedHeader, $designExceptions=[])
$i
Definition: gallery.phtml:31
$template
Definition: export.php:12