-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathcreate_file.php
48 lines (40 loc) · 1.4 KB
/
create_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
<?php
declare(strict_types=1);
namespace Psl\Filesystem;
use Psl\Internal;
use Psl\Math;
use Psl\Str;
use function touch;
/**
* Create the file specified by $filename.
*
* @param non-empty-string $filename
* @param int|null $time The touch time as a Unix timestamp.
* Defaults to the current system time.
* @param int|null $access_time The access time as a Unix timestamp.
* Defaults to the current system time.
*
* @throws Exception\RuntimeException If unable to create the file.
*/
function create_file(string $filename, null|int $time = null, null|int $access_time = null): void
{
if (null === $access_time && null === $time) {
$fun = static fn(): bool => touch($filename);
} elseif (null === $access_time) {
$fun = static fn(): bool => touch($filename, $time);
} else {
$time = $time ?? $access_time;
$fun = static fn(): bool => touch($filename, $time, Math\maxva($access_time, $time));
}
namespace\create_directory_for_file($filename);
[$result, $error_message] = Internal\box($fun);
// @codeCoverageIgnoreStart
if (false === $result && !namespace\is_file($filename)) {
throw new Exception\RuntimeException(Str\format(
'Failed to create file "%s": %s.',
$filename,
$error_message ?? 'internal error',
));
}
// @codeCoverageIgnoreEnd
}