1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Enjoys\AssetsCollector; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use Enjoys\AssetsCollector\Exception\PathDirectoryIsNotValid; |
9
|
|
|
use Enjoys\AssetsCollector\Strategy\ManyFilesStrategy; |
10
|
|
|
use Psr\Http\Client\ClientInterface; |
11
|
|
|
use Psr\Http\Message\RequestFactoryInterface; |
12
|
|
|
use Psr\Log\LoggerInterface; |
13
|
|
|
use Psr\Log\NullLogger; |
14
|
|
|
|
15
|
|
|
use function putenv; |
16
|
|
|
|
17
|
|
|
class Environment |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
private string $projectDir; |
21
|
|
|
|
22
|
|
|
private string $compileDir; |
23
|
|
|
|
24
|
|
|
private string $baseUrl = ''; |
25
|
|
|
|
26
|
|
|
private int $cacheTime = -1; |
27
|
|
|
|
28
|
|
|
private ?string $version = null; |
29
|
|
|
|
30
|
|
|
private string $paramVersion = 'v'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var class-string<Strategy>|Strategy |
|
|
|
|
34
|
|
|
*/ |
35
|
|
|
private string|Strategy $strategy = ManyFilesStrategy::class; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array<string, Closure(string):string|Minifier|null> |
|
|
|
|
39
|
80 |
|
*/ |
40
|
|
|
private array $minifiers = []; |
41
|
80 |
|
|
42
|
80 |
|
/** |
43
|
80 |
|
* @var array<string, Closure(Asset[]):string|Renderer|null> |
|
|
|
|
44
|
80 |
|
*/ |
45
|
|
|
private array $renderers = []; |
46
|
80 |
|
|
47
|
|
|
private ?ClientInterface $httpClient = null; |
48
|
80 |
|
|
49
|
1 |
|
private ?RequestFactoryInterface $requestFactory = null; |
50
|
1 |
|
|
51
|
1 |
|
/** |
52
|
|
|
* Environment constructor. |
53
|
79 |
|
* @param string $compileDir |
54
|
79 |
|
* @param string $projectDir Если пустая строка, то realpath вернет текущую рабочую директорию |
55
|
|
|
* @param LoggerInterface $logger |
56
|
79 |
|
*/ |
57
|
79 |
|
public function __construct( |
58
|
|
|
string $compileDir = '/', |
59
|
|
|
string $projectDir = '', |
60
|
|
|
private LoggerInterface $logger = new NullLogger() |
61
|
|
|
) { |
62
|
|
|
$projectDir = realpath($projectDir); |
63
|
79 |
|
|
64
|
|
|
if ($projectDir === false) { |
65
|
79 |
|
throw new PathDirectoryIsNotValid( |
66
|
|
|
"The project directory is not installed or the directory could not be determined automatically" |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
putenv("ASSETS_PROJECT_DIRECTORY=$projectDir/"); |
70
|
|
|
|
71
|
|
|
$this->projectDir = $projectDir; |
72
|
|
|
$this->compileDir = $this->normalizeCompileDir($compileDir); |
73
|
|
|
} |
74
|
|
|
|
75
|
79 |
|
public function getProjectDir(): string |
76
|
|
|
{ |
77
|
79 |
|
return $this->projectDir; |
78
|
26 |
|
} |
79
|
|
|
|
80
|
79 |
|
/** |
81
|
79 |
|
* Функцию realpath() нельзя применять так как директории изначально может не быть, |
82
|
|
|
* она может потом быть создана, если будут права |
83
|
|
|
* @infection-ignore-all |
84
|
|
|
*/ |
85
|
|
|
private function normalizeCompileDir(string $path): string |
86
|
|
|
{ |
87
|
42 |
|
if (str_starts_with($path, $this->getProjectDir())) { |
88
|
|
|
$path = str_replace($this->getProjectDir(), '', $path); |
89
|
42 |
|
} |
90
|
|
|
$path = $this->getProjectDir() . '/' . ltrim($path, '/\.'); |
91
|
|
|
return rtrim($path, '/'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function getCompileDir(): string |
95
|
|
|
{ |
96
|
|
|
return $this->compileDir; |
97
|
42 |
|
} |
98
|
|
|
|
99
|
42 |
|
public function setBaseUrl(string $baseUrl = '/'): Environment |
100
|
42 |
|
{ |
101
|
|
|
$this->baseUrl = rtrim($baseUrl, '\/'); |
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function getBaseUrl(): string |
106
|
21 |
|
{ |
107
|
|
|
return $this->baseUrl; |
108
|
21 |
|
} |
109
|
|
|
|
110
|
|
|
public function setCacheTime(int $cacheTime): Environment |
111
|
|
|
{ |
112
|
|
|
$this->cacheTime = $cacheTime; |
113
|
|
|
return $this; |
114
|
|
|
} |
115
|
1 |
|
|
116
|
|
|
public function getCacheTime(): int |
117
|
1 |
|
{ |
118
|
1 |
|
return $this->cacheTime; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function getStrategy(): Strategy |
122
|
|
|
{ |
123
|
|
|
if ($this->strategy instanceof Strategy) { |
124
|
15 |
|
return $this->strategy; |
125
|
|
|
} |
126
|
15 |
|
return new $this->strategy(); |
127
|
15 |
|
} |
128
|
|
|
|
129
|
1 |
|
/** |
130
|
|
|
* @param class-string<Strategy>|Strategy $strategy |
|
|
|
|
131
|
|
|
*/ |
132
|
|
|
public function setStrategy(string|Strategy $strategy): Environment |
133
|
|
|
{ |
134
|
|
|
$this->strategy = $strategy; |
135
|
|
|
return $this; |
136
|
1 |
|
} |
137
|
|
|
|
138
|
1 |
|
public function getLogger(): LoggerInterface |
139
|
1 |
|
{ |
140
|
|
|
return $this->logger; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function setLogger(LoggerInterface $logger): Environment |
144
|
|
|
{ |
145
|
|
|
$this->logger = $logger; |
146
|
|
|
return $this; |
147
|
2 |
|
} |
148
|
|
|
|
149
|
2 |
|
public function getHttpClient(): ?ClientInterface |
150
|
2 |
|
{ |
151
|
|
|
return $this->httpClient; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function setHttpClient(?ClientInterface $httpClient): Environment |
155
|
|
|
{ |
156
|
7 |
|
$this->httpClient = $httpClient; |
157
|
|
|
return $this; |
158
|
7 |
|
} |
159
|
|
|
|
160
|
|
|
public function getRequestFactory(): ?RequestFactoryInterface |
161
|
13 |
|
{ |
162
|
|
|
return $this->requestFactory; |
163
|
13 |
|
} |
164
|
|
|
|
165
|
|
|
public function setRequestFactory(?RequestFactoryInterface $requestFactory): Environment |
166
|
1 |
|
{ |
167
|
|
|
$this->requestFactory = $requestFactory; |
168
|
1 |
|
return $this; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param AssetType $type |
173
|
|
|
* @param Minifier|Closure(string):string|null $minifier |
174
|
|
|
* @return Environment |
175
|
16 |
|
*/ |
176
|
|
|
public function setMinifier(AssetType $type, Minifier|Closure|null $minifier): Environment |
177
|
16 |
|
{ |
178
|
16 |
|
$this->minifiers[$type->value] = $minifier; |
179
|
|
|
return $this; |
180
|
|
|
} |
181
|
39 |
|
|
182
|
|
|
public function getMinifier(AssetType $type): ?Minifier |
183
|
39 |
|
{ |
184
|
|
|
return MinifierFactory::get($this->minifiers[$type->value] ?? null); |
185
|
|
|
} |
186
|
5 |
|
|
187
|
|
|
/** |
188
|
5 |
|
* @param AssetType $type |
189
|
5 |
|
* @param Renderer|Closure(Asset[]):string|null $renderer |
190
|
|
|
* @return Environment |
191
|
|
|
*/ |
192
|
10 |
|
public function setRenderer(AssetType $type, Renderer|Closure|null $renderer): Environment |
193
|
|
|
{ |
194
|
10 |
|
$this->renderers[$type->value] = $renderer; |
195
|
|
|
return $this; |
196
|
|
|
} |
197
|
14 |
|
|
198
|
|
|
public function getRenderer(AssetType $type): Renderer |
199
|
14 |
|
{ |
200
|
|
|
$renderer = $this->renderers[$type->value] |
201
|
|
|
?? RenderFactory::getDefaultRenderer($type); |
202
|
|
|
|
203
|
|
|
if ($renderer instanceof Renderer) { |
204
|
|
|
return $renderer; |
205
|
|
|
} |
206
|
6 |
|
return RenderFactory::createFromClosure($renderer); |
207
|
|
|
} |
208
|
6 |
|
|
209
|
1 |
|
public function setParamVersion(string $paramVersion): Environment |
210
|
|
|
{ |
211
|
5 |
|
$this->paramVersion = $paramVersion; |
212
|
|
|
return $this; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function setVersion(string|int|float $version): Environment |
216
|
|
|
{ |
217
|
4 |
|
$this->version = (string)$version; |
218
|
|
|
return $this; |
219
|
4 |
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @return array<string, string> |
223
|
|
|
*/ |
224
|
|
|
public function getVersionQuery(): array |
225
|
|
|
{ |
226
|
|
|
if ($this->version === null) { |
227
|
|
|
return []; |
228
|
|
|
} |
229
|
|
|
return [$this->paramVersion => $this->version]; |
230
|
|
|
} |
231
|
4 |
|
|
232
|
|
|
public function getVersion(): ?string |
233
|
4 |
|
{ |
234
|
|
|
return $this->version; |
235
|
|
|
} |
236
|
2 |
|
|
237
|
|
|
public function getParamVersion(): string |
238
|
2 |
|
{ |
239
|
2 |
|
return $this->paramVersion; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
} |
243
|
|
|
|