1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Enjoys\AssetsCollector; |
4
|
|
|
|
5
|
|
|
use Psr\Log\LoggerInterface; |
6
|
|
|
use Psr\Log\NullLogger; |
7
|
|
|
|
8
|
|
|
class Helpers |
9
|
|
|
{ |
10
|
|
|
|
11
|
15 |
|
public static function getHttpScheme(): string |
12
|
|
|
{ |
13
|
15 |
|
$scheme = 'http'; |
14
|
15 |
|
if (isset($_SERVER['HTTP_SCHEME'])) { |
15
|
1 |
|
return $_SERVER['HTTP_SCHEME']; |
16
|
|
|
} |
17
|
|
|
|
18
|
14 |
|
if (isset($_SERVER['HTTPS']) && \strtolower($_SERVER['HTTPS']) != 'off') { |
19
|
1 |
|
return 'https'; |
20
|
|
|
} |
21
|
|
|
|
22
|
13 |
|
if (isset($_SERVER['SERVER_PORT']) && 443 == $_SERVER['SERVER_PORT']) { |
23
|
1 |
|
return 'https'; |
24
|
|
|
} |
25
|
12 |
|
return $scheme; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param string $file |
30
|
|
|
* @param string $data |
31
|
|
|
* @param string $mode |
32
|
|
|
* @param LoggerInterface|null $logger |
33
|
|
|
* @return void |
34
|
|
|
*/ |
35
|
1 |
|
public static function writeFile( |
36
|
|
|
string $file, |
37
|
|
|
string $data, |
38
|
|
|
string $mode = 'w', |
39
|
|
|
LoggerInterface $logger = null |
40
|
|
|
): void { |
41
|
1 |
|
$logger ??= new NullLogger(); |
42
|
|
|
|
43
|
1 |
|
$f = fopen($file, $mode); |
44
|
1 |
|
if ($f !== false) { |
45
|
1 |
|
fwrite($f, $data); |
46
|
1 |
|
fclose($f); |
47
|
1 |
|
$logger->info(sprintf('Write to: %s', $file)); |
48
|
|
|
} |
49
|
1 |
|
} |
50
|
|
|
|
51
|
2 |
|
public static function createEmptyFile(string $file, LoggerInterface $logger = null): void |
52
|
|
|
{ |
53
|
2 |
|
$logger ??= new NullLogger(); |
54
|
|
|
|
55
|
2 |
|
$f = fopen($file, 'w'); |
56
|
2 |
|
if ($f !== false) { |
57
|
2 |
|
fwrite($f, ''); |
58
|
2 |
|
fclose($f); |
59
|
2 |
|
$logger->info(sprintf('Create file: %s', $file)); |
60
|
|
|
} |
61
|
2 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $path |
65
|
|
|
* @param int $permissions |
66
|
|
|
* @param LoggerInterface|null $logger |
67
|
|
|
* @return void |
68
|
|
|
* @throws \Exception |
69
|
|
|
*/ |
70
|
13 |
|
public static function createDirectory(string $path, int $permissions = 0777, LoggerInterface $logger = null): void |
71
|
|
|
{ |
72
|
13 |
|
$logger ??= new NullLogger(); |
73
|
|
|
|
74
|
13 |
|
if (preg_match("/(\/\.+|\.+)$/i", $path)) { |
75
|
6 |
|
throw new \Exception( |
76
|
6 |
|
sprintf("Нельзя создать директорию: %s", $path) |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
//Clear the most recent error |
81
|
7 |
|
error_clear_last(); |
82
|
|
|
|
83
|
7 |
|
if (!is_dir($path)) { |
84
|
7 |
|
if (@mkdir($path, $permissions, true) === false) { |
85
|
|
|
/** @var string[] $error */ |
86
|
1 |
|
$error = error_get_last(); |
87
|
1 |
|
throw new \Exception( |
88
|
1 |
|
sprintf("Не удалось создать директорию: %s! Причина: %s", $path, $error['message']) |
89
|
|
|
); |
90
|
|
|
} |
91
|
6 |
|
$logger->info(sprintf('Create directory %s', $path)); |
92
|
|
|
} |
93
|
6 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $link |
97
|
|
|
* @param string $target |
98
|
|
|
* @param LoggerInterface|null $logger |
99
|
|
|
* @throws \Exception |
100
|
|
|
*/ |
101
|
2 |
|
public static function createSymlink(string $link, string $target, LoggerInterface $logger = null): void |
102
|
|
|
{ |
103
|
2 |
|
$logger ??= new NullLogger(); |
104
|
|
|
|
105
|
2 |
|
$directory = pathinfo($link, PATHINFO_DIRNAME); |
106
|
2 |
|
Helpers::createDirectory($directory, 0755, $logger); |
|
|
|
|
107
|
|
|
|
108
|
2 |
|
if (file_exists($link)) { |
109
|
|
|
symlink($target, $link); |
110
|
|
|
$logger->info(sprintf('Created symlink: %s', $link)); |
111
|
|
|
} |
112
|
2 |
|
} |
113
|
|
|
} |
114
|
|
|
|