. */ namespace SP\Infrastructure\File; use SP\Core\Exceptions\InvalidClassException; use function SP\__u; /** * Class FileCache * * @package SP\Infrastructure\File; */ class FileCache extends FileCacheBase { /** * @throws FileException * @throws InvalidClassException */ public function load(?string $path = null, ?string $class = null): mixed { $this->checkOrInitializePath($path); /** @noinspection UnserializeExploitsInspection */ $data = unserialize($this->path->checkIsReadable()->readToString()); if ($class && (!class_exists($class) || !($data instanceof $class))) { throw new InvalidClassException( sprintf(__u('Either class does not exist or file data cannot unserialized into: %s'), $class) ); } return $data; } /** * @throws FileException */ public function save(mixed $data, ?string $path = null): FileCacheInterface { $this->checkOrInitializePath($path); $this->createPath(); $this->path->checkIsWritable()->open('wb', true); $this->path->write(serialize($data))->close(); return $this; } }