VclTemplateLocator.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\PageCache\Model\Varnish;
  7. use Magento\Framework\App\Config\ScopeConfigInterface;
  8. use Magento\Framework\Module\Dir;
  9. use Magento\Framework\Module\Dir\Reader;
  10. use Magento\Framework\Filesystem\Directory\ReadFactory;
  11. use Magento\PageCache\Model\VclTemplateLocatorInterface;
  12. use Magento\PageCache\Exception\UnsupportedVarnishVersion;
  13. class VclTemplateLocator implements VclTemplateLocatorInterface
  14. {
  15. /**
  16. * XML path to Varnish 5 config template path
  17. */
  18. const VARNISH_5_CONFIGURATION_PATH = 'system/full_page_cache/varnish5/path';
  19. /**
  20. * XML path to Varnish 4 config template path
  21. */
  22. const VARNISH_4_CONFIGURATION_PATH = 'system/full_page_cache/varnish4/path';
  23. /**
  24. *
  25. */
  26. const VARNISH_SUPPORTED_VERSION_4 = '4';
  27. /**
  28. *
  29. */
  30. const VARNISH_SUPPORTED_VERSION_5 = '5';
  31. /**
  32. * @var array
  33. */
  34. private $supportedVarnishVersions = [
  35. self::VARNISH_SUPPORTED_VERSION_4 => self::VARNISH_4_CONFIGURATION_PATH,
  36. self::VARNISH_SUPPORTED_VERSION_5 => self::VARNISH_5_CONFIGURATION_PATH,
  37. ];
  38. /**
  39. * @var Reader
  40. */
  41. private $reader;
  42. /**
  43. * @var ReadFactory
  44. */
  45. private $readFactory;
  46. /**
  47. * @var ScopeConfigInterface
  48. */
  49. private $scopeConfig;
  50. /**
  51. * VclTemplateLocator constructor.
  52. *
  53. * @param Reader $reader
  54. * @param ReadFactory $readFactory
  55. * @param ScopeConfigInterface $scopeConfig
  56. */
  57. public function __construct(Reader $reader, ReadFactory $readFactory, ScopeConfigInterface $scopeConfig)
  58. {
  59. $this->reader = $reader;
  60. $this->readFactory = $readFactory;
  61. $this->scopeConfig = $scopeConfig;
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function getTemplate($version)
  67. {
  68. $moduleEtcPath = $this->reader->getModuleDir(Dir::MODULE_ETC_DIR, 'Magento_PageCache');
  69. $configFilePath = $moduleEtcPath . '/' . $this->scopeConfig->getValue($this->getVclTemplatePath($version));
  70. $directoryRead = $this->readFactory->create($moduleEtcPath);
  71. $configFilePath = $directoryRead->getRelativePath($configFilePath);
  72. $template = $directoryRead->readFile($configFilePath);
  73. return $template;
  74. }
  75. /**
  76. * Get Vcl template path
  77. *
  78. * @param int $version Varnish version
  79. * @return string
  80. * @throws UnsupportedVarnishVersion
  81. */
  82. private function getVclTemplatePath($version)
  83. {
  84. if (!isset($this->supportedVarnishVersions[$version])) {
  85. throw new UnsupportedVarnishVersion(__('Unsupported varnish version'));
  86. }
  87. return $this->supportedVarnishVersions[$version];
  88. }
  89. }