xref: /plugin/combo/ComboStrap/CacheResults.php (revision 977ce05d19d8dab0a70c9a27f8da0b7039299e82)
1<?php
2
3
4namespace ComboStrap;
5
6use dokuwiki\Cache\CacheParser;
7
8/**
9 * Class CacheResults
10 * @package ComboStrap
11 */
12class CacheResults
13{
14
15    private $cacheResults;
16    /**
17     * @var string
18     */
19    private $wikiId;
20
21    /**
22     * CacheReporter constructor.
23     * @param string $wikiId
24     */
25    public function __construct(string $wikiId)
26    {
27        $this->wikiId = $wikiId;
28    }
29
30    public function setData(\Doku_Event $event)
31    {
32
33        $cacheParser = $event->data;
34        /**
35         * Metadata and other rendering may occurs
36         * recursively in one request
37         *
38         * We record only the first one because the second call one will use the first
39         * one and overwrite the result
40         */
41        if (!isset($this->cacheResults[$cacheParser->mode])) {
42            $this->cacheResults[$cacheParser->mode] = (new CacheResult($cacheParser))
43                ->setResult($event->result);
44            /**
45             * Add snippet and output dependencies
46             */
47            if ($cacheParser->mode === HtmlDocument::mode) {
48                $page = $cacheParser->page;
49                $htmlDocument = Page::createPageFromId($page)
50                    ->getHtmlDocument();
51
52                /**
53                 * @var CacheParser[] $cacheStores
54                 */
55                $cacheStores = [$htmlDocument->getSnippetCacheStore(), $htmlDocument->getDependenciesCacheStore()];
56                foreach ($cacheStores as $cacheStore) {
57                    if(file_exists($cacheStore->cache)) {
58                        $this->cacheResults[$cacheStore->mode] = (new CacheResult($cacheStore))
59                            ->setResult($event->result);
60                    }
61                }
62            }
63        }
64    }
65
66    /**
67     * @return CacheResult[]
68     */
69    public function getResults(): array
70    {
71        return $this->cacheResults;
72    }
73
74    public function hasResultForMode($mode): bool
75    {
76        return isset($this->cacheResults[$mode]);
77    }
78
79    /**
80     * @param string $mode
81     * @return null|CacheResult
82     */
83    public function getResultForMode(string $mode): ?CacheResult
84    {
85        return $this->cacheResults[$mode];
86    }
87
88}
89