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    const CANONICAL = "cache-results";
16
17    private $cacheResults;
18    /**
19     * @var string
20     */
21    private string $wikiId;
22
23    /**
24     * CacheReporter constructor.
25     * @param string $wikiId
26     */
27    public function __construct(string $wikiId)
28    {
29        $this->wikiId = $wikiId;
30    }
31
32    public function setData(\Doku_Event $event)
33    {
34
35
36        $cacheParser = $event->data;
37        /**
38         * Metadata and other rendering may occurs
39         * recursively in one request
40         *
41         * We record only the first one because the second call one will use the first
42         * one and overwrite the result
43         */
44        if (!isset($this->cacheResults[$cacheParser->mode])) {
45            $this->cacheResults[$cacheParser->mode] = (new CacheResult($cacheParser))
46                ->setResult($event->result);
47            /**
48             * Add snippet and output dependencies
49             */
50            if ($cacheParser->mode === FetcherMarkup::XHTML_MODE) {
51
52                $page = $cacheParser->page;
53                try {
54                    $markupFetcher = MarkupPath::createMarkupFromId($page)->createHtmlFetcherWithRequestedPathAsContextPath();
55                } catch (ExceptionNotExists $e) {
56                    // should not happen
57                    LogUtility::internalError("The executing path should exist as it's executed",self::CANONICAL, $e);
58                    return;
59                }
60
61                /**
62                 * @var CacheParser[] $cacheStores
63                 */
64                $cacheStores = [$markupFetcher->getSnippetCacheStore(), $markupFetcher->getDependenciesCacheStore()];
65                foreach ($cacheStores as $cacheStore) {
66                    if (file_exists($cacheStore->cache)) {
67                        $this->cacheResults[$cacheStore->mode] = (new CacheResult($cacheStore))
68                            ->setResult($event->result);
69                    }
70                }
71
72            }
73        }
74    }
75
76    /**
77     * @return CacheResult[]
78     */
79    public function getResults(): array
80    {
81        return $this->cacheResults;
82    }
83
84    public function hasResultForMode($mode): bool
85    {
86        return isset($this->cacheResults[$mode]);
87    }
88
89    /**
90     * @param string $mode
91     * @return null|CacheResult
92     */
93    public function getResultForMode(string $mode): ?CacheResult
94    {
95        return $this->cacheResults[$mode];
96    }
97
98    /**
99     * @return string
100     */
101    public function getWikiId(): string
102    {
103        return $this->wikiId;
104    }
105
106}
107