Skip to content

Commit

Permalink
Merge pull request #11 from keboola/odin-destructor
Browse files Browse the repository at this point in the history
remove destructor
  • Loading branch information
odinuv authored Feb 18, 2019
2 parents fb9043c + 36fd92a commit 7a1703e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@ $temp = new Temp('prefix');
// Creates a file with unique name suffixed by 'suffix'
$tempFile = $temp->createTmpFile('suffix');
echo 'Files are stored in: ' . $temp->getTmpFolder();
$temp->remove();
```

## Migration from version 1.0
The temp folder is no longer deleted automatically in the destructor. It needs to
be removed explicitly by calling the `remove()` method.
16 changes: 5 additions & 11 deletions src/Keboola/Temp/Temp.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,13 @@ public function setId($id)
/**
* Delete all files created by syrup component run
*/
public function __destruct()
public function remove()
{
try {
foreach ($this->files as $file) {
if (file_exists($file['file']) && is_file($file['file'])) {
$this->filesystem->remove($file['file']->getPathname());
}
foreach ($this->files as $file) {
if (file_exists($file['file']) && is_file($file['file'])) {
$this->filesystem->remove($file['file']->getPathname());
}
$this->filesystem->remove($this->getTmpPath());
} catch (\Exception $e) {
// Graceful destructor, does not throw any errors.
// Fixes issues when deleting files on a server that is just shutting down.
// https://github.com/keboola/docker-bundle/issues/215
}
$this->filesystem->remove($this->getTmpPath());
}
}
12 changes: 6 additions & 6 deletions tests/Keboola/Temp/TempTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function testCreateFile()
public function testCreateFileNested()
{
$temp = new Temp();
$file = $temp->createFile('dir/file');
$temp->createFile('dir/file');

self::assertFileExists($temp->getTmpFolder() . '/dir/file');
}
Expand All @@ -54,22 +54,22 @@ public function testSetTmpFolder()
$expectedTmpDir = sys_get_temp_dir() . "/test/aabb";
$this->assertEquals($expectedTmpDir, $temp->getTmpFolder());

$file = $temp->createFile('file');
$temp->createFile('file');
self::assertFileExists(sys_get_temp_dir() . "/test/aabb/file");
}

public function testCleanup()
{
$temp = new Temp();
$file = $temp->createFile('file');
$deeperFile = $temp->createFile('dir/file2');
$temp->createFile('file');
$temp->createFile('dir/file2');

$dir = $temp->getTmpFolder();

self::assertFileExists($dir . '/file');
self::assertFileExists($dir . '/dir/file2');

unset($temp);
$temp->remove();
self::assertFileNotExists($dir);
}

Expand All @@ -83,7 +83,7 @@ public function testCleanupForeignFile()
touch($dir . '/file');
self::assertFileExists($dir . '/file');

unset($temp);
$temp->remove();
self::assertFileNotExists($dir);
}
}

0 comments on commit 7a1703e

Please sign in to comment.