1<?php
2
3
4namespace ComboStrap;
5
6
7class CacheReportHtmlDataBlockArray
8{
9
10    const RESULT_STATUS = 'result';
11    const DATE_MODIFIED = 'mtime';
12    const CACHE_FILE = "file";
13
14    /**
15     * Used when the cache data report
16     * are injected in the page in a json format
17     */
18    public const APPLICATION_COMBO_CACHE_JSON = "application/combo+cache+json";
19    const DEPENDENCY_ATT = "dependency";
20
21
22    /**
23     * @return array - a array that will be transformed as json HTML data block
24     * to be included in a HTML page in order to insert cache results in the html page
25     */
26    public static function getFromRuntime(): array
27    {
28        $cacheManager = CacheManager::getOrCreate();
29        $cacheReporters = $cacheManager->getCacheResults();
30        if ($cacheReporters === null) {
31            return [];
32        }
33        $htmlDataBlock = [];
34        foreach ($cacheReporters as $cacheReporter) {
35
36            foreach ($cacheReporter->getResults() as $result) {
37
38                $modifiedDate = "";
39                if ($result->getPath() !== null) {
40                    $modifiedTime = FileSystems::getModifiedTime($result->getPath());
41                    if ($modifiedTime !== null) {
42                        // the file exists
43                        $modifiedDate = $modifiedTime->format(Iso8601Date::getFormat());
44                    }
45                }
46                $mode = $result->getMode();
47                $slotId = $result->getSlotId();
48
49                $cacheFile = null;
50                try {
51                    $dokuPath = $result->getPath()->toDokuPath();
52                    $cacheFile = $dokuPath->getDokuwikiId();
53                } catch (ExceptionCombo $e) {
54                    LogUtility::msg("The path ({$result->getPath()}) could not be transformed as wiki path. Error:{$e->getMessage()}");
55                }
56                $data = [
57                    self::RESULT_STATUS => $result->getResult(),
58                    self::DATE_MODIFIED => $modifiedDate,
59                    self::CACHE_FILE => $cacheFile
60                ];
61
62                if ($mode === HtmlDocument::mode) {
63                    $dependencies = $cacheManager
64                        ->getCacheDependenciesForSlot($slotId)
65                        ->getDependencies();
66                    if ($dependencies !== null) {
67                        $data[self::DEPENDENCY_ATT] = $dependencies;
68                    }
69                }
70
71                $htmlDataBlock[$slotId][$mode] = $data;
72
73            }
74
75        }
76        return $htmlDataBlock;
77    }
78
79
80    /**
81     * An utility function to extract the cache data block from test responses
82     * @param \TestResponse $response
83     * @return mixed
84     * @throws ExceptionCombo
85     */
86    public static function extractFromResponse(\TestResponse $response)
87    {
88        $metaCacheMain = $response->queryHTML('script[type="' . CacheReportHtmlDataBlockArray::APPLICATION_COMBO_CACHE_JSON . '"]');
89        if ($metaCacheMain->count() != 1) {
90            throw new ExceptionCombo("The data cache was not found");
91        }
92        $cacheJsonTextValue = $metaCacheMain->elements[0]->childNodes->item(0)->textContent;
93        return json_decode(XmlUtility::extractTextWithoutCdata($cacheJsonTextValue), true);
94    }
95}
96