1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Enjoys\AssetsCollector; |
4
|
|
|
|
5
|
|
|
use Psr\Log\LoggerInterface; |
6
|
|
|
use Psr\Log\NullLogger; |
7
|
|
|
|
8
|
|
|
use function Enjoys\FileSystem\createDirectory; |
9
|
|
|
use function Enjoys\FileSystem\CreateSymlink; |
10
|
|
|
use function Enjoys\FileSystem\writeFile; |
11
|
|
|
|
12
|
|
|
final class Helpers |
13
|
|
|
{ |
14
|
|
|
|
15
|
18 |
|
public static function getHttpScheme(): string |
16
|
|
|
{ |
17
|
18 |
|
if (isset($_SERVER['HTTP_SCHEME'])) { |
18
|
2 |
|
return $_SERVER['HTTP_SCHEME']; |
19
|
|
|
} |
20
|
|
|
|
21
|
16 |
|
if (isset($_SERVER['HTTPS']) && \strtolower($_SERVER['HTTPS']) != 'off') { |
22
|
1 |
|
return 'https'; |
23
|
|
|
} |
24
|
|
|
|
25
|
15 |
|
if (isset($_SERVER['SERVER_PORT']) && 443 == (int)$_SERVER['SERVER_PORT']) { |
26
|
1 |
|
return 'https'; |
27
|
|
|
} |
28
|
14 |
|
return 'http'; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $file |
33
|
|
|
* @param string $data |
34
|
|
|
* @param string $mode |
35
|
|
|
* @param LoggerInterface|null $logger |
36
|
|
|
* @return void |
37
|
|
|
* @throws \Exception |
38
|
|
|
*/ |
39
|
3 |
|
public static function writeFile( |
40
|
|
|
string $file, |
41
|
|
|
string $data, |
42
|
|
|
string $mode = 'w', |
43
|
|
|
LoggerInterface $logger = null |
44
|
|
|
): void { |
45
|
3 |
|
$logger ??= new NullLogger(); |
46
|
3 |
|
writeFile($file, $data, $mode); |
47
|
3 |
|
$logger->info(sprintf('Write to: %s', $file)); |
48
|
|
|
} |
49
|
|
|
|
50
|
4 |
|
public static function createEmptyFile(string $file, LoggerInterface $logger = null): void |
51
|
|
|
{ |
52
|
4 |
|
$logger ??= new NullLogger(); |
53
|
4 |
|
writeFile($file, ''); |
54
|
4 |
|
$logger->info(sprintf('Create file: %s', $file)); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $path |
59
|
|
|
* @param int $permissions |
60
|
|
|
* @param LoggerInterface|null $logger |
61
|
|
|
* @return void |
62
|
|
|
* @throws \Exception |
63
|
|
|
*/ |
64
|
12 |
|
public static function createDirectory(string $path, int $permissions = 0775, LoggerInterface $logger = null): void |
65
|
|
|
{ |
66
|
12 |
|
$logger ??= new NullLogger(); |
67
|
|
|
|
68
|
12 |
|
if (createDirectory($path, $permissions)) { |
69
|
5 |
|
$logger->info(sprintf('Create directory %s', $path)); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $link |
75
|
|
|
* @param string $target |
76
|
|
|
* @param LoggerInterface|null $logger |
77
|
|
|
* @throws \Exception |
78
|
|
|
*/ |
79
|
11 |
|
public static function createSymlink(string $link, string $target, LoggerInterface $logger = null): void |
80
|
|
|
{ |
81
|
11 |
|
$logger ??= new NullLogger(); |
82
|
|
|
|
83
|
|
|
try { |
84
|
11 |
|
if (CreateSymlink($link, $target)) { |
85
|
11 |
|
$logger->info(sprintf('Created symlink: %s', $link)); |
86
|
|
|
} |
87
|
|
|
} catch (\Exception $e) { |
88
|
|
|
$logger->notice($e->getMessage()); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|