-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathcreate_file.php
51 lines (43 loc) · 1.49 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
49
50
51
<?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 int|null $time The touch time as a Unix timestamp,
* If not supplied the current system time is used.
* @param int|null $access_time The access time as a Unix timestamp,
* If not supplied the current system time is used.
*
* @throws Exception\RuntimeException If unable to create the file.
*/
function create_file(string $filename, ?int $time = 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));
}
/** @psalm-suppress MissingThrowsDocblock */
$directory = get_directory($filename);
if (!is_directory($directory)) {
create_directory($directory);
}
[$result, $error_message] = Internal\box($fun);
// @codeCoverageIgnoreStart
if (false === $result && !is_file($filename)) {
throw new Exception\RuntimeException(Str\format(
'Failed to create file "%s": %s.',
$filename,
$error_message ?? 'internal error'
));
}
// @codeCoverageIgnoreEnd
}