Varnish.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\PageCache\Model\System\Config\Backend;
  7. /**
  8. * Backend model for processing Varnish settings
  9. *
  10. * Class Varnish
  11. */
  12. class Varnish extends \Magento\Framework\App\Config\Value
  13. {
  14. /**
  15. * @var array
  16. */
  17. protected $defaultValues;
  18. /**
  19. * Set default data if empty fields have been left
  20. *
  21. * @return $this|\Magento\Framework\Model\AbstractModel
  22. * @throws \Magento\Framework\Exception\LocalizedException
  23. */
  24. public function beforeSave()
  25. {
  26. $data = $this->_getDefaultValues();
  27. $currentValue = $this->getValue();
  28. if (!$currentValue) {
  29. $replaceValue = isset($data[$this->getField()]) ? $data[$this->getField()] : false;
  30. $this->setValue($replaceValue);
  31. }
  32. return $this;
  33. }
  34. /**
  35. * Get Default Config Values
  36. *
  37. * @return array
  38. */
  39. protected function _getDefaultValues()
  40. {
  41. if (!$this->defaultValues) {
  42. $this->defaultValues = $this->_config->getValue('system/full_page_cache/default');
  43. }
  44. return $this->defaultValues;
  45. }
  46. /**
  47. * If fields are empty fill them with default data
  48. *
  49. * @return $this|\Magento\Framework\Model\AbstractModel
  50. */
  51. protected function _afterLoad()
  52. {
  53. $data = $this->_getDefaultValues();
  54. $currentValue = $this->getValue();
  55. if (!$currentValue) {
  56. foreach ($data as $field => $value) {
  57. if (strstr($this->getPath(), $field)) {
  58. $this->setValue($value);
  59. $this->save();
  60. break;
  61. }
  62. }
  63. }
  64. return $this;
  65. }
  66. }