CacheManager.php 2.9 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

declare(strict_types=1);

namespace Magento\Framework\Interception\Config;

/**
 * Interception cache manager.
 *
 * Responsible for handling interaction with compiled and uncompiled interception data
 */
class CacheManager
{
    /**
     * @var \Magento\Framework\Cache\FrontendInterface
     */
    private $cache;

    /**
     * @var \Magento\Framework\Serialize\SerializerInterface
     */
    private $serializer;

    /**
     * @var \Magento\Framework\App\ObjectManager\ConfigWriterInterface
     */
    private $configWriter;

    /**
     * @var \Magento\Framework\App\ObjectManager\ConfigLoader\Compiled
     */
    private $compiledLoader;

    /**
     * @param \Magento\Framework\Cache\FrontendInterface $cache
     * @param \Magento\Framework\Serialize\SerializerInterface $serializer
     * @param \Magento\Framework\App\ObjectManager\ConfigWriterInterface $configWriter
     * @param \Magento\Framework\App\ObjectManager\ConfigLoader\Compiled $compiledLoader
     */
    public function __construct(
        \Magento\Framework\Cache\FrontendInterface $cache,
        \Magento\Framework\Serialize\SerializerInterface $serializer,
        \Magento\Framework\App\ObjectManager\ConfigWriterInterface $configWriter,
        \Magento\Framework\App\ObjectManager\ConfigLoader\Compiled $compiledLoader
    ) {
        $this->cache = $cache;
        $this->serializer = $serializer;
        $this->configWriter = $configWriter;
        $this->compiledLoader = $compiledLoader;
    }

    /**
     * Load the interception config from cache
     *
     * @param string $key
     * @return array|null
     */
    public function load(string $key): ?array
    {
        if ($this->isCompiled($key)) {
            return $this->compiledLoader->load($key);
        }

        $intercepted = $this->cache->load($key);
        return $intercepted ? $this->serializer->unserialize($intercepted) : null;
    }

    /**
     * Save config to cache backend
     *
     * @param string $key
     * @param array $data
     */
    public function save(string $key, array $data)
    {
        $this->cache->save($this->serializer->serialize($data), $key);
    }

    /**
     * Save config to filesystem
     *
     * @param string $key
     * @param array $data
     */
    public function saveCompiled(string $key, array $data)
    {
        $this->configWriter->write($key, $data);
    }

    /**
     * Purge interception cache
     *
     * @param string $key
     */
    public function clean(string $key)
    {
        $this->cache->remove($key);
    }

    /**
     * Check for the compiled config with the generated metadata
     *
     * @param string $key
     * @return bool
     */
    private function isCompiled(string $key): bool
    {
        return file_exists(\Magento\Framework\App\ObjectManager\ConfigLoader\Compiled::getFilePath($key));
    }
}