Skip to content

Commit

Permalink
writer buffers moved from static to protected
Browse files Browse the repository at this point in the history
  • Loading branch information
aVadim483 committed Dec 23, 2023
1 parent 1c3fed4 commit edf06f0
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 24 deletions.
12 changes: 12 additions & 0 deletions src/FastExcelWriter/Excel.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,14 @@ public function __construct(?array $options = [])
];
}


public function __destruct()
{
if (!$this->saved && $this->fileName) {
$this->writer->saveToFile($this->fileName, false, $this->getMetadata());
}
}

/**
* @param string|object $class
* @param string|array $options
Expand Down Expand Up @@ -1382,6 +1390,10 @@ public function getProtection(): array
*/
public function save(?string $fileName = null, ?bool $overWrite = true): bool
{
if ($this->saved) {
ExceptionFile::throwNew('The workbook is already saved');
}

if (!$fileName && $this->fileName) {
$fileName = $this->fileName;
}
Expand Down
4 changes: 2 additions & 2 deletions src/FastExcelWriter/Sheet.php
Original file line number Diff line number Diff line change
Expand Up @@ -2579,7 +2579,7 @@ public function writeAreasRows($writer): Sheet
}

/**
* @param $writer
* @param Writer $writer
*
* @return void
*/
Expand All @@ -2591,7 +2591,7 @@ public function writeDataBegin($writer)
}

$sheetFileName = $writer->tempFilename();
$this->setFileWriter($writer::makeWriteBuffer($sheetFileName));
$this->setFileWriter($writer->makeWriteBuffer($sheetFileName));

$this->fileWriter->write('<sheetData>');

Expand Down
31 changes: 22 additions & 9 deletions src/FastExcelWriter/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ class Writer
. '|sort'
. ')\s*\(/mui';

protected static array $buffers = [];
//protected static array $buffers = [];
protected array $buffers = [];

/** @var Excel */
protected $excel;
Expand Down Expand Up @@ -210,12 +211,14 @@ public function __construct(?array $options = [])
*/
public function __destruct()
{
foreach (self::$buffers as $name => $buffer) {
/* moved to saveToFile()
foreach ($this->buffers as $name => $buffer) {
if ($buffer) {
$buffer->close();
self::$buffers[$name] = null;
$this->buffers[$name] = null;
}
}
*/
if (!empty($this->tempFiles['tmp'])) {
foreach ($this->tempFiles['tmp'] as $tempFile) {
if (is_file($tempFile)) {
Expand All @@ -233,14 +236,14 @@ public function __destruct()
*
* @return WriterBuffer
*/
public static function makeWriteBuffer(string $fileName): WriterBuffer
public function makeWriteBuffer(string $fileName): WriterBuffer
{
if (isset(self::$buffers[$fileName])) {
self::$buffers[$fileName] = null;
if (isset($this->buffers[$fileName])) {
$this->buffers[$fileName] = null;
}
self::$buffers[$fileName] = new WriterBuffer($fileName);
$this->buffers[$fileName] = new WriterBuffer($fileName);

return self::$buffers[$fileName];
return $this->buffers[$fileName];
}

/**
Expand Down Expand Up @@ -343,6 +346,9 @@ public function saveToFile(string $fileName, ?bool $overWrite = true, ?array $me
ExceptionFile::throwNew('Directory "%s" for output file is not exist', dirname($fileName));
}
if (file_exists($fileName)) {
if (!$overWrite) {
ExceptionFile::throwNew('File "%s" already exists', $fileName);
}
if ($overWrite && is_writable($fileName)) {
@unlink($fileName); //if the zip already exists, remove it
}
Expand Down Expand Up @@ -384,6 +390,13 @@ public function saveToFile(string $fileName, ?bool $overWrite = true, ?array $me
// 'xl/theme/theme{%n}.xml' -- workbook
$this->_writeThemesFiles($relationShips);

foreach ($this->buffers as $name => $buffer) {
if ($buffer) {
$buffer->close();
$this->buffers[$name] = null;
}
}

$this->writeToTemp('xl/workbook.xml', $this->_buildWorkbookXML($this->excel));
$this->writeToTemp('xl/_rels/workbook.xml.rels', $this->_buildWorkbookRelsXML($relationShips));
$this->writeToTemp('docProps/app.xml', $this->_buildAppXML($metadata));
Expand Down Expand Up @@ -785,7 +798,7 @@ protected function _writeDrawingFile(string $entry, array $imageList, array &$re
*/
protected function _writeSheetHead(Sheet $sheet): WriterBuffer
{
$fileWriter = self::makeWriteBuffer($this->tempFilename());
$fileWriter = $this->makeWriteBuffer($this->tempFilename());
$fileWriter->write('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' . "\n");
$nameSpaces = [
'xmlns' => 'http://schemas.openxmlformats.org/spreadsheetml/2006/main',
Expand Down
36 changes: 23 additions & 13 deletions tests/FastExcelWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ public function testExcelWriter0()
$this->assertEquals('B9', $this->cells['B9']);
$this->assertEquals('D9', $this->cells['D9']);
$this->assertEquals('E9', $this->cells['E9']);

unlink($testFileName);

$this->excelReader = null;
$this->cells = [];
}


Expand Down Expand Up @@ -249,7 +254,7 @@ public function testExcelWriter1()

$this->checkDefaultStyle($this->getStyle('a7', true));

// unlink($testFileName);
unlink($testFileName);

$this->excelReader = null;
$this->cells = [];
Expand Down Expand Up @@ -682,26 +687,31 @@ public function testExcelWriterSingleValue()
unlink($testFileName);
}

$excel = Excel::create();
$sheet = $excel->sheet();
$sheet->setValue('B5', 'test');
$this->excelReader = $this->saveCheckRead($excel, $testFileName);
$excel1 = Excel::create();
$sheet1 = $excel1->sheet();
$sheet1->setValue('B5', 'test');

$excel2 = Excel::create();
$sheet2 = $excel2->sheet();
$sheet2->setValue('B5:C7', 'test');

$excel3 = Excel::create();
$sheet3 = $excel3->sheet();

$this->excelReader = $this->saveCheckRead($excel1, $testFileName);
$this->cells = $this->excelReader->readCells();
$this->assertEquals('test', $this->cells['B5']);
unlink($testFileName);
$this->cells = [];

$excel = Excel::create();
$sheet = $excel->sheet();
$sheet->setValue('B5:C7', 'test');
$this->excelReader = $this->saveCheckRead($excel, $testFileName);
$this->excelReader = $this->saveCheckRead($excel2, $testFileName);
$this->cells = $this->excelReader->readCells();
$this->assertEquals('test', $this->cells['B5']);
unlink($testFileName);
$this->cells = [];

$excel = Excel::create();
$sheet = $excel->sheet();
$sheet->setValue([2, 5], 'test');
$this->excelReader = $this->saveCheckRead($excel, $testFileName);
$sheet3->setValue([2, 5], 'test');
$this->excelReader = $this->saveCheckRead($excel3, $testFileName);
$this->cells = $this->excelReader->readCells();
$this->assertEquals('test', $this->cells['B5']);
unlink($testFileName);
Expand Down

0 comments on commit edf06f0

Please sign in to comment.