1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Enjoys\AssetsCollector; |
4
|
|
|
|
5
|
|
|
use Enjoys\AssetsCollector\Content\Minify\Adapters\NullMinify; |
6
|
|
|
use Enjoys\AssetsCollector\Content\Minify\MinifyInterface; |
7
|
|
|
use Enjoys\AssetsCollector\Exception\PathDirectoryIsNotValid; |
8
|
|
|
use Enjoys\AssetsCollector\Exception\UnexpectedParameters; |
9
|
|
|
use Psr\Log\LoggerInterface; |
10
|
|
|
use Psr\Log\NullLogger; |
11
|
|
|
|
12
|
|
|
class Environment |
13
|
|
|
{ |
14
|
|
|
private string $projectDir; |
15
|
|
|
private string $compileDir; |
16
|
|
|
private string $baseUrl = ''; |
17
|
|
|
private int $cacheTime = -1; |
18
|
|
|
private int $strategy = Assets::STRATEGY_MANY_FILES; |
19
|
|
|
private string $render = Assets::RENDER_HTML; |
20
|
|
|
private ?string $version = null; |
21
|
|
|
private string $paramVersion = '?v='; |
22
|
|
|
private LoggerInterface $logger; |
23
|
|
|
private int $directoryPermissions = 0775; |
24
|
|
|
/** |
25
|
|
|
* @var array{css: MinifyInterface, js: MinifyInterface} |
26
|
|
|
*/ |
27
|
|
|
private array $minify; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Environment constructor. |
31
|
|
|
* @param string $compileDir |
32
|
|
|
* @param string $projectDir Если пустая строка, то realpath вернет текущую рабочую директорию |
33
|
|
|
* @noinspection PhpUnnecessaryCurlyVarSyntaxInspection |
34
|
|
|
*/ |
35
|
66 |
|
public function __construct(string $compileDir = '/', string $projectDir = '') |
36
|
|
|
{ |
37
|
66 |
|
$this->minify = [ |
38
|
66 |
|
'css' => new NullMinify(), |
39
|
66 |
|
'js' => new NullMinify() |
40
|
|
|
]; |
41
|
|
|
|
42
|
66 |
|
$projectDir = realpath($projectDir); |
43
|
|
|
|
44
|
66 |
|
if ($projectDir === false) { |
45
|
1 |
|
throw new PathDirectoryIsNotValid( |
46
|
1 |
|
"Не установлена директория проекта или не удалось автоматически определить директорию" |
47
|
|
|
); |
48
|
|
|
} |
49
|
65 |
|
$this->projectDir = $projectDir; |
50
|
65 |
|
\putenv("ASSETS_PROJECT_DIRECTORY={$this->projectDir}/"); |
51
|
|
|
|
52
|
65 |
|
$this->compileDir = $this->setCompileDir($compileDir); |
53
|
65 |
|
$this->logger = new NullLogger(); |
54
|
65 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
65 |
|
public function getProjectDir(): string |
60
|
|
|
{ |
61
|
65 |
|
return $this->projectDir; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Функцию realpath() нельзя применять так как директории изначально может не быть, |
67
|
|
|
* она может потом быть создана, если будут права |
68
|
|
|
* @param string $path |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
65 |
|
private function setCompileDir(string $path): string |
72
|
|
|
{ |
73
|
65 |
|
if (str_starts_with($path, $this->getProjectDir())) { |
74
|
20 |
|
$path = str_replace($this->getProjectDir(), '', $path); |
75
|
|
|
} |
76
|
65 |
|
$path = $this->getProjectDir() . '/' . ltrim($path, '/\.'); |
77
|
65 |
|
return rtrim($path, '/'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
27 |
|
public function getCompileDir(): string |
84
|
|
|
{ |
85
|
27 |
|
return $this->compileDir; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $baseUrl |
91
|
|
|
* @return $this |
92
|
|
|
*/ |
93
|
34 |
|
public function setBaseUrl(string $baseUrl = '/'): Environment |
94
|
|
|
{ |
95
|
34 |
|
$this->baseUrl = rtrim($baseUrl, '\/'); |
96
|
34 |
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
16 |
|
public function getBaseUrl(): string |
103
|
|
|
{ |
104
|
16 |
|
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
|
9 |
|
public function getVersion(): ?string |
121
|
|
|
{ |
122
|
9 |
|
if ($this->version === null) { |
123
|
9 |
|
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
|
7 |
|
public function getStrategy(): int |
158
|
|
|
{ |
159
|
7 |
|
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
|
11 |
|
public function setStrategy(int $strategy): Environment |
172
|
|
|
{ |
173
|
11 |
|
$this->strategy = $strategy; |
174
|
11 |
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
14 |
|
public function getLogger(): LoggerInterface |
178
|
|
|
{ |
179
|
14 |
|
return $this->logger; |
180
|
|
|
} |
181
|
|
|
|
182
|
1 |
|
public function setLogger(LoggerInterface $logger): void |
183
|
|
|
{ |
184
|
1 |
|
$this->logger = $logger; |
185
|
1 |
|
} |
186
|
|
|
|
187
|
5 |
|
public function setMinifyJS(MinifyInterface $minifyImpl): void |
188
|
|
|
{ |
189
|
5 |
|
$this->minify['js'] = $minifyImpl; |
190
|
5 |
|
} |
191
|
|
|
|
192
|
8 |
|
public function setMinifyCSS(MinifyInterface $minifyImpl): void |
193
|
|
|
{ |
194
|
8 |
|
$this->minify['css'] = $minifyImpl; |
195
|
8 |
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param string $type |
199
|
|
|
* @return MinifyInterface |
200
|
|
|
*/ |
201
|
4 |
|
public function getMinify(string $type): MinifyInterface |
202
|
|
|
{ |
203
|
4 |
|
if (!array_key_exists($type, $this->minify)) { |
204
|
1 |
|
throw new UnexpectedParameters('Possible use only css or js'); |
205
|
|
|
} |
206
|
3 |
|
return $this->minify[$type]; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @return int |
211
|
|
|
*/ |
212
|
2 |
|
public function getDirectoryPermissions(): int |
213
|
|
|
{ |
214
|
2 |
|
return $this->directoryPermissions; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @param int $directoryPermissions |
219
|
|
|
*/ |
220
|
|
|
public function setDirectoryPermissions(int $directoryPermissions): void |
221
|
|
|
{ |
222
|
|
|
$this->directoryPermissions = $directoryPermissions; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|