Manager.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\Cache;
  7. use Magento\Framework\App;
  8. /**
  9. * Cache status manager
  10. *
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Manager
  15. {
  16. /**
  17. * Cache types list
  18. *
  19. * @var TypeListInterface
  20. */
  21. private $cacheTypeList;
  22. /**
  23. * Cache state service
  24. *
  25. * @var StateInterface
  26. */
  27. private $cacheState;
  28. /**
  29. * Cache types pool
  30. *
  31. * @var Type\FrontendPool
  32. */
  33. private $pool;
  34. /**
  35. * Constructor
  36. *
  37. * @param TypeListInterface $cacheTypeList
  38. * @param StateInterface $cacheState
  39. * @param Type\FrontendPool $pool
  40. */
  41. public function __construct(
  42. TypeListInterface $cacheTypeList,
  43. StateInterface $cacheState,
  44. Type\FrontendPool $pool
  45. ) {
  46. $this->cacheTypeList = $cacheTypeList;
  47. $this->cacheState = $cacheState;
  48. $this->pool = $pool;
  49. }
  50. /**
  51. * Updates cache status for the requested types
  52. *
  53. * @param string[] $types
  54. * @param bool $isEnabled
  55. * @return array List of types with changed status
  56. */
  57. public function setEnabled(array $types, $isEnabled)
  58. {
  59. $changedStatusTypes = [];
  60. $isUpdated = false;
  61. foreach ($types as $type) {
  62. if ($this->cacheState->isEnabled($type) === $isEnabled) { // no need to poke it, if is not going to change
  63. continue;
  64. }
  65. $this->cacheState->setEnabled($type, $isEnabled);
  66. $isUpdated = true;
  67. $changedStatusTypes[] = $type;
  68. }
  69. if ($isUpdated) {
  70. $this->cacheState->persist();
  71. }
  72. return $changedStatusTypes;
  73. }
  74. /**
  75. * Cleans up caches
  76. *
  77. * @param array $types
  78. * @return void
  79. */
  80. public function clean(array $types)
  81. {
  82. foreach ($types as $type) {
  83. $this->cacheTypeList->cleanType($type);
  84. }
  85. }
  86. /**
  87. * Flushes specified cache storages
  88. *
  89. * @param string[] $types
  90. * @return void
  91. */
  92. public function flush(array $types)
  93. {
  94. $flushedBackend = [];
  95. foreach ($types as $type) {
  96. $backend = $this->pool->get($type)->getBackend();
  97. if (in_array($backend, $flushedBackend, true)) { // it was already flushed from another frontend
  98. continue;
  99. }
  100. $backend->clean();
  101. $flushedBackend[] = $backend;
  102. }
  103. }
  104. /**
  105. * Presents summary about cache status
  106. *
  107. * @return array
  108. */
  109. public function getStatus()
  110. {
  111. $result = [];
  112. foreach ($this->cacheTypeList->getTypes() as $type) {
  113. $result[$type['id']] = $type['status'];
  114. }
  115. return $result;
  116. }
  117. /**
  118. * @return array
  119. */
  120. public function getAvailableTypes()
  121. {
  122. $result = [];
  123. foreach ($this->cacheTypeList->getTypes() as $type) {
  124. $result[] = $type['id'];
  125. }
  126. return $result;
  127. }
  128. }