CacheManager.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Framework\Interception\Config;
  8. /**
  9. * Interception cache manager.
  10. *
  11. * Responsible for handling interaction with compiled and uncompiled interception data
  12. */
  13. class CacheManager
  14. {
  15. /**
  16. * @var \Magento\Framework\Cache\FrontendInterface
  17. */
  18. private $cache;
  19. /**
  20. * @var \Magento\Framework\Serialize\SerializerInterface
  21. */
  22. private $serializer;
  23. /**
  24. * @var \Magento\Framework\App\ObjectManager\ConfigWriterInterface
  25. */
  26. private $configWriter;
  27. /**
  28. * @var \Magento\Framework\App\ObjectManager\ConfigLoader\Compiled
  29. */
  30. private $compiledLoader;
  31. /**
  32. * @param \Magento\Framework\Cache\FrontendInterface $cache
  33. * @param \Magento\Framework\Serialize\SerializerInterface $serializer
  34. * @param \Magento\Framework\App\ObjectManager\ConfigWriterInterface $configWriter
  35. * @param \Magento\Framework\App\ObjectManager\ConfigLoader\Compiled $compiledLoader
  36. */
  37. public function __construct(
  38. \Magento\Framework\Cache\FrontendInterface $cache,
  39. \Magento\Framework\Serialize\SerializerInterface $serializer,
  40. \Magento\Framework\App\ObjectManager\ConfigWriterInterface $configWriter,
  41. \Magento\Framework\App\ObjectManager\ConfigLoader\Compiled $compiledLoader
  42. ) {
  43. $this->cache = $cache;
  44. $this->serializer = $serializer;
  45. $this->configWriter = $configWriter;
  46. $this->compiledLoader = $compiledLoader;
  47. }
  48. /**
  49. * Load the interception config from cache
  50. *
  51. * @param string $key
  52. * @return array|null
  53. */
  54. public function load(string $key): ?array
  55. {
  56. if ($this->isCompiled($key)) {
  57. return $this->compiledLoader->load($key);
  58. }
  59. $intercepted = $this->cache->load($key);
  60. return $intercepted ? $this->serializer->unserialize($intercepted) : null;
  61. }
  62. /**
  63. * Save config to cache backend
  64. *
  65. * @param string $key
  66. * @param array $data
  67. */
  68. public function save(string $key, array $data)
  69. {
  70. $this->cache->save($this->serializer->serialize($data), $key);
  71. }
  72. /**
  73. * Save config to filesystem
  74. *
  75. * @param string $key
  76. * @param array $data
  77. */
  78. public function saveCompiled(string $key, array $data)
  79. {
  80. $this->configWriter->write($key, $data);
  81. }
  82. /**
  83. * Purge interception cache
  84. *
  85. * @param string $key
  86. */
  87. public function clean(string $key)
  88. {
  89. $this->cache->remove($key);
  90. }
  91. /**
  92. * Check for the compiled config with the generated metadata
  93. *
  94. * @param string $key
  95. * @return bool
  96. */
  97. private function isCompiled(string $key): bool
  98. {
  99. return file_exists(\Magento\Framework\App\ObjectManager\ConfigLoader\Compiled::getFilePath($key));
  100. }
  101. }