mirror of
https://github.com/php-win-ext/php-sdk-binary-tools.git
synced 2026-03-24 17:12:12 +01:00
Some are not as specific as they should be, and some may even be wrong, but since these are only type hints (not type declarations) they won't affect code execution, but rather provide some help to better understand the code – for developers as well as tooling.
101 lines
1.9 KiB
PHP
101 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace SDK;
|
|
|
|
class Cache
|
|
{
|
|
/** @var string */
|
|
protected $id;
|
|
|
|
/** @var string */
|
|
protected $hash;
|
|
|
|
public function __construct(string $id)
|
|
{/*{{{*/
|
|
$this->id = $id;
|
|
$this->hash = md5($id);
|
|
/* XXX pass as arg, fine for now. */
|
|
}/*}}}*/
|
|
|
|
protected function getCacheablePath(string $path, bool $relative = false) : string
|
|
{/*{{{*/
|
|
if ($relative) {
|
|
$dir = Config::getCacheDir();
|
|
$name = $path;
|
|
} else {
|
|
$dir = dirname($path);
|
|
$name = basename($path);
|
|
}
|
|
|
|
return $dir . DIRECTORY_SEPARATOR . $this->hash . "." . $name;
|
|
}/*}}}*/
|
|
|
|
public function fileIsCached(string $path, bool $relative = false) : bool
|
|
{/*{{{*/
|
|
return file_exists($this->getCacheablePath($path, $relative));
|
|
}/*}}}*/
|
|
|
|
/* TODO Sometimes a timestamp comparison might make sense. */
|
|
public function cachedContentDiffers(string $path, string $content, bool $relative = false) : bool
|
|
{/*{{{*/
|
|
$p = $this->getCacheablePath($path, $relative);
|
|
|
|
if (!file_exists($p)) {
|
|
return true;
|
|
}
|
|
|
|
$old_sum = md5_file($p);
|
|
$new_sum = md5($content);
|
|
|
|
return $old_sum !== $new_sum;
|
|
}/*}}}*/
|
|
|
|
public function cacheContent(string $path, string $content, bool $relative = false) : void
|
|
{/*{{{*/
|
|
$p = $this->getCacheablePath($path, $relative);
|
|
|
|
$to_write = strlen($content);
|
|
$wrote = 0;
|
|
|
|
$fd = fopen($p, "wb");
|
|
|
|
flock($fd, LOCK_EX);
|
|
|
|
do {
|
|
$got = fwrite($fd, substr($content, $wrote));
|
|
if (false === $got) {
|
|
break;
|
|
}
|
|
$wrote += $got;
|
|
} while ($wrote < $to_write);
|
|
|
|
flock($fd, LOCK_UN);
|
|
|
|
fclose($fd);
|
|
|
|
if ($to_write !== $wrote) {
|
|
throw new Exception("Couldn't cache '$p'");
|
|
}
|
|
}/*}}}*/
|
|
|
|
public function getCachedContent(string $path, bool $relative = false) : ?string
|
|
{/*{{{*/
|
|
$p = $this->getCacheablePath($path, $relative);
|
|
|
|
if (file_exists($p)) {
|
|
return file_get_contents($p);
|
|
}
|
|
|
|
return NULL;
|
|
}/*}}}*/
|
|
}
|
|
|
|
/*
|
|
* Local variables:
|
|
* tab-width: 4
|
|
* c-basic-offset: 4
|
|
* End:
|
|
* vim600: sw=4 ts=4 fdm=marker
|
|
* vim<600: sw=4 ts=4
|
|
*/
|