1<?php 2 3 4namespace ComboStrap; 5 6 7use ComboStrap\Meta\Api\MetadataDateTime; 8use ComboStrap\Meta\Store\MetadataDokuWikiStore; 9use DateTime; 10 11/** 12 * Class CacheExpirationFrequencyMeta 13 * @package ComboStrap 14 * Represents the cache expiration date metadata 15 */ 16class CacheExpirationDate extends MetadataDateTime 17{ 18 19 20 /** 21 * The meta key that has the expiration date 22 */ 23 public const PROPERTY_NAME = "date_cache_expiration"; 24 25 26 public static function createForPage(ResourceCombo $page): CacheExpirationDate 27 { 28 return (new CacheExpirationDate()) 29 ->setResource($page); 30 } 31 32 /** 33 * 34 * @throws ExceptionNotFound - if their is no default value, the HTML document does not exists, the cache is disabled 35 * 36 */ 37 public function getDefaultValue(): DateTime 38 { 39 $resourceCombo = $this->getResource(); 40 if (!($resourceCombo instanceof MarkupPath)) { 41 throw new ExceptionNotFound("Cache expiration is only available for page fragment"); 42 } 43 44 /** 45 * We use {@link FetcherMarkup::getContentCachePath()} 46 * and not {@link FetcherMarkup::processIfNeededAndGetFetchPath()} 47 * to not create the HTML 48 */ 49 try { 50 $fetcherMarkup = $resourceCombo->createHtmlFetcherWithItselfAsContextPath(); 51 } catch (ExceptionNotExists $e) { 52 throw new ExceptionNotFound("The executing path does not exist."); 53 } 54 $path = $fetcherMarkup->getContentCachePath(); 55 if (!FileSystems::exists($path)) { 56 throw new ExceptionNotFound("There is no HTML document created to expire."); 57 } 58 59 60 $cacheIntervalInSecond = Site::getXhtmlCacheTime(); 61 if ($cacheIntervalInSecond === -1) { 62 throw new ExceptionNotFound("Cache has been disabled globally on the site"); 63 } 64 65 /** 66 * Not the modified time (it's modified by a process when the cache is read 67 * for whatever reason) 68 */ 69 $expirationTime = FileSystems::getCreationTime($path); 70 if ($cacheIntervalInSecond !== null) { 71 $expirationTime->modify('+' . $cacheIntervalInSecond . ' seconds'); 72 } 73 74 return $expirationTime; 75 76 } 77 78 79 /** 80 * @throws ExceptionNotFound 81 */ 82 public function getValue(): DateTime 83 { 84 85 try { 86 return parent::getValue(); 87 } catch (ExceptionNotFound $e) { 88 $cronExpression = CacheExpirationFrequency::createForPage($this->getResource())->getValue(); 89 try { 90 $value = Cron::getDate($cronExpression); 91 parent::setValue($value); 92 return $value; 93 } catch (ExceptionBadArgument $badArgument) { 94 throw $e; 95 } 96 } 97 98 } 99 100 101 public static function getName(): string 102 { 103 return self::PROPERTY_NAME; 104 } 105 106 107 public static function getPersistenceType(): string 108 { 109 return MetadataDokuWikiStore::CURRENT_METADATA; 110 } 111 112 113 public static function getCanonical(): string 114 { 115 return CacheExpirationFrequency::PROPERTY_NAME; 116 } 117 118 public static function getTab(): string 119 { 120 return MetaManagerForm::TAB_CACHE_VALUE; 121 } 122 123 public static function getDescription(): string 124 { 125 return "The next cache expiration date (calculated from the cache frequency expression)"; 126 } 127 128 public static function getLabel(): string 129 { 130 return "Cache Expiration Date"; 131 } 132 133 public static function isMutable(): bool 134 { 135 return false; 136 } 137 138 public static function isOnForm(): bool 139 { 140 return true; 141 } 142} 143