1<?php 2 3 4namespace ComboStrap; 5 6 7class CacheLog 8{ 9 10 11 const TIMESTAMP_ATT = "timestamp"; 12 const EVENT_ATT = "event"; 13 const PATH_ATT = "path"; 14 const EXTENSION_ATT = "extension"; 15 const OPERATION_ATT = "operation"; 16 const MESSAGE_ATT = "message"; 17 const CACHE_LOG_TABLE = 'cache_log'; 18 const CACHE_LOG_ATTRIBUTES = [ 19 self::TIMESTAMP_ATT, 20 self::EVENT_ATT, 21 self::PATH_ATT, 22 self::EXTENSION_ATT, 23 self::OPERATION_ATT, 24 self::MESSAGE_ATT 25 ]; 26 27 public static function deleteCacheIfExistsAndLog(PageCompilerDocument $outputDocument, string $event, string $message) 28 { 29 $instructionsFile = $outputDocument->getCachePath(); 30 if (FileSystems::exists($instructionsFile)) { 31 FileSystems::delete($instructionsFile); 32 try { 33 CacheLog::logCacheEvent( 34 $event, 35 $outputDocument->getPage()->getPath()->toString(), 36 $outputDocument->getExtension(), 37 CacheManager::CACHE_DELETION, 38 $message 39 ); 40 } catch (ExceptionCombo $e) { 41 // should not fired 42 LogUtility::log2file("Error while logging cache event. Error: {$e->getMessage()}"); 43 } 44 } 45 } 46 47 public static function renderCacheAndLog(PageCompilerDocument $outputDocument, string $event, string $message) 48 { 49 $outputDocument->process(); 50 try { 51 CacheLog::logCacheEvent( 52 $event, 53 $outputDocument->getPage()->getPath()->toString(), 54 $outputDocument->getExtension(), 55 CacheManager::CACHE_CREATION, 56 $message 57 ); 58 } catch (ExceptionCombo $e) { 59 // should not fired 60 LogUtility::log2file("Error while logging cache event. Error: {$e->getMessage()}"); 61 } 62 } 63 64 /** 65 * @throws ExceptionCombo 66 */ 67 public static function logCacheEvent(string $event, string $path, string $format, string $operation, string $message) 68 { 69 70 71 $row = array( 72 self::TIMESTAMP_ATT => date("c"), 73 self::EVENT_ATT => $event, 74 self::PATH_ATT => $path, 75 self::EXTENSION_ATT => $format, 76 self::OPERATION_ATT => $operation, 77 self::MESSAGE_ATT => $message 78 ); 79 $request = Sqlite::createOrGetBackendSqlite() 80 ->createRequest() 81 ->setTableRow(self::CACHE_LOG_TABLE, $row); 82 try { 83 $request 84 ->execute(); 85 } finally { 86 $request->close(); 87 } 88 89 90 } 91 92 /** 93 * @throws ExceptionCombo 94 */ 95 public static function getCacheLog(): array 96 { 97 $sqlite = Sqlite::createOrGetBackendSqlite(); 98 if ($sqlite === null) { 99 throw new ExceptionCombo("Sqlite is not available"); 100 } 101 102 103 /** 104 * Execute 105 */ 106 $attributes[] = DatabasePageRow::ROWID; 107 $attributes = array_merge($attributes,self::CACHE_LOG_ATTRIBUTES); 108 $select = Sqlite::createSelectFromTableAndColumns(self::CACHE_LOG_TABLE, $attributes); 109 $request = $sqlite->createRequest() 110 ->setQuery($select); 111 try { 112 return $request->execute() 113 ->getRows(); 114 } catch (ExceptionCombo $e) { 115 throw new ExceptionCombo("Unable to get the cache log. Error:" . $e->getMessage(),self::CANONICAL,0,$e); 116 } finally { 117 $request->close(); 118 } 119 120 } 121 122} 123