Skip to content

Commit

Permalink
Excel::log() is replaced with exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
aVadim483 committed Apr 24, 2021
1 parent b564074 commit a7467db
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 12 deletions.
Empty file added demo/output/.gitignore
Empty file.
2 changes: 2 additions & 0 deletions src/FastExcelWriter/Area.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace avadim\FastExcelWriter;

use avadim\FastExcelWriter\Exception\Exception;

/**
* Class Area
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace avadim\FastExcelWriter;
namespace avadim\FastExcelWriter\Exception;

/**
* Class Exception
Expand Down
15 changes: 15 additions & 0 deletions src/FastExcelWriter/Exception/SaveException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace avadim\FastExcelWriter\Exception;

/**
* Class Exception
*
* @package avadim\FastExcelWriter
*/
class SaveException extends Exception
{

}

// EOF
21 changes: 12 additions & 9 deletions src/FastExcelWriter/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace avadim\FastExcelWriter;

use avadim\FastExcelWriter\Exception\Exception;
use avadim\FastExcelWriter\Exception\SaveException;

/**
* Class Writer
*
Expand Down Expand Up @@ -34,10 +37,10 @@ public function __construct($excel)
{
date_default_timezone_get() || date_default_timezone_set('UTC');//php.ini missing tz, avoid warning
if (!is_writable($this->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);
Expand Down Expand Up @@ -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/');
Expand Down
6 changes: 4 additions & 2 deletions src/FastExcelWriter/WriterBuffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace avadim\FastExcelWriter;

use avadim\FastExcelWriter\Exception\Exception;

/**
* Class WriterBuffer
*
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit a7467db

Please sign in to comment.