|
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
|
|
|
class Helpers |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
15 |
|
public static function getHttpScheme(): string |
|
16
|
|
|
{ |
|
17
|
15 |
|
$scheme = 'http'; |
|
18
|
15 |
|
if (isset($_SERVER['HTTP_SCHEME'])) { |
|
19
|
1 |
|
return (string)$_SERVER['HTTP_SCHEME']; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
14 |
|
if (isset($_SERVER['HTTPS']) && \strtolower((string)$_SERVER['HTTPS']) != 'off') { |
|
23
|
1 |
|
return 'https'; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
13 |
|
if (isset($_SERVER['SERVER_PORT']) && 443 == (int)$_SERVER['SERVER_PORT']) { |
|
27
|
1 |
|
return 'https'; |
|
28
|
|
|
} |
|
29
|
12 |
|
return $scheme; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param string $file |
|
34
|
|
|
* @param string $data |
|
35
|
|
|
* @param string $mode |
|
36
|
|
|
* @param LoggerInterface|null $logger |
|
37
|
|
|
* @return void |
|
38
|
|
|
*/ |
|
39
|
1 |
|
public static function writeFile( |
|
40
|
|
|
string $file, |
|
41
|
|
|
string $data, |
|
42
|
|
|
string $mode = 'w', |
|
43
|
|
|
LoggerInterface $logger = null |
|
44
|
|
|
): void { |
|
45
|
1 |
|
$logger ??= new NullLogger(); |
|
46
|
1 |
|
writeFile($file, $data, $mode); |
|
47
|
1 |
|
$logger->info(sprintf('Write to: %s', $file)); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
2 |
|
public static function createEmptyFile(string $file, LoggerInterface $logger = null): void |
|
51
|
|
|
{ |
|
52
|
2 |
|
$logger ??= new NullLogger(); |
|
53
|
2 |
|
writeFile($file, ''); |
|
54
|
2 |
|
$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
|
10 |
|
public static function createDirectory(string $path, int $permissions = 0775, LoggerInterface $logger = null): void |
|
65
|
|
|
{ |
|
66
|
10 |
|
$logger ??= new NullLogger(); |
|
67
|
|
|
|
|
68
|
10 |
|
if (createDirectory($path, $permissions)) { |
|
69
|
4 |
|
$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
|
|
|
|