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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\PageCache\Model\Varnish;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Module\Dir;
use Magento\Framework\Module\Dir\Reader;
use Magento\Framework\Filesystem\Directory\ReadFactory;
use Magento\PageCache\Model\VclTemplateLocatorInterface;
use Magento\PageCache\Exception\UnsupportedVarnishVersion;
class VclTemplateLocator implements VclTemplateLocatorInterface
{
/**
* XML path to Varnish 5 config template path
*/
const VARNISH_5_CONFIGURATION_PATH = 'system/full_page_cache/varnish5/path';
/**
* XML path to Varnish 4 config template path
*/
const VARNISH_4_CONFIGURATION_PATH = 'system/full_page_cache/varnish4/path';
/**
*
*/
const VARNISH_SUPPORTED_VERSION_4 = '4';
/**
*
*/
const VARNISH_SUPPORTED_VERSION_5 = '5';
/**
* @var array
*/
private $supportedVarnishVersions = [
self::VARNISH_SUPPORTED_VERSION_4 => self::VARNISH_4_CONFIGURATION_PATH,
self::VARNISH_SUPPORTED_VERSION_5 => self::VARNISH_5_CONFIGURATION_PATH,
];
/**
* @var Reader
*/
private $reader;
/**
* @var ReadFactory
*/
private $readFactory;
/**
* @var ScopeConfigInterface
*/
private $scopeConfig;
/**
* VclTemplateLocator constructor.
*
* @param Reader $reader
* @param ReadFactory $readFactory
* @param ScopeConfigInterface $scopeConfig
*/
public function __construct(Reader $reader, ReadFactory $readFactory, ScopeConfigInterface $scopeConfig)
{
$this->reader = $reader;
$this->readFactory = $readFactory;
$this->scopeConfig = $scopeConfig;
}
/**
* {@inheritdoc}
*/
public function getTemplate($version)
{
$moduleEtcPath = $this->reader->getModuleDir(Dir::MODULE_ETC_DIR, 'Magento_PageCache');
$configFilePath = $moduleEtcPath . '/' . $this->scopeConfig->getValue($this->getVclTemplatePath($version));
$directoryRead = $this->readFactory->create($moduleEtcPath);
$configFilePath = $directoryRead->getRelativePath($configFilePath);
$template = $directoryRead->readFile($configFilePath);
return $template;
}
/**
* Get Vcl template path
*
* @param int $version Varnish version
* @return string
* @throws UnsupportedVarnishVersion
*/
private function getVclTemplatePath($version)
{
if (!isset($this->supportedVarnishVersions[$version])) {
throw new UnsupportedVarnishVersion(__('Unsupported varnish version'));
}
return $this->supportedVarnishVersions[$version];
}
}