Theme.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Model\Design\Backend;
  7. use Magento\Framework\App\Config\Value;
  8. class Theme extends Value
  9. {
  10. /**
  11. * Design package instance
  12. *
  13. * @var \Magento\Framework\View\DesignInterface
  14. */
  15. protected $_design = null;
  16. /**
  17. * Path to config node with list of caches
  18. *
  19. * @var string
  20. */
  21. const XML_PATH_INVALID_CACHES = 'design/invalid_caches';
  22. /**
  23. * Initialize dependencies
  24. *
  25. * @param \Magento\Framework\Model\Context $context
  26. * @param \Magento\Framework\Registry $registry
  27. * @param \Magento\Framework\App\Config\ScopeConfigInterface $config
  28. * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
  29. * @param \Magento\Framework\View\DesignInterface $design
  30. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  31. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  32. * @param array $data
  33. */
  34. public function __construct(
  35. \Magento\Framework\Model\Context $context,
  36. \Magento\Framework\Registry $registry,
  37. \Magento\Framework\App\Config\ScopeConfigInterface $config,
  38. \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
  39. \Magento\Framework\View\DesignInterface $design,
  40. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  41. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  42. array $data = []
  43. ) {
  44. $this->_design = $design;
  45. parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);
  46. }
  47. /**
  48. * Validate specified value against frontend area
  49. *
  50. * @return $this
  51. */
  52. public function beforeSave()
  53. {
  54. if ('' != $this->getValue()) {
  55. $design = clone $this->_design;
  56. $design->setDesignTheme($this->getValue(), \Magento\Framework\App\Area::AREA_FRONTEND);
  57. }
  58. return parent::beforeSave();
  59. }
  60. /**
  61. * Invalidate cache
  62. *
  63. * @param bool $forceInvalidate
  64. * @return void
  65. */
  66. protected function invalidateCache($forceInvalidate = false)
  67. {
  68. $types = array_keys(
  69. $this->_config->getValue(
  70. self::XML_PATH_INVALID_CACHES,
  71. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  72. )
  73. );
  74. if ($forceInvalidate || $this->isValueChanged()) {
  75. $this->cacheTypeList->invalidate($types);
  76. }
  77. }
  78. /**
  79. * @return array
  80. */
  81. public function getValue()
  82. {
  83. return $this->getData('value') !== null ? $this->getData('value') : '';
  84. }
  85. /**
  86. * {@inheritdoc}
  87. *
  88. * {@inheritdoc}. In addition, it sets status 'invalidate' for blocks and other output caches
  89. *
  90. * @return $this
  91. */
  92. public function afterSave()
  93. {
  94. $this->invalidateCache();
  95. return parent::afterSave();
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function afterDelete()
  101. {
  102. $this->invalidateCache(true);
  103. return parent::afterDelete();
  104. }
  105. }