. */ namespace SP\Storage\File; /** * Class FileHandler * * @package SP\Storage\File; */ final class FileHandler { const CHUNK_LENGTH = 8192; /** * @var string */ protected $file; /** * @var resource */ protected $handle; /** * FileHandler constructor. * * @param string $file */ public function __construct(string $file) { $this->file = $file; } /** * Writes data into file * * @param $data * * @return FileHandler * @throws FileException */ public function write($data) { if (!is_resource($this->handle)) { $this->open('wb'); } if (@fwrite($this->handle, $data) === false) { throw new FileException(sprintf(__('No es posible escribir en el archivo (%s)'), $this->file)); } return $this; } /** * Opens the file * * @param $mode * * @return resource * @throws FileException */ public function open($mode = 'r') { if (($this->handle = @fopen($this->file, $mode)) === false) { throw new FileException(sprintf(__('No es posible abrir el archivo (%s)'), $this->file)); } return $this->handle; } /** * Reads data from file into a string * * @return string Data read from file * @throws FileException */ public function readToString(): string { if (($data = file_get_contents($this->file)) === false) { throw new FileException(sprintf(__('No es posible leer desde el archivo (%s)'), $this->file)); } return $data; } /** * Reads data from file into an array * * @throws FileException */ public function readToArray(): array { if (($data = @file($this->file, FILE_SKIP_EMPTY_LINES)) === false) { throw new FileException(sprintf(__('No es posible leer desde el archivo (%s)'), $this->file)); } return $data; } /** * Reads data from file * * @return string Data read from file * @throws FileException */ public function read() { if (!is_resource($this->handle)) { $this->open('rb'); } $data = ''; while (!feof($this->handle)) { $data .= fread($this->handle, self::CHUNK_LENGTH); } $this->close(); return $data; } /** * Closes the file * * @throws FileException * @return FileHandler */ public function close() { if (!is_resource($this->handle) || @fclose($this->handle) === false) { throw new FileException(sprintf(__('No es posible cerrar el archivo (%s)'), $this->file)); } return $this; } /** * Checks if the file is writable * * @throws FileException * @return FileHandler */ public function checkIsWritable() { if (!is_writable($this->file) && @touch($this->file) === false) { throw new FileException(sprintf(__('No es posible escribir el archivo (%s)'), $this->file)); } return $this; } /** * Checks if the file exists * * @throws FileException * @return FileHandler */ public function checkFileExists() { if (!file_exists($this->file)) { throw new FileException(sprintf(__('Archivo no encontrado (%s)'), $this->file)); } return $this; } /** * @return string */ public function getFile(): string { return $this->file; } /** * @param bool $isExceptionOnZero * * @return int * @throws FileException */ public function getFileSize($isExceptionOnZero = false): int { $size = filesize($this->file); if ($size === false || ($isExceptionOnZero === true && $size === 0)) { throw new FileException(sprintf(__('No es posible leer el archivo (%s)'), $this->file)); } return $size; } /** * Clears the stat cache for the given file * * @return FileHandler */ public function clearCache() { clearstatcache(true, $this->file); return $this; } /** * Deletes a file * * @return FileHandler * @throws FileException */ public function delete() { if (@unlink($this->file) === false) { throw new FileException(sprintf(__('No es posible eliminar el archivo (%s)'), $this->file)); } return $this; } /** * Returns the content type in MIME format * * @return string * @throws FileException */ public function getFileType(): string { $this->checkIsReadable(); return mime_content_type($this->file); } /** * Checks if the file is readable * * @throws FileException * @return FileHandler */ public function checkIsReadable() { if (!is_readable($this->file)) { throw new FileException(sprintf(__('No es posible leer el archivo (%s)'), $this->file)); } return $this; } }