. */ namespace SP\Storage; /** * Class FileCache * * @package SP\Storage */ class FileCache implements FileStorageInterface { /** * @param string $path * * @return mixed * @throws FileException */ public function load($path) { if (!file_exists($path)) { throw new FileException(sprintf(__('No es posible leer el archivo (%s)'), $path)); } if (!($data = file_get_contents($path))) { throw new FileException(sprintf(__('Error al leer datos del archivo (%s)'), $path)); } return unserialize($data); } /** * @param string $path * @param mixed $data * @return FileStorageInterface * @throws FileException */ public function save($path, $data) { $dir = dirname($path); if (!is_dir($dir) && mkdir($dir, 0700, true) === false) { throw new FileException(sprintf(__('No es posible crear el directorio (%s)'), $dir)); } if (file_exists($path) && !is_writable($path)) { throw new FileException(sprintf(__('No es posible escribir en el archivo (%s)'), $path)); } if (!file_put_contents($path, serialize($data))) { throw new FileException(sprintf(__('No es posible escribir en el archivo (%s)'), $path)); } return $this; } /** * @param string $path * * @return FileStorageInterface * @throws FileException */ public function delete($path) { if (file_exists($path) && !is_writable($path)) { throw new FileException(sprintf(__('No es posible abrir el archivo (%s)'), $path)); } if (!unlink($path)) { throw new FileException(sprintf(__('Error al eliminar el archivo (%s)'), $path)); } return $this; } /** * Returns whether the file is expired * * @param string $path * @param int $time * @return mixed */ public function isExpired($path, $time = 86400) { return !file_exists($path) || time() > filemtime($path) + $time; } }