Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Abstract often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Abstract, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | abstract class Nexcessnet_Turpentine_Model_Varnish_Configurator_Abstract { |
||
|
|||
23 | |||
24 | const VCL_CUSTOM_C_CODE_FILE = 'uuid.c'; |
||
25 | |||
26 | /** |
||
27 | * Get the correct version of a configurator from a socket |
||
28 | * |
||
29 | * @param Nexcessnet_Turpentine_Model_Varnish_Admin_Socket $socket |
||
30 | * @return Nexcessnet_Turpentine_Model_Varnish_Configurator_Abstract |
||
31 | */ |
||
32 | static public function getFromSocket($socket) { |
||
33 | try { |
||
34 | $version = $socket->getVersion(); |
||
35 | } catch (Mage_Core_Exception $e) { |
||
36 | Mage::getSingleton('core/session') |
||
37 | ->addError('Error determining Varnish version: '. |
||
38 | $e->getMessage()); |
||
39 | return null; |
||
40 | } |
||
41 | switch ($version) { |
||
42 | case '4.0': |
||
43 | return Mage::getModel( |
||
44 | 'turpentine/varnish_configurator_version4', |
||
45 | array('socket' => $socket) ); |
||
46 | |||
47 | case '3.0': |
||
48 | return Mage::getModel( |
||
49 | 'turpentine/varnish_configurator_version3', |
||
50 | array('socket' => $socket) ); |
||
51 | case '2.1': |
||
52 | return Mage::getModel( |
||
53 | 'turpentine/varnish_configurator_version2', |
||
54 | array('socket' => $socket) ); |
||
55 | default: |
||
56 | Mage::throwException('Unsupported Varnish version'); |
||
57 | } |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * The socket this configurator is based on |
||
62 | * |
||
63 | * @var Nexcessnet_Turpentine_Model_Varnish_Admin_Socket |
||
64 | */ |
||
65 | protected $_socket = null; |
||
66 | /** |
||
67 | * options array |
||
68 | * |
||
69 | * @var array |
||
70 | */ |
||
71 | protected $_options = array( |
||
72 | 'vcl_template' => null, |
||
73 | ); |
||
74 | |||
75 | public function __construct($options = array()) { |
||
76 | $this->_options = array_merge($this->_options, $options); |
||
77 | } |
||
78 | |||
79 | abstract public function generate($doClean = true); |
||
80 | // abstract protected function _getTemplateVars(); |
||
81 | |||
82 | /** |
||
83 | * Save the generated config to the file specified in Magento config |
||
84 | * |
||
85 | * @param string $generatedConfig config generated by @generate |
||
86 | * @return null |
||
87 | */ |
||
88 | public function save($generatedConfig) { |
||
89 | $filename = $this->_getVclFilename(); |
||
90 | $dir = dirname($filename); |
||
91 | if ( ! is_dir($dir)) { |
||
92 | // this umask is probably redundant, but just in case... |
||
93 | if ( ! mkdir($dir, 0777 & ~umask(), true)) { |
||
94 | $err = error_get_last(); |
||
95 | return array(false, $err); |
||
96 | } |
||
97 | } |
||
98 | if (strlen($generatedConfig) !== |
||
99 | file_put_contents($filename, $generatedConfig)) { |
||
100 | $err = error_get_last(); |
||
101 | return array(false, $err); |
||
102 | } |
||
103 | return array(true, null); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Get the full path for a given template filename |
||
108 | * |
||
109 | * @param string $baseFilename |
||
110 | * @return string |
||
111 | */ |
||
112 | protected function _getVclTemplateFilename($baseFilename) { |
||
113 | $extensionDir = Mage::getModuleDir('', 'Nexcessnet_Turpentine'); |
||
114 | return sprintf('%s/misc/%s', $extensionDir, $baseFilename); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Get the name of the file to save the VCL to |
||
119 | * |
||
120 | * @return string |
||
121 | */ |
||
122 | protected function _getVclFilename() { |
||
123 | return $this->_formatTemplate( |
||
124 | Mage::getStoreConfig('turpentine_varnish/servers/config_file'), |
||
125 | array('root_dir' => Mage::getBaseDir()) ); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * Get the name of the custom include VCL file |
||
130 | * |
||
131 | * @return string |
||
132 | */ |
||
133 | protected function _getCustomIncludeFilename() { |
||
134 | return $this->_formatTemplate( |
||
135 | Mage::getStoreConfig('turpentine_varnish/servers/custom_include_file'), |
||
136 | array('root_dir' => Mage::getBaseDir()) ); |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Format a template string, replacing {{keys}} with the appropriate values |
||
141 | * and remove unspecified keys |
||
142 | * |
||
143 | * @param string $template template string to operate on |
||
144 | * @param array $vars array of key => value replacements |
||
145 | * @return string |
||
146 | */ |
||
147 | protected function _formatTemplate($template, array $vars) { |
||
148 | $needles = array_map(create_function('$k', 'return "{{".$k."}}";'), |
||
149 | array_keys($vars)); |
||
150 | $replacements = array_values($vars); |
||
151 | // do replacements, then delete unused template vars |
||
152 | return preg_replace('~{{[^}]+}}~', '', |
||
153 | str_replace($needles, $replacements, $template)); |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Format a VCL subroutine call |
||
158 | * |
||
159 | * @param string $subroutine subroutine name |
||
160 | * @return string |
||
161 | */ |
||
162 | protected function _vcl_call($subroutine) { |
||
163 | return sprintf('call %s;', $subroutine); |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Get the Magento admin frontname |
||
168 | * |
||
169 | * This is just the plain string, not in URL format. ex: |
||
170 | * http://example.com/magento/admin -> admin |
||
171 | * |
||
172 | * @return string |
||
173 | */ |
||
174 | protected function _getAdminFrontname() { |
||
175 | if (Mage::getStoreConfig('admin/url/use_custom_path')) { |
||
176 | return Mage::getStoreConfig('admin/url/custom_path'); |
||
177 | } else { |
||
178 | return (string) Mage::getConfig()->getNode( |
||
179 | 'admin/routers/adminhtml/args/frontName' ); |
||
180 | } |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * Get the hostname for host normalization from Magento's base URL |
||
185 | * |
||
186 | * @return string |
||
187 | */ |
||
188 | protected function _getNormalizeHostTarget() { |
||
189 | $configHost = trim(Mage::getStoreConfig( |
||
190 | 'turpentine_vcl/normalization/host_target' )); |
||
191 | if ($configHost) { |
||
192 | return $configHost; |
||
193 | } else { |
||
194 | $baseUrl = parse_url(Mage::getBaseUrl()); |
||
195 | if (isset($baseUrl['port'])) { |
||
196 | return sprintf('%s:%d', $baseUrl['host'], $baseUrl['port']); |
||
197 | } else { |
||
198 | return $baseUrl['host']; |
||
199 | } |
||
200 | } |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * Get hosts as regex |
||
205 | * |
||
206 | * ex: base_url: example.com |
||
207 | * path_regex: (example.com|example.net) |
||
208 | * |
||
209 | * @return string |
||
210 | */ |
||
211 | public function getAllowedHostsRegex() { |
||
212 | $hosts = array(); |
||
213 | foreach (Mage::app()->getStores() as $store) { |
||
214 | $hosts[] = parse_url($store->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB, false), PHP_URL_HOST); |
||
215 | } |
||
216 | |||
217 | $hosts = array_values(array_unique($hosts)); |
||
218 | |||
219 | $pattern = '('.implode('|', array_map("preg_quote", $hosts)).')'; |
||
220 | return $pattern; |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Get the Host normalization sub routine |
||
225 | * |
||
226 | * @return string |
||
227 | */ |
||
228 | protected function _vcl_sub_allowed_hosts_regex() { |
||
229 | $tpl = <<<EOS |
||
230 | # if host is not allowed in magento pass to backend |
||
231 | if (req.http.host !~ "{{allowed_hosts_regex}}") { |
||
232 | return (pass); |
||
233 | } |
||
234 | EOS; |
||
235 | return $this->_formatTemplate($tpl, array( |
||
236 | 'allowed_hosts_regex' => $this->getAllowedHostsRegex() )); |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * Get the base url path regex |
||
241 | * |
||
242 | * ex: base_url: http://example.com/magento/ |
||
243 | * path_regex: /magento/(?:(?:index|litespeed)\.php/)? |
||
244 | * |
||
245 | * @return string |
||
246 | */ |
||
247 | public function getBaseUrlPathRegex() { |
||
248 | $pattern = '^(%s)(?:(?:index|litespeed)\\.php/)?'; |
||
249 | return sprintf($pattern, implode('|', |
||
250 | array_map(create_function('$x', 'return preg_quote($x,"|");'), |
||
251 | $this->_getBaseUrlPaths()))); |
||
252 | } |
||
253 | |||
254 | /** |
||
255 | * Get the path part of each store's base URL and static file URLs |
||
256 | * |
||
257 | * @return array |
||
258 | */ |
||
259 | protected function _getBaseUrlPaths() { |
||
260 | $paths = array(); |
||
261 | $linkTypes = array(Mage_Core_Model_Store::URL_TYPE_LINK, |
||
262 | Mage_Core_Model_Store::URL_TYPE_JS, |
||
263 | Mage_Core_Model_Store::URL_TYPE_SKIN, |
||
264 | Mage_Core_Model_Store::URL_TYPE_MEDIA); |
||
265 | foreach (Mage::app()->getStores() as $store) { |
||
266 | foreach ($linkTypes as $linkType) { |
||
267 | $paths[] = parse_url($store->getBaseUrl($linkType, false), |
||
268 | PHP_URL_PATH); |
||
269 | $paths[] = parse_url($store->getBaseUrl($linkType, true), |
||
270 | PHP_URL_PATH); |
||
271 | } |
||
272 | } |
||
273 | $paths = array_unique($paths); |
||
274 | usort($paths, create_function('$a, $b', |
||
275 | 'return strlen( $b ) - strlen( $a );')); |
||
276 | return array_values($paths); |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * Format the URL exclusions for insertion in a regex. Admin frontname and |
||
281 | * API are automatically added. |
||
282 | * |
||
283 | * @return string |
||
284 | */ |
||
285 | protected function _getUrlExcludes() { |
||
286 | $urls = Mage::getStoreConfig('turpentine_vcl/urls/url_blacklist'); |
||
287 | return implode('|', array_merge(array($this->_getAdminFrontname(), 'api'), |
||
288 | Mage::helper('turpentine/data')->cleanExplode(PHP_EOL, $urls))); |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * Get the default cache TTL from Magento config |
||
293 | * |
||
294 | * @return string |
||
295 | */ |
||
296 | protected function _getDefaultTtl() { |
||
299 | |||
300 | /** |
||
301 | * Get the default backend configuration string |
||
302 | * |
||
303 | * @return string |
||
304 | */ |
||
305 | View Code Duplication | protected function _getDefaultBackend() { |
|
306 | $timeout = Mage::getStoreConfig('turpentine_vcl/backend/frontend_timeout'); |
||
307 | $default_options = array( |
||
308 | 'first_byte_timeout' => $timeout.'s', |
||
309 | 'between_bytes_timeout' => $timeout.'s', |
||
310 | ); |
||
311 | if (Mage::getStoreConfig('turpentine_vcl/backend/load_balancing') != 'no') { |
||
312 | return $this->_vcl_director('default', $default_options); |
||
313 | } else { |
||
314 | return $this->_vcl_backend('default', |
||
315 | Mage::getStoreConfig('turpentine_vcl/backend/backend_host'), |
||
316 | Mage::getStoreConfig('turpentine_vcl/backend/backend_port'), |
||
317 | $default_options); |
||
318 | } |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * Get the admin backend configuration string |
||
323 | * |
||
324 | * @return string |
||
325 | */ |
||
326 | View Code Duplication | protected function _getAdminBackend() { |
|
327 | $timeout = Mage::getStoreConfig('turpentine_vcl/backend/admin_timeout'); |
||
328 | $admin_options = array( |
||
329 | 'first_byte_timeout' => $timeout.'s', |
||
330 | 'between_bytes_timeout' => $timeout.'s', |
||
331 | ); |
||
332 | if (Mage::getStoreConfig('turpentine_vcl/backend/load_balancing') != 'no') { |
||
333 | return $this->_vcl_director('admin', $admin_options); |
||
334 | } else { |
||
335 | return $this->_vcl_backend('admin', |
||
336 | Mage::getStoreConfig('turpentine_vcl/backend/backend_host'), |
||
337 | Mage::getStoreConfig('turpentine_vcl/backend/backend_port'), |
||
338 | $admin_options); |
||
339 | } |
||
340 | } |
||
341 | |||
342 | /** |
||
343 | * Get the grace period for vcl_fetch |
||
344 | * |
||
345 | * This is curently hardcoded to 15 seconds, will be configurable at some |
||
346 | * point |
||
347 | * |
||
348 | * @return string |
||
349 | */ |
||
350 | protected function _getGracePeriod() { |
||
353 | |||
354 | /** |
||
355 | * Get whether debug headers should be enabled or not |
||
356 | * |
||
357 | * @return string |
||
358 | */ |
||
359 | protected function _getEnableDebugHeaders() { |
||
363 | |||
364 | /** |
||
365 | * Format the GET variable excludes for insertion in a regex |
||
366 | * |
||
367 | * @return string |
||
368 | */ |
||
369 | protected function _getGetParamExcludes() { |
||
370 | return implode('|', Mage::helper('turpentine/data')->cleanExplode(',', |
||
371 | Mage::getStoreConfig('turpentine_vcl/params/get_params'))); |
||
372 | } |
||
373 | |||
374 | protected function _getIgnoreGetParameters() |
||
375 | { |
||
376 | /** @var Nexcessnet_Turpentine_Helper_Data $helper */ |
||
377 | $helper = Mage::helper('turpentine'); |
||
378 | $ignoredParameters = $helper->cleanExplode(',', Mage::getStoreConfig('turpentine_vcl/params/ignore_get_params')); |
||
379 | return implode('|', $ignoredParameters); |
||
380 | } |
||
381 | |||
382 | /** |
||
383 | * @return boolean |
||
384 | */ |
||
385 | protected function _sendUnModifiedUrlToBackend() |
||
386 | { |
||
387 | return Mage::getStoreConfigFlag('turpentine_vcl/params/transfer_unmodified_url'); |
||
388 | } |
||
389 | |||
390 | /** |
||
391 | * Get the Generate Session |
||
392 | * |
||
393 | * @return string |
||
394 | */ |
||
395 | protected function _getGenerateSessionStart() { |
||
396 | return Mage::getStoreConfig('turpentine_varnish/general/vcl_fix') |
||
397 | ? '/* -- REMOVED' : ''; |
||
398 | } |
||
399 | |||
400 | /** |
||
401 | * Get the Generate Session |
||
402 | * |
||
403 | * @return string |
||
404 | */ |
||
405 | protected function _getGenerateSessionEnd() { |
||
406 | return Mage::getStoreConfig('turpentine_varnish/general/vcl_fix') |
||
407 | ? '-- */' : ''; |
||
408 | } |
||
409 | |||
410 | |||
411 | /** |
||
412 | * Get the Generate Session |
||
413 | * |
||
414 | * @return string |
||
415 | */ |
||
416 | protected function _getGenerateSession() { |
||
417 | return Mage::getStoreConfigFlag('turpentine_varnish/general/vcl_fix') |
||
418 | ? 'return (pipe);' : 'call generate_session;'; |
||
419 | } |
||
420 | |||
421 | |||
422 | /** |
||
423 | * Get the Generate Session Expires |
||
424 | * |
||
425 | * @return string |
||
426 | */ |
||
427 | protected function _getGenerateSessionExpires() { |
||
428 | return Mage::getStoreConfig('turpentine_varnish/general/vcl_fix') |
||
429 | ? '# call generate_session_expires' : 'call generate_session_expires;'; |
||
430 | } |
||
431 | |||
432 | /** |
||
433 | * Get the Force Static Caching option |
||
434 | * |
||
435 | * @return string |
||
436 | */ |
||
437 | protected function _getForceCacheStatic() { |
||
441 | |||
442 | /** |
||
443 | * Get the Force Static Caching option |
||
444 | * |
||
445 | * @return string |
||
446 | */ |
||
447 | protected function _getSimpleHashStatic() { |
||
448 | return Mage::getStoreConfig('turpentine_vcl/static/simple_hash') |
||
449 | ? 'true' : 'false'; |
||
450 | } |
||
451 | |||
452 | /** |
||
453 | * Format the list of static cache extensions |
||
454 | * |
||
455 | * @return string |
||
456 | */ |
||
457 | protected function _getStaticExtensions() { |
||
458 | return implode('|', Mage::helper('turpentine/data')->cleanExplode(',', |
||
459 | Mage::getStoreConfig('turpentine_vcl/static/exts'))); |
||
460 | } |
||
461 | |||
462 | /** |
||
463 | * Get the static caching TTL |
||
464 | * |
||
465 | * @return string |
||
466 | */ |
||
467 | protected function _getStaticTtl() { |
||
470 | |||
471 | /** |
||
472 | * Format the by-url TTL value list |
||
473 | * |
||
474 | * @return string |
||
475 | */ |
||
476 | protected function _getUrlTtls() { |
||
477 | $str = array(); |
||
478 | $configTtls = Mage::helper('turpentine/data')->cleanExplode(PHP_EOL, |
||
479 | Mage::getStoreConfig('turpentine_vcl/ttls/url_ttls')); |
||
480 | $ttls = array(); |
||
481 | foreach ($configTtls as $line) { |
||
482 | $ttls[] = explode(',', trim($line)); |
||
483 | } |
||
484 | foreach ($ttls as $ttl) { |
||
485 | $str[] = sprintf('if (bereq.url ~ "%s%s") { set beresp.ttl = %ds; }', |
||
486 | $this->getBaseUrlPathRegex(), $ttl[0], $ttl[1]); |
||
487 | } |
||
488 | $str = implode(' else ', $str); |
||
489 | if ($str) { |
||
490 | $str .= sprintf(' else { set beresp.ttl = %ds; }', |
||
491 | $this->_getDefaultTtl()); |
||
492 | } else { |
||
493 | $str = sprintf('set beresp.ttl = %ds;', $this->_getDefaultTtl()); |
||
494 | } |
||
495 | return $str; |
||
496 | } |
||
497 | |||
498 | /** |
||
499 | * Get the Enable Caching value |
||
500 | * |
||
501 | * @return string |
||
502 | */ |
||
503 | protected function _getEnableCaching() { |
||
507 | |||
508 | /** |
||
509 | * Get the list of allowed debug IPs |
||
510 | * |
||
511 | * @return array |
||
512 | */ |
||
513 | protected function _getDebugIps() { |
||
514 | return Mage::helper('turpentine/data')->cleanExplode(',', |
||
515 | Mage::getStoreConfig('dev/restrict/allow_ips')); |
||
516 | } |
||
517 | |||
518 | /** |
||
519 | * Get the list of crawler IPs |
||
520 | * |
||
521 | * @return array |
||
522 | */ |
||
523 | protected function _getCrawlerIps() { |
||
524 | return Mage::helper('turpentine/data')->cleanExplode(',', |
||
525 | Mage::getStoreConfig('turpentine_vcl/backend/crawlers')); |
||
526 | } |
||
527 | |||
528 | /** |
||
529 | * Get the regex formatted list of crawler user agents |
||
530 | * |
||
531 | * @return string |
||
532 | */ |
||
533 | protected function _getCrawlerUserAgents() { |
||
534 | return implode('|', Mage::helper('turpentine/data') |
||
535 | ->cleanExplode(',', |
||
536 | Mage::getStoreConfig( |
||
537 | 'turpentine_vcl/backend/crawler_user_agents' ))); |
||
538 | } |
||
539 | |||
540 | /** |
||
541 | * Get the time to increase a cached objects TTL on cache hit (in seconds). |
||
542 | * |
||
543 | * This should be set very low since it gets added to every hit. |
||
544 | * |
||
545 | * @return string |
||
546 | */ |
||
547 | protected function _getLruFactor() { |
||
550 | |||
551 | /** |
||
552 | * Get the advanced session validation restrictions |
||
553 | * |
||
554 | * Note that if User-Agent Normalization is on then the normalized user-agent |
||
555 | * is used for user-agent validation instead of the full user-agent |
||
556 | * |
||
557 | * @return string |
||
558 | */ |
||
559 | protected function _getAdvancedSessionValidationTargets() { |
||
560 | $validation = array(); |
||
561 | if (Mage::getStoreConfig('web/session/use_remote_addr')) { |
||
562 | $validation[] = 'client.ip'; |
||
563 | } |
||
564 | if (Mage::getStoreConfig('web/session/use_http_via')) { |
||
565 | $validation[] = 'req.http.Via'; |
||
566 | } |
||
567 | if (Mage::getStoreConfig('web/session/use_http_x_forwarded_for')) { |
||
568 | $validation[] = 'req.http.X-Forwarded-For'; |
||
569 | } |
||
570 | if (Mage::getStoreConfig( |
||
571 | 'web/session/use_http_user_agent' ) && |
||
572 | ! Mage::getStoreConfig( |
||
573 | 'turpentine_vcl/normalization/user_agent' )) { |
||
574 | $validation[] = 'req.http.User-Agent'; |
||
575 | } |
||
576 | return $validation; |
||
577 | } |
||
578 | |||
579 | /** |
||
580 | * Remove empty and commented out lines from the generated VCL |
||
581 | * |
||
582 | * @param string $dirtyVcl generated vcl |
||
583 | * @return string |
||
584 | */ |
||
585 | protected function _cleanVcl($dirtyVcl) { |
||
586 | return implode(PHP_EOL, |
||
587 | array_filter( |
||
588 | Mage::helper('turpentine/data') |
||
589 | ->cleanExplode(PHP_EOL, $dirtyVcl), |
||
590 | array($this, '_cleanVclHelper') |
||
591 | ) |
||
592 | ); |
||
593 | } |
||
594 | |||
595 | /** |
||
596 | * Helper to filter out blank/commented lines for VCL cleaning |
||
597 | * |
||
598 | * @param string $line |
||
599 | * @return bool |
||
600 | */ |
||
601 | protected function _cleanVclHelper($line) { |
||
602 | return $line && |
||
603 | ((substr($line, 0, 1) != '#' && |
||
604 | substr($line, 0, 2) != '//') || |
||
605 | substr($line, 0, 8) == '#include'); |
||
606 | } |
||
607 | |||
608 | /** |
||
609 | * Format a VCL backend declaration |
||
610 | * |
||
611 | * @param string $name name of the backend |
||
612 | * @param string $host backend host |
||
613 | * @param string $port backend port |
||
614 | * @param array $options options |
||
615 | * @return string |
||
616 | */ |
||
617 | protected function _vcl_backend($name, $host, $port, $options = array()) { |
||
618 | $tpl = <<<EOS |
||
619 | backend {{name}} { |
||
620 | .host = "{{host}}"; |
||
621 | .port = "{{port}}"; |
||
622 | |||
623 | EOS; |
||
624 | $vars = array( |
||
625 | 'host' => $host, |
||
626 | 'port' => $port, |
||
627 | 'name' => $name, |
||
628 | ); |
||
629 | $str = $this->_formatTemplate($tpl, $vars); |
||
630 | foreach ($options as $key => $value) { |
||
631 | $str .= sprintf(' .%s = %s;', $key, $value).PHP_EOL; |
||
632 | } |
||
633 | $str .= '}'.PHP_EOL; |
||
634 | return $str; |
||
635 | } |
||
636 | |||
637 | /** |
||
638 | * Format a VCL director declaration, for load balancing |
||
639 | * |
||
640 | * @param string $name name of the director, also used to select config settings |
||
641 | * @param array $backendOptions options for each backend |
||
642 | * @return string |
||
643 | */ |
||
644 | protected function _vcl_director($name, $backendOptions) { |
||
645 | $tpl = <<<EOS |
||
646 | director {{name}} round-robin { |
||
647 | {{backends}} |
||
648 | } |
||
649 | EOS; |
||
650 | if ('admin' == $name && 'yes_admin' == Mage::getStoreConfig('turpentine_vcl/backend/load_balancing')) { |
||
651 | $backendNodes = Mage::helper('turpentine/data')->cleanExplode(PHP_EOL, |
||
652 | Mage::getStoreConfig('turpentine_vcl/backend/backend_nodes_admin')); |
||
653 | $probeUrl = Mage::getStoreConfig('turpentine_vcl/backend/backend_probe_url_admin'); |
||
654 | } else { |
||
655 | $backendNodes = Mage::helper('turpentine/data')->cleanExplode(PHP_EOL, |
||
656 | Mage::getStoreConfig('turpentine_vcl/backend/backend_nodes')); |
||
657 | $probeUrl = Mage::getStoreConfig('turpentine_vcl/backend/backend_probe_url'); |
||
658 | } |
||
659 | $backends = ''; |
||
660 | foreach ($backendNodes as $backendNode) { |
||
661 | $parts = explode(':', $backendNode, 2); |
||
662 | $host = (empty($parts[0])) ? '127.0.0.1' : $parts[0]; |
||
663 | $port = (empty($parts[1])) ? '80' : $parts[1]; |
||
664 | $backends .= $this->_vcl_director_backend($host, $port, $probeUrl, $backendOptions); |
||
665 | } |
||
666 | $vars = array( |
||
667 | 'name' => $name, |
||
668 | 'backends' => $backends |
||
669 | ); |
||
670 | return $this->_formatTemplate($tpl, $vars); |
||
671 | } |
||
672 | |||
673 | /** |
||
674 | * Format a VCL backend declaration to put inside director |
||
675 | * |
||
676 | * @param string $host backend host |
||
677 | * @param string $port backend port |
||
678 | * @param string $probeUrl URL to check if backend is up |
||
679 | * @param array $options extra options for backend |
||
680 | * @return string |
||
681 | */ |
||
682 | protected function _vcl_director_backend($host, $port, $probeUrl = '', $options = array()) { |
||
683 | $tpl = <<<EOS |
||
684 | { |
||
685 | .backend = { |
||
686 | .host = "{{host}}"; |
||
687 | .port = "{{port}}"; |
||
688 | {{probe}} |
||
689 | |||
690 | EOS; |
||
691 | $vars = array( |
||
692 | 'host' => $host, |
||
693 | 'port' => $port, |
||
694 | 'probe' => '' |
||
695 | ); |
||
696 | if ( ! empty($probeUrl)) { |
||
697 | $vars['probe'] = $this->_vcl_get_probe($probeUrl); |
||
698 | } |
||
699 | $str = $this->_formatTemplate($tpl, $vars); |
||
700 | foreach ($options as $key => $value) { |
||
701 | $str .= sprintf(' .%s = %s;', $key, $value).PHP_EOL; |
||
702 | } |
||
703 | $str .= <<<EOS |
||
704 | } |
||
705 | } |
||
706 | EOS; |
||
707 | return $str; |
||
708 | } |
||
709 | |||
710 | /** |
||
711 | * Format a VCL probe declaration to put in backend which is in director |
||
712 | * |
||
713 | * @param string $probeUrl URL to check if backend is up |
||
714 | * @return string |
||
715 | */ |
||
716 | protected function _vcl_get_probe($probeUrl) { |
||
717 | $urlParts = parse_url($probeUrl); |
||
718 | if (empty($urlParts)) { |
||
719 | // Malformed URL |
||
720 | return ''; |
||
721 | } else { |
||
722 | $tpl = <<<EOS |
||
723 | .probe = { |
||
724 | .request = |
||
725 | "GET {{probe_path}} HTTP/1.1" |
||
726 | "Host: {{probe_host}}" |
||
727 | "Connection: close"; |
||
728 | } |
||
729 | EOS; |
||
730 | $vars = array( |
||
731 | 'probe_host' => $urlParts['host'], |
||
732 | 'probe_path' => $urlParts['path'] |
||
733 | ); |
||
734 | return $this->_formatTemplate($tpl, $vars); |
||
735 | } |
||
736 | } |
||
737 | |||
738 | /** |
||
739 | * Format a VCL ACL declaration |
||
740 | * |
||
741 | * @param string $name ACL name |
||
742 | * @param array $hosts list of hosts to add to the ACL |
||
743 | * @return string |
||
744 | */ |
||
745 | protected function _vcl_acl($name, array $hosts) { |
||
746 | $tpl = <<<EOS |
||
747 | acl {{name}} { |
||
748 | {{hosts}} |
||
749 | } |
||
750 | EOS; |
||
751 | $fmtHost = create_function('$h', 'return sprintf(\'"%s";\',$h);'); |
||
752 | $vars = array( |
||
753 | 'name' => $name, |
||
754 | 'hosts' => implode("\n ", array_map($fmtHost, $hosts)), |
||
755 | ); |
||
756 | return $this->_formatTemplate($tpl, $vars); |
||
757 | } |
||
758 | |||
759 | /** |
||
760 | * Get the User-Agent normalization sub routine |
||
761 | * |
||
762 | * @return string |
||
763 | */ |
||
764 | protected function _vcl_sub_normalize_user_agent() { |
||
789 | |||
790 | /** |
||
791 | * Get the Accept-Encoding normalization sub routine |
||
792 | * |
||
793 | * @return string |
||
794 | */ |
||
795 | protected function _vcl_sub_normalize_encoding() { |
||
811 | |||
812 | /** |
||
813 | * Get the Host normalization sub routine |
||
814 | * |
||
815 | * @return string |
||
816 | */ |
||
817 | protected function _vcl_sub_normalize_host() { |
||
818 | $tpl = <<<EOS |
||
819 | set req.http.Host = "{{normalize_host_target}}"; |
||
820 | |||
821 | EOS; |
||
822 | return $this->_formatTemplate($tpl, array( |
||
823 | 'normalize_host_target' => $this->_getNormalizeHostTarget() )); |
||
824 | } |
||
825 | |||
826 | /** |
||
827 | * Get the hostname for cookie normalization |
||
828 | * |
||
829 | * @return string |
||
830 | */ |
||
831 | protected function _getNormalizeCookieTarget() { |
||
832 | return trim(Mage::getStoreConfig( |
||
833 | 'turpentine_vcl/normalization/cookie_target' )); |
||
834 | } |
||
835 | |||
836 | /** |
||
837 | * Get the regex for cookie normalization |
||
838 | * |
||
839 | * @return string |
||
840 | */ |
||
841 | protected function _getNormalizeCookieRegex() { |
||
842 | return trim(Mage::getStoreConfig( |
||
843 | 'turpentine_vcl/normalization/cookie_regex' )); |
||
844 | } |
||
845 | |||
846 | /** |
||
847 | * Get the allowed IPs when in maintenance mode |
||
848 | * |
||
849 | * @return string |
||
850 | */ |
||
851 | View Code Duplication | protected function _vcl_sub_maintenance_allowed_ips() { |
|
852 | if (( ! $this->_getDebugIps()) || ! Mage::getStoreConfig('turpentine_vcl/maintenance/custom_vcl_synth')) { |
||
889 | |||
890 | /** |
||
891 | * Get the allowed IPs when in maintenance mode |
||
892 | * |
||
893 | * @return string |
||
894 | */ |
||
895 | View Code Duplication | protected function _vcl_sub_synth() |
|
929 | |||
930 | |||
931 | |||
932 | /** |
||
933 | * Build the list of template variables to apply to the VCL template |
||
934 | * |
||
935 | * @return array |
||
936 | */ |
||
937 | protected function _getTemplateVars() { |
||
1017 | } |
||
1018 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.