1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Enjoys\AssetsCollector; |
4
|
|
|
|
5
|
|
|
use Psr\Log\LoggerInterface; |
6
|
|
|
use Psr\Log\NullLogger; |
7
|
|
|
|
8
|
|
|
class Environment |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
private string $projectDir; |
12
|
|
|
private string $compileDir; |
13
|
|
|
private string $baseUrl = ''; |
14
|
|
|
|
15
|
|
|
private int $cacheTime = -1; |
16
|
|
|
|
17
|
|
|
private int $strategy = Assets::STRATEGY_MANY_FILES; |
18
|
|
|
private string $render = Assets::RENDER_HTML; |
19
|
|
|
|
20
|
|
|
private ?string $version = null; |
21
|
|
|
private string $paramVersion = '?v='; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array{css: array<mixed>, js: array<mixed>} |
25
|
|
|
*/ |
26
|
|
|
private array $minifyOptions = [ |
27
|
|
|
'css' => [], |
28
|
|
|
'js' => [] |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
private LoggerInterface $logger; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Environment constructor. |
35
|
|
|
* @param string $compileDir |
36
|
|
|
* @param string $projectDir Если пустая строка, то realpath вернет текущую рабочую директорию |
37
|
|
|
* @throws \Exception |
38
|
|
|
*/ |
39
|
47 |
|
public function __construct(string $compileDir = '/', string $projectDir = '') |
40
|
|
|
{ |
41
|
47 |
|
$projectDir = realpath($projectDir); |
42
|
|
|
|
43
|
47 |
|
if ($projectDir === false) { |
44
|
1 |
|
throw new \Exception( |
45
|
1 |
|
sprintf("Не установлена директория проекта или не удалось автоматически определить директорию") |
46
|
|
|
); |
47
|
|
|
} |
48
|
46 |
|
$this->projectDir = $projectDir; |
49
|
46 |
|
\putenv("ASSETS_PROJECT_DIRECTORY={$this->projectDir}/"); |
50
|
|
|
|
51
|
46 |
|
$this->compileDir = $this->setCompileDir($compileDir); |
52
|
46 |
|
$this->logger = new NullLogger(); |
53
|
46 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
46 |
|
public function getProjectDir(): string |
59
|
|
|
{ |
60
|
46 |
|
return $this->projectDir; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* realpath нельзя применять так как директории изначально может не быть |
66
|
|
|
* она может потом быть создана, если будут права |
67
|
|
|
* @param string $path |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
46 |
|
private function setCompileDir(string $path): string |
71
|
|
|
{ |
72
|
46 |
|
if (str_starts_with($path, $this->getProjectDir())) { |
73
|
16 |
|
$path = str_replace($this->getProjectDir(), '', $path); |
74
|
|
|
} |
75
|
46 |
|
$path = $this->getProjectDir() . '/' . ltrim($path, '/\.'); |
76
|
46 |
|
$path = rtrim($path, '/'); |
77
|
46 |
|
return $path; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
13 |
|
public function getCompileDir(): string |
84
|
|
|
{ |
85
|
13 |
|
return $this->compileDir; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $baseUrl |
91
|
|
|
* @return $this |
92
|
|
|
*/ |
93
|
25 |
|
public function setBaseUrl(string $baseUrl = '/'): Environment |
94
|
|
|
{ |
95
|
25 |
|
$this->baseUrl = rtrim($baseUrl, '\/'); |
96
|
25 |
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
8 |
|
public function getBaseUrl(): string |
103
|
|
|
{ |
104
|
8 |
|
return $this->baseUrl; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $version |
109
|
|
|
* @return $this |
110
|
|
|
*/ |
111
|
1 |
|
public function setVersion(string $version): Environment |
112
|
|
|
{ |
113
|
1 |
|
$this->version = $version; |
114
|
1 |
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
6 |
|
public function getVersion(): ?string |
121
|
|
|
{ |
122
|
6 |
|
if ($this->version === null) { |
123
|
6 |
|
return null; |
124
|
|
|
} |
125
|
1 |
|
return $this->paramVersion . $this->version; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @param string $paramVersion |
130
|
|
|
* @return $this |
131
|
|
|
*/ |
132
|
1 |
|
public function setParamVersion(string $paramVersion): Environment |
133
|
|
|
{ |
134
|
1 |
|
$this->paramVersion = $paramVersion; |
135
|
1 |
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param int $cacheTime |
141
|
|
|
* @return $this |
142
|
|
|
*/ |
143
|
1 |
|
public function setCacheTime(int $cacheTime): Environment |
144
|
|
|
{ |
145
|
1 |
|
$this->cacheTime = $cacheTime; |
146
|
1 |
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return int |
151
|
|
|
*/ |
152
|
3 |
|
public function getCacheTime(): int |
153
|
|
|
{ |
154
|
3 |
|
return $this->cacheTime; |
155
|
|
|
} |
156
|
|
|
|
157
|
4 |
|
public function getStrategy(): int |
158
|
|
|
{ |
159
|
4 |
|
return $this->strategy; |
160
|
|
|
} |
161
|
|
|
|
162
|
1 |
|
public function getRender(): string |
163
|
|
|
{ |
164
|
1 |
|
return $this->render; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param int $strategy |
169
|
|
|
* @return Environment |
170
|
|
|
*/ |
171
|
2 |
|
public function setStrategy(int $strategy): Environment |
172
|
|
|
{ |
173
|
2 |
|
$this->strategy = $strategy; |
174
|
2 |
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
10 |
|
public function getLogger(): LoggerInterface |
178
|
|
|
{ |
179
|
10 |
|
return $this->logger; |
180
|
|
|
} |
181
|
|
|
|
182
|
1 |
|
public function setLogger(LoggerInterface $logger): void |
183
|
|
|
{ |
184
|
1 |
|
$this->logger = $logger; |
185
|
1 |
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @return array{css: array<mixed>, js: array<mixed>} |
189
|
|
|
*/ |
190
|
2 |
|
public function getMinifyOptions(): array |
191
|
|
|
{ |
192
|
2 |
|
return $this->minifyOptions; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* @param array|array[] $options |
197
|
|
|
*/ |
198
|
|
|
public function setCssMinifyOptions(array $options): void |
199
|
|
|
{ |
200
|
|
|
$this->minifyOptions['css'] = $options; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param array|array[] $options |
205
|
|
|
*/ |
206
|
|
|
public function setJsMinifyOptions(array $options): void |
207
|
|
|
{ |
208
|
|
|
$this->minifyOptions['js'] = $options; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|