Create cache directory if it is missing

This commit is contained in:
grandeljay 2022-06-14 10:32:25 +02:00
parent c8940b2b9c
commit 6c637cd24c

View file

@ -19,6 +19,7 @@ class Cache
/** /**
* Protected * Protected
*/ */
protected string $url;
protected string $directory = ROOT . '/src/cache'; protected string $directory = ROOT . '/src/cache';
protected int $maxAge = 2592000; // 30 days protected int $maxAge = 2592000; // 30 days
@ -35,10 +36,15 @@ class Cache
protected function write(mixed $value): void protected function write(mixed $value): void
{ {
$filepath = $this->getFilepath(); $filepath = $this->getFilepath();
$directory = dirname($filepath); $directoryName = dirname($filepath);
$directoryCache = dirname($directoryName);
if (false === file_exists($directory)) { if (false === file_exists($directoryCache)) {
mkdir($directory); mkdir($directoryCache);
}
if (false === file_exists($directoryName)) {
mkdir($directoryName);
} }
file_put_contents($filepath, json_encode($value)); file_put_contents($filepath, json_encode($value));
@ -47,8 +53,9 @@ class Cache
/** /**
* Public * Public
*/ */
public function __construct(protected string $url) public function __construct($url)
{ {
$this->url = trim($url);
} }
public function exists(): bool public function exists(): bool