-
Notifications
You must be signed in to change notification settings - Fork 0
/
TempDirectory.php
60 lines (53 loc) · 1.46 KB
/
TempDirectory.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
53
54
55
56
57
58
59
60
<?php
namespace Aternos\IO\System\Directory;
use Aternos\IO\Exception\CreateDirectoryException;
use Aternos\IO\Exception\DeleteException;
use Aternos\IO\Exception\GetTargetException;
use Aternos\IO\Exception\IOException;
use Aternos\IO\Exception\MissingPermissionsException;
/**
* Class TempDirectory
*
* Temporary directory on disk, created automatically in the temp directory, deleted on destruct by default
*
* @package Aternos\IO\System\Directory
*/
class TempDirectory extends Directory
{
/**
* @param string $prefix Prefix for the temporary directory name
* @param bool $deleteOnDestruct
* @throws CreateDirectoryException
* @throws IOException
*/
public function __construct(string $prefix = "io-", protected bool $deleteOnDestruct = true)
{
do {
$path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $prefix . uniqid();
} while (file_exists($path));
parent::__construct($path);
$this->create();
}
/**
* @throws DeleteException
* @throws GetTargetException
* @throws MissingPermissionsException
* @throws IOException
*/
public function __destruct()
{
if ($this->deleteOnDestruct) {
$this->delete();
}
}
/**
* @return array|string[]
*/
public function __serialize(): array
{
return [
...parent::__serialize(),
"deleteOnDestruct" => $this->deleteOnDestruct
];
}
}