-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathcreate_temporary_file.php
56 lines (48 loc) · 1.76 KB
/
create_temporary_file.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
<?php
declare(strict_types=1);
namespace Psl\Filesystem;
use Psl;
use Psl\Env;
use Psl\SecureRandom;
use Psl\Str;
/**
* Create a temporary file.
*
* @param string|null $directory The directory where the temporary filename will be created.
* If no specified, `Env\temp_dir()` will be used to retrieve
* the system default temporary directory.
* @param string|null $prefix The prefix of the generated temporary filename.
*
* @throws Psl\Exception\InvariantViolationException If $directory doesn't exist or is not writable.
* @throws Psl\Exception\InvariantViolationException If $prefix contains a directory separator.
* @throws Exception\RuntimeException If unable to create the file.
*
* @return string The absolute path to the temporary file.
*/
function create_temporary_file(?string $directory = null, ?string $prefix = null): string
{
if (null !== $directory) {
Psl\invariant(is_directory($directory), '$directory is not a directory.');
Psl\invariant(is_writable($directory), '$directory is not writable.');
} else {
$directory = Env\temp_dir();
}
if (null !== $prefix) {
Psl\invariant(
!Str\contains($prefix, SEPARATOR),
'$prefix should not contain a directory separator ( "%s" ).',
SEPARATOR
);
} else {
$prefix = '';
}
try {
$filename = $directory . '/' . $prefix . SecureRandom\string(8);
// @codeCoverageIgnoreStart
} catch (SecureRandom\Exception\InsufficientEntropyException $e) {
throw new Exception\RuntimeException('Unable to gather enough entropy to generate filename.', 0, $e);
}
// @codeCoverageIgnoreEnd
create_file($filename);
return $filename;
}