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