177 lines
6.2 KiB
PHP
177 lines
6.2 KiB
PHP
<?php
|
|
|
|
namespace App;
|
|
|
|
use App\Blogs\Blogs;
|
|
use App\Utils\Debug;
|
|
|
|
class Cache {
|
|
|
|
static $pathCacheDir = [
|
|
'feed' => __DIR__ . '/../cache/feed',
|
|
'page' => __DIR__ . '/../cache/page',
|
|
'home' => __DIR__ . '/../cache/page',
|
|
'post' => __DIR__ . '/../cache/post',
|
|
'posts' => __DIR__ . '/../cache/posts',
|
|
'dev' => __DIR__ . '/../cache/page'
|
|
];
|
|
static $fileDir = __DIR__ . '/../datas';
|
|
static $templateDir = '../template';
|
|
|
|
/**
|
|
* Checks if a cache file exists for the given request URL
|
|
*
|
|
* @param string $requestUrl The requested URL to check for a cached version
|
|
* @param string $cacheDir The directory where the cache is stored, defaults to 'page'
|
|
* @return bool Returns true if the cache file exists; otherwise, false
|
|
*/
|
|
static function isCache(array $params): bool {
|
|
if (file_exists(self::$pathCacheDir[$params['type']] . '/' . $params['cacheName'])) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Checks if the page is valid based on the modification times of the markdown and cache files
|
|
*
|
|
* @param array $params An associative array
|
|
* @return bool Returns true if the cached page is still valid; otherwise, false
|
|
*/
|
|
static function isValidPage(array $params): bool {
|
|
$mdModifiedTime = 0;
|
|
// @todo add test if file exist
|
|
if ($params['type'] === 'home') {
|
|
$mdModifiedTime = filemtime(self::$templateDir . '/home.php');
|
|
} elseif ($params['type'] === 'post') {
|
|
$postList = new Blogs($params);
|
|
$postAttr = $postList->returnPostInfo($params['slug']);
|
|
$mdModifiedTime = filemtime($postAttr['file']);
|
|
} elseif ($params['type'] === 'posts') {
|
|
$mdModifiedTime = 0;
|
|
} elseif(file_exists(self::$fileDir . '/' . $params['type'] . 's' . $params['requestUrl'] . '.md')) {
|
|
$mdModifiedTime = filemtime(self::$fileDir . '/' . $params['type'] . 's' . $params['requestUrl'] . '.md');
|
|
}
|
|
$cacheModifiedTime = filemtime(self::$pathCacheDir[$params['type']] . '/' . $params['cacheName']);
|
|
|
|
if ($mdModifiedTime > $cacheModifiedTime) {
|
|
return false;
|
|
} else {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Checks if the feed is valid based on the feed's last modified timestamp
|
|
* and the cache file's modified time
|
|
*
|
|
* @param string $feedName The name of the feed
|
|
* @param int $feedTimestamp The last modified timestamp of the feed
|
|
* @return bool
|
|
*/
|
|
static function isValidFeed(string $feedName, int $feedTimestamp): bool {
|
|
$feedName = $feedName;
|
|
|
|
if (file_exists(self::$pathCacheDir['feed'] . '/' . $feedName . '.html')) {
|
|
$cacheModifiedTime = filemtime(self::$pathCacheDir['feed'] . '/' . $feedName . '.html');
|
|
if ($cacheModifiedTime > $feedTimestamp) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Validates the API by checking if the cache file exists and if its hash
|
|
* matches the provided API hash
|
|
*
|
|
* @param string $apiName The name of the API
|
|
* @param string $apiHash Hash of the API data
|
|
* @return bool
|
|
*/
|
|
static function isValidApi(string $apiName, string $apiHash): bool {
|
|
$apiName = $apiName;
|
|
|
|
if (file_exists(self::$pathCacheDir['feed'] . '/' . $apiName . '.html')) {
|
|
$cacheHashFile = sha1_file(self::$pathCacheDir['feed'] . '/' . $apiName . '.html');
|
|
|
|
if ($cacheHashFile === $apiHash) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Validates the API by checking if the cache file exists and if its hash
|
|
* matches the provided API hash
|
|
*
|
|
* @param string $apiName The name of the API
|
|
* @param string $apiHash Hash of the API data
|
|
* @return bool
|
|
*/
|
|
static function isValidConfig(): bool {
|
|
if (file_exists(__DIR__ . '/../datas/config.json')) {
|
|
$configLastMod = filemtime(__DIR__ . '/../datas/config.json');
|
|
if (file_exists(self::$pathCacheDir['home'] . '/home.html') && filemtime(self::$pathCacheDir['home'] . '/home.html') > $configLastMod) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
Cache::clearCache('posts');
|
|
Cache::clearCache('post');
|
|
Cache::clearCache('page');
|
|
Cache::clearCache('feed');
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Retrieves the contents of a cached file for the given request URL
|
|
*
|
|
* @param string $cacheName The requested URL to retrieve the cached content for
|
|
* @param string $cacheDir The directory where the cache is stored'
|
|
* @return string The contents of the cached file
|
|
*/
|
|
static function getCache(string $cacheName, string $cacheDir = 'page'): ?string {
|
|
$cacheFilename = self::$pathCacheDir[$cacheDir] . '/' . $cacheName;
|
|
if (file_exists($cacheFilename)) {
|
|
return file_get_contents($cacheFilename);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Stores the given HTML content in a cache file for the specified request URL
|
|
*
|
|
* @param string $cacheName The requested URL
|
|
* @param string $htmlPage The HTML content to be cached
|
|
* @param string $cacheDir The directory where the cache will be stored
|
|
* @return void
|
|
*/
|
|
static function setCache(string $cacheName, string $htmlPage, string $cacheDir = 'page'): void {
|
|
file_put_contents(self::$pathCacheDir[$cacheDir] . '/' . $cacheName, $htmlPage);
|
|
}
|
|
|
|
/**
|
|
* Clears all HTML cache files in the specified cache directory
|
|
*
|
|
* @return void
|
|
*/
|
|
static function clearCache($type): void {
|
|
array_map('unlink', glob(self::$pathCacheDir[$type] . "/*.html"));
|
|
}
|
|
|
|
/**
|
|
* Deletes the cache file for the specified request URL
|
|
*
|
|
* @param string $cacheName The URL for unvalidate cache
|
|
* @param string $cacheDir The directory where the cache is stored
|
|
* @return void
|
|
*/
|
|
static function unValidateCache(string $cacheName, string $cacheDir = 'page'): void {
|
|
if (file_exists(self::$pathCacheDir[$cacheDir] . '/' . $cacheName)) {
|
|
unlink(self::$pathCacheDir[$cacheDir] . '/' . $cacheName);
|
|
}
|
|
}
|
|
}
|