1<?php
2
3
4namespace ComboStrap;
5
6
7use dokuwiki\Cache\CacheParser;
8
9class CacheManager
10{
11
12    /**
13     * The meta key that has the expiration date
14     */
15    const DATE_CACHE_EXPIRATION_META_KEY = "date_cache_expiration";
16    const RESULT_STATUS = 'result';
17    const DATE_MODIFIED = 'ftime';
18
19    /**
20     * Just an utility variable to tracks the slot processed
21     * @var array the processed slot
22     */
23    private $cacheDataBySlots = array();
24
25
26    /**
27     * @return CacheManager
28     */
29    public static function get()
30    {
31        global $comboCacheManagerScript;
32        if (empty($comboCacheManagerScript)) {
33            self::init();
34        }
35        return $comboCacheManagerScript;
36    }
37
38    public static function init()
39    {
40        global $comboCacheManagerScript;
41        $comboCacheManagerScript = new CacheManager();
42
43    }
44
45    /**
46     * In test, we may run more than once
47     * This function delete the cache manager
48     * and is called when Dokuwiki close (ie {@link \action_plugin_combo_cache::close()})
49     */
50    public static function close()
51    {
52
53        global $comboCacheManagerScript;
54        unset($comboCacheManagerScript);
55
56    }
57
58    /**
59     * Keep track of the parsed bar (ie page in page)
60     * @param $pageId
61     * @param $result
62     * @param CacheParser $cacheParser
63     */
64    public function addSlot($pageId, $result, $cacheParser)
65    {
66        if (!isset($this->cacheDataBySlots[$pageId])) {
67            $this->cacheDataBySlots[$pageId] = [];
68        }
69        /**
70         * Metadata and other rendering may occurs
71         * recursively in one request
72         *
73         * We record only the first one because the second call one will use the first
74         * one
75         */
76        if (!isset($this->cacheDataBySlots[$pageId][$cacheParser->mode])) {
77            $date = null;
78            if(file_exists($cacheParser->cache)){
79                $date = Iso8601Date::createFromTimestamp(filemtime($cacheParser->cache))->getDateTime();
80            }
81            $this->cacheDataBySlots[$pageId][$cacheParser->mode] = [
82                self::RESULT_STATUS => $result,
83                self::DATE_MODIFIED => $date
84            ];
85        }
86
87    }
88
89    public function getXhtmlRenderCacheSlotResults()
90    {
91        $xhtmlRenderResult = [];
92        foreach ($this->cacheDataBySlots as $pageId => $modes) {
93            foreach($modes as $mode => $values) {
94                if ($mode === "xhtml") {
95                    $xhtmlRenderResult[$pageId] = $this->cacheDataBySlots[$pageId][$mode][self::RESULT_STATUS];
96                }
97            }
98        }
99        return $xhtmlRenderResult;
100    }
101
102    public function getCacheSlotResults()
103    {
104
105        return $this->cacheDataBySlots;
106    }
107
108    public function isCacheLogPresent($pageId, $mode)
109    {
110        return isset($this->cacheDataBySlots[$pageId][$mode]);
111    }
112
113
114}
115