-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathTemporaryFileHelper.php
52 lines (44 loc) · 1.25 KB
/
TemporaryFileHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
namespace Unleash\Client\Tests\TestHelpers;
trait TemporaryFileHelper
{
/**
* @var bool
*/
private $_tfh_ShutdownFunctionRegistered = false;
/**
* @var array<string>
*/
private $_tfh_DeleteLaterList = [];
private function _registerShutdownFunction(): void
{
if ($this->_tfh_ShutdownFunctionRegistered) {
return;
}
register_shutdown_function(function () {
foreach ($this->_tfh_DeleteLaterList as $file) {
if (file_exists($file)) {
if (!is_writable($file)) {
chmod($file, 0666);
}
unlink($file);
}
}
});
$this->_tfh_ShutdownFunctionRegistered = true;
}
private function deleteLater(string $filePath): void
{
$this->_registerShutdownFunction();
$this->_tfh_DeleteLaterList[] = $filePath;
}
private function createTemporaryFile(bool $autoCleanup = true): string
{
$this->_registerShutdownFunction();
$file = tempnam(sys_get_temp_dir(), 'unleash_tests_file_helper');
if ($autoCleanup) {
$this->deleteLater($file);
}
return $file;
}
}