1<?php 2 3 4namespace ComboStrap; 5 6 7use ComboStrap\Xml\XmlDocument; 8 9class CacheReportHtmlDataBlockArray 10{ 11 12 const RESULT_STATUS = 'result'; 13 const DATE_MODIFIED = 'mtime'; 14 const CACHE_FILE = "file"; 15 16 /** 17 * Used when the cache data report 18 * are injected in the page in a json format 19 */ 20 public const APPLICATION_COMBO_CACHE_JSON = "application/combo+cache+json"; 21 const DEPENDENCY_ATT = "dependency"; 22 23 24 /** 25 * @return array - a array that will be transformed as json HTML data block 26 * to be included in a HTML page in order to insert cache results in the html page 27 */ 28 public static function getFromContext(): array 29 { 30 $cacheManager = ExecutionContext::getActualOrCreateFromEnv() 31 ->getCacheManager(); 32 $cacheReporters = $cacheManager->getCacheResults(); 33 $htmlDataBlock = []; 34 foreach ($cacheReporters as $cacheReporter) { 35 36 $cacheResults = $cacheReporter->getResults(); 37 foreach ($cacheResults as $result) { 38 39 $modifiedDate = ""; 40 if ($result->getPath() !== null) { 41 try { 42 $modifiedTime = FileSystems::getModifiedTime($result->getPath()); 43 $modifiedDate = $modifiedTime->format(Iso8601Date::getFormat()); 44 } catch (ExceptionNotFound $e) { 45 // the file exists 46 } 47 } 48 $mode = $result->getMode(); 49 $sourcePath = $result->getMarkupPath()->getPathObject(); 50 /** 51 * If this is not a wiki path, we try to transform it as wiki path 52 * to get a shorter path (ie id) in the report 53 */ 54 if (!($sourcePath instanceof WikiPath)) { 55 try { 56 $sourcePath = WikiPath::createFromPathObject($sourcePath); 57 } catch (ExceptionBadArgument $e) { 58 // could not be transformed as wiki path (missing a drive) 59 } 60 } 61 $cacheFile = $result->getPath(); 62 try { 63 $cacheFile = $cacheFile->toWikiPath(); 64 } catch (ExceptionBadArgument $e) { 65 LogUtility::error("Cache reporter: The cache file could not be transformed as a wiki path. Error: " . $e->getMessage()); 66 } 67 68 69 $data = [ 70 self::RESULT_STATUS => $result->getResult(), 71 self::DATE_MODIFIED => $modifiedDate, 72 self::CACHE_FILE => $cacheFile->toAbsoluteId() 73 ]; 74 75 if ($mode === FetcherMarkup::XHTML_MODE) { 76 try { 77 $dependencies = FetcherMarkup::createXhtmlMarkupFetcherFromPath($sourcePath, $sourcePath) 78 ->getOutputCacheDependencies() 79 ->getDependencies(); 80 } catch (ExceptionNotExists $e) { 81 continue; 82 } 83 $data[self::DEPENDENCY_ATT] = $dependencies; 84 } 85 86 $htmlDataBlock[$sourcePath->toAbsoluteId()][$mode] = $data; 87 88 } 89 90 } 91 return $htmlDataBlock; 92 } 93 94 95 /** 96 * An utility function to extract the cache data block from test responses 97 * @param XmlDocument $xmlDom 98 * @return mixed 99 * @throws ExceptionCompile 100 */ 101 public static function extractFromHtmlDom(XmlDocument $xmlDom) 102 { 103 $metaCacheMain = $xmlDom->querySelector('script[type="' . CacheReportHtmlDataBlockArray::APPLICATION_COMBO_CACHE_JSON . '"]'); 104 $cacheJsonTextValue = $metaCacheMain->getNodeValueWithoutCdata(); 105 return json_decode($cacheJsonTextValue, true); 106 } 107} 108