Skip to content

Commit

Permalink
fix #40;
Browse files Browse the repository at this point in the history
clear buffers & remove tmp files
  • Loading branch information
aVadim483 committed Oct 8, 2023
1 parent 2a33503 commit eabfda8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
27 changes: 14 additions & 13 deletions src/FastExcelWriter/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,15 +168,14 @@ class Writer
. '|sort'
. ')\s*\(/mui';

protected static array $buffers = [];

/** @var Excel */
protected $excel;

/** @var array */
protected array $tempFiles = [];

/** @var array */
protected array $tempXlsx = [];

/** @var string */
protected $tempDir = '';

Expand Down Expand Up @@ -211,11 +210,10 @@ public function __construct(?array $options = [])
*/
public function __destruct()
{
if (!empty($this->tempFiles['zip'])) {
foreach ($this->tempFiles['zip'] as $tempFile) {
if (is_file($tempFile)) {
@unlink($tempFile);
}
foreach (self::$buffers as $name => $buffer) {
if ($buffer) {
$buffer->close();
self::$buffers[$name] = null;
}
}
if (!empty($this->tempFiles['tmp'])) {
Expand All @@ -235,9 +233,14 @@ public function __destruct()
*
* @return WriterBuffer
*/
public static function makeWriteBuffer(string $fileName)
public static function makeWriteBuffer(string $fileName): WriterBuffer
{
return new WriterBuffer($fileName);
if (isset(self::$buffers[$fileName])) {
self::$buffers[$fileName] = null;
}
self::$buffers[$fileName] = new WriterBuffer($fileName);

return self::$buffers[$fileName];
}

/**
Expand Down Expand Up @@ -276,9 +279,7 @@ public function tempFilename($localName = null)
if ($localName) {
$this->tempFiles['zip'][$localName] = $filename;
}
else {
$this->tempFiles['tmp'][] = $filename;
}
$this->tempFiles['tmp'][] = $filename;
}

return $filename;
Expand Down
2 changes: 1 addition & 1 deletion src/FastExcelWriter/WriterBuffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function __construct(string $filename, ?string $openFlags = 'wb', ?bool $
/**
* @return string
*/
public function getFileName()
public function getFileName(): string
{
return $this->fileName;
}
Expand Down

0 comments on commit eabfda8

Please sign in to comment.