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