State.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * An ultimate accessor to cache types' statuses
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\App\Cache;
  9. use Magento\Framework\App\DeploymentConfig;
  10. use Magento\Framework\App\DeploymentConfig\Writer;
  11. use Magento\Framework\Config\File\ConfigFilePool;
  12. /**
  13. * Cache State
  14. */
  15. class State implements StateInterface
  16. {
  17. /**
  18. * Disallow cache
  19. */
  20. const PARAM_BAN_CACHE = 'global_ban_use_cache';
  21. /**
  22. * Deployment config key
  23. */
  24. const CACHE_KEY = 'cache_types';
  25. /**
  26. * Deployment configuration
  27. *
  28. * @var DeploymentConfig
  29. */
  30. private $config;
  31. /**
  32. * Deployment configuration storage writer
  33. *
  34. * @var Writer
  35. */
  36. private $writer;
  37. /**
  38. * Associative array of cache type codes and their statuses (enabled/disabled)
  39. *
  40. * @var array
  41. */
  42. private $statuses;
  43. /**
  44. * Whether all cache types are forced to be disabled
  45. *
  46. * @var bool
  47. */
  48. private $banAll;
  49. /**
  50. * Constructor
  51. *
  52. * @param DeploymentConfig $config
  53. * @param Writer $writer
  54. * @param bool $banAll
  55. */
  56. public function __construct(DeploymentConfig $config, Writer $writer, $banAll = false)
  57. {
  58. $this->config = $config;
  59. $this->writer = $writer;
  60. $this->banAll = $banAll;
  61. }
  62. /**
  63. * Whether a cache type is enabled or not at the moment
  64. *
  65. * @param string $cacheType
  66. * @return bool
  67. */
  68. public function isEnabled($cacheType)
  69. {
  70. $this->load();
  71. return (bool)($this->statuses[$cacheType] ?? false);
  72. }
  73. /**
  74. * Enable/disable a cache type in run-time
  75. *
  76. * @param string $cacheType
  77. * @param bool $isEnabled
  78. * @return void
  79. */
  80. public function setEnabled($cacheType, $isEnabled)
  81. {
  82. $this->load();
  83. $this->statuses[$cacheType] = (int)$isEnabled;
  84. }
  85. /**
  86. * Save the current statuses (enabled/disabled) of cache types to the persistent storage
  87. *
  88. * @return void
  89. */
  90. public function persist()
  91. {
  92. $this->load();
  93. $this->writer->saveConfig([ConfigFilePool::APP_ENV => [self::CACHE_KEY => $this->statuses]]);
  94. }
  95. /**
  96. * Load statuses (enabled/disabled) of cache types
  97. *
  98. * @return void
  99. */
  100. private function load()
  101. {
  102. if (null === $this->statuses) {
  103. $this->statuses = [];
  104. if ($this->banAll) {
  105. return;
  106. }
  107. $this->statuses = $this->config->getConfigData(self::CACHE_KEY) ?: [];
  108. }
  109. }
  110. }