From a7467db7f2e5512b77dca051e8f22e721d52321f Mon Sep 17 00:00:00 2001 From: aVadim Date: Sat, 24 Apr 2021 12:24:06 +0300 Subject: [PATCH] Excel::log() is replaced with exceptions --- demo/output/.gitignore | 0 src/FastExcelWriter/Area.php | 2 ++ .../{ => Exception}/Exception.php | 2 +- .../Exception/SaveException.php | 15 +++++++++++++ src/FastExcelWriter/Writer.php | 21 +++++++++++-------- src/FastExcelWriter/WriterBuffer.php | 6 ++++-- 6 files changed, 34 insertions(+), 12 deletions(-) create mode 100644 demo/output/.gitignore rename src/FastExcelWriter/{ => Exception}/Exception.php (74%) create mode 100644 src/FastExcelWriter/Exception/SaveException.php diff --git a/demo/output/.gitignore b/demo/output/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/src/FastExcelWriter/Area.php b/src/FastExcelWriter/Area.php index be9be4b..4c10ada 100644 --- a/src/FastExcelWriter/Area.php +++ b/src/FastExcelWriter/Area.php @@ -2,6 +2,8 @@ namespace avadim\FastExcelWriter; +use avadim\FastExcelWriter\Exception\Exception; + /** * Class Area * diff --git a/src/FastExcelWriter/Exception.php b/src/FastExcelWriter/Exception/Exception.php similarity index 74% rename from src/FastExcelWriter/Exception.php rename to src/FastExcelWriter/Exception/Exception.php index 8931a70..f2ebb16 100644 --- a/src/FastExcelWriter/Exception.php +++ b/src/FastExcelWriter/Exception/Exception.php @@ -1,6 +1,6 @@ tempFilename())) { - Excel::log('Warning: tempdir ' . sys_get_temp_dir() . ' not writeable, use ->setTempDir()'); + throw new Exception('Warning: tempdir ' . sys_get_temp_dir() . ' is not writeable, use ->setTempDir()'); } if (!class_exists('\ZipArchive')) { - Excel::log('Error: ZipArchive class does not exist'); + throw new Exception('Error: ZipArchive class does not exist'); } $this->excel = $excel; $this->addCellStyle('GENERAL', null); @@ -201,22 +204,22 @@ public function saveToFile($fileName, $overWrite = true, $metadata = []) $this->writeSheetDataEnd($sheet);//making sure all footers have been written } + if (!is_dir(dirname($fileName))) { + throw new SaveException('Directory "' . dirname($fileName) . '" for output file is not exist.'); + } if (file_exists($fileName)) { if ($overWrite && is_writable($fileName)) { @unlink($fileName); //if the zip already exists, remove it } else { - Excel::log("Error in " . __CLASS__ . "::" . __FUNCTION__ . ", file is not writeable."); - return false; + throw new SaveException('File "' . $fileName. '" is not writeable'); } } $zip = new \ZipArchive(); if (empty($sheets)) { - Excel::log("Error in " . __CLASS__ . "::" . __FUNCTION__ . ", no worksheets defined."); - return false; + throw new SaveException('No worksheets defined'); } - if (!$zip->open($fileName, \ZipArchive::CREATE)) { - Excel::log("Error in " . __CLASS__ . "::" . __FUNCTION__ . ", unable to create zip."); - return false; + if (!$zip->open($fileName, \ZIPARCHIVE::CREATE)) { + throw new SaveException('Unable to create zip "' . $fileName. '"'); } $zip->addEmptyDir('docProps/'); diff --git a/src/FastExcelWriter/WriterBuffer.php b/src/FastExcelWriter/WriterBuffer.php index 82f5146..c7ac255 100644 --- a/src/FastExcelWriter/WriterBuffer.php +++ b/src/FastExcelWriter/WriterBuffer.php @@ -2,6 +2,8 @@ namespace avadim\FastExcelWriter; +use avadim\FastExcelWriter\Exception\Exception; + /** * Class WriterBuffer * @@ -63,11 +65,11 @@ public function flush($force = false) if (!$this->fd) { $this->fd = fopen($this->fileName, $this->openFlags); if ($this->fd === false) { - Excel::log("Unable to open {$this->fileName} for writing."); + throw new Exception("Unable to open {$this->fileName} for writing"); } } if ($this->checkUtf8 && !self::isValidUTF8($this->buffer)) { - Excel::log("Error, invalid UTF8 encoding detected"); + //Excel::log("Error, invalid UTF8 encoding detected"); $this->checkUtf8 = false; } fwrite($this->fd, $this->buffer);