-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRuntimePath.php
More file actions
52 lines (39 loc) · 1.32 KB
/
Copy pathRuntimePath.php
File metadata and controls
52 lines (39 loc) · 1.32 KB
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
declare(strict_types=1);
namespace Micilini\PhpSockets\Support;
final class RuntimePath
{
public static function attachmentsDirectory(): string
{
$configuredPath = getenv('PHPSOCKETS_ATTACHMENT_DIR');
if (is_string($configuredPath) && trim($configuredPath) !== '') {
return self::normalize($configuredPath);
}
$workingDirectory = getcwd();
if (is_string($workingDirectory) && $workingDirectory !== '') {
return self::join($workingDirectory, '.phpsockets', 'attachments');
}
return self::join(
sys_get_temp_dir(),
'phpsockets-' . substr(hash('sha256', __DIR__), 0, 12),
'attachments',
);
}
private static function join(string $basePath, string ...$segments): string
{
$path = rtrim($basePath, '/\\');
if ($path === '') {
$path = DIRECTORY_SEPARATOR;
}
foreach ($segments as $segment) {
$path .= DIRECTORY_SEPARATOR . trim($segment, '/\\');
}
return $path;
}
private static function normalize(string $path): string
{
$path = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, trim($path));
$path = rtrim($path, DIRECTORY_SEPARATOR);
return $path !== '' ? $path : DIRECTORY_SEPARATOR;
}
}