|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Models; |
|
4
|
|
|
|
|
5
|
|
|
use App\Porter; |
|
6
|
|
|
use App\PorterLibrary; |
|
7
|
|
|
use App\Support\Contracts\Cli; |
|
8
|
|
|
use App\Support\Nginx\SiteConfBuilder; |
|
9
|
|
|
use App\Support\Ssl\CertificateBuilder; |
|
10
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
11
|
|
|
|
|
12
|
|
|
class Site extends Model |
|
13
|
|
|
{ |
|
14
|
|
|
protected $guarded = []; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* PHP Version |
|
18
|
|
|
* |
|
19
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
20
|
|
|
*/ |
|
21
|
3 |
|
public function php_version() |
|
22
|
|
|
{ |
|
23
|
3 |
|
return $this->belongsTo(PhpVersion::class); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Resolve the site from the current working directory |
|
28
|
|
|
* |
|
29
|
|
|
* @param null $path |
|
|
|
|
|
|
30
|
|
|
* @return null |
|
31
|
|
|
*/ |
|
32
|
4 |
|
public static function resolveFromPathOrCurrentWorkingDirectory($path = null) |
|
33
|
|
|
{ |
|
34
|
4 |
|
$name = static::nameFromPath($path ?: app(Cli::class)->currentWorkingDirectory()); |
|
35
|
|
|
|
|
36
|
4 |
|
if (! $name) { |
|
37
|
|
|
return null; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
4 |
|
return static::where('name', $name)->first(); |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Resolve the site from the current working directory |
|
45
|
|
|
* Fail if not found. |
|
46
|
|
|
* |
|
47
|
|
|
* @param null $path |
|
|
|
|
|
|
48
|
|
|
* @return null |
|
49
|
|
|
* @throws \Exception |
|
50
|
|
|
*/ |
|
51
|
1 |
|
public static function resolveFromPathOrCurrentWorkingDirectoryOrFail($path = null) |
|
52
|
|
|
{ |
|
53
|
1 |
|
$site = static::resolveFromPathOrCurrentWorkingDirectory($path); |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
1 |
|
if (! $site) { |
|
|
|
|
|
|
56
|
1 |
|
throw new \Exception("Site not found."); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
return $site; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Get the url for this site |
|
64
|
|
|
* |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
7 |
|
public function getUrlAttribute() |
|
68
|
|
|
{ |
|
69
|
7 |
|
return $this->name.'.'.setting('domain'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get the scheme for this site |
|
74
|
|
|
* |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
1 |
|
public function getSchemeAttribute() |
|
78
|
|
|
{ |
|
79
|
1 |
|
return ($this->secure ? 'https' : 'http').'://'; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Return the path for the NGiNX config file |
|
84
|
|
|
* |
|
85
|
|
|
* @return string |
|
86
|
|
|
*/ |
|
87
|
2 |
|
public function getNginxConfPathAttribute() |
|
88
|
|
|
{ |
|
89
|
2 |
|
return $this->getPorterLibrary()->configPath()."/nginx/conf.d/{$this->name}.conf"; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Return the full NGiNX template to use |
|
94
|
|
|
* |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
2 |
|
public function getNginxConfTemplateAttribute() |
|
98
|
|
|
{ |
|
99
|
2 |
|
$type = $this->nginx_conf ?? 'default'; |
|
100
|
|
|
|
|
101
|
2 |
|
return "nginx.{$type}.domain" . (($this->secure ?? false) ? '_secure' : ''); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Build the files for this site (e.g. nginx conf) |
|
106
|
|
|
* |
|
107
|
|
|
* @throws \Throwable |
|
108
|
|
|
*/ |
|
109
|
6 |
|
public function buildFiles() |
|
110
|
|
|
{ |
|
111
|
6 |
|
$this->getSiteConfigBuilder()->build($this); |
|
112
|
6 |
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Destroy the files for this site (e.g. NGiNX conf) |
|
116
|
|
|
*/ |
|
117
|
1 |
|
public function destroyFiles() |
|
118
|
|
|
{ |
|
119
|
1 |
|
$this->getSiteConfigBuilder()->destroy($this); |
|
120
|
1 |
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Secure the site. Build certs. |
|
124
|
|
|
*/ |
|
125
|
1 |
|
public function secure() |
|
126
|
|
|
{ |
|
127
|
1 |
|
$this->buildCertificate(); |
|
128
|
|
|
|
|
129
|
1 |
|
$this->update(['secure' => true]); |
|
130
|
|
|
|
|
131
|
1 |
|
$this->buildFiles(); |
|
132
|
|
|
|
|
133
|
1 |
|
$this->getPorter()->restartServing(); |
|
134
|
1 |
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Unsecure this site |
|
138
|
|
|
*/ |
|
139
|
1 |
|
public function unsecure() |
|
140
|
|
|
{ |
|
141
|
1 |
|
$this->destroyCertificate(); |
|
142
|
|
|
|
|
143
|
1 |
|
$this->update(['secure' => false]); |
|
144
|
|
|
|
|
145
|
1 |
|
$this->buildFiles(); |
|
146
|
|
|
|
|
147
|
1 |
|
$this->getPorter()->restartServing(); |
|
148
|
1 |
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Remove this site and associated files |
|
152
|
|
|
* |
|
153
|
|
|
* @throws \Exception |
|
154
|
|
|
* @throws \Throwable |
|
155
|
|
|
*/ |
|
156
|
1 |
|
public function remove() |
|
157
|
|
|
{ |
|
158
|
1 |
|
$this->destroyCertificate(); |
|
159
|
|
|
|
|
160
|
1 |
|
$this->getSiteConfigBuilder()->destroy($this); |
|
161
|
|
|
|
|
162
|
1 |
|
$this->delete(); |
|
163
|
|
|
|
|
164
|
1 |
|
$this->getPorter()->restartServing(); |
|
165
|
1 |
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Set the PHP version for the site |
|
169
|
|
|
* |
|
170
|
|
|
* @param int|null $phpVersionId |
|
171
|
|
|
* @throws \Throwable |
|
172
|
|
|
*/ |
|
173
|
2 |
|
public function setPhpVersion($phpVersionId = null) |
|
174
|
|
|
{ |
|
175
|
2 |
|
$this->update(['php_version_id' => $phpVersionId ?: PhpVersion::defaultVersion()->id]); |
|
176
|
|
|
|
|
177
|
2 |
|
$this->buildFiles(); |
|
178
|
|
|
|
|
179
|
2 |
|
$this->getPorter()->restartServing(); |
|
180
|
2 |
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Set the nginx type for the site (we have different template configs we can use) |
|
184
|
|
|
* |
|
185
|
|
|
* @param $type |
|
186
|
|
|
* @throws \Throwable |
|
187
|
|
|
*/ |
|
188
|
1 |
|
public function setNginxType($type) |
|
189
|
|
|
{ |
|
190
|
1 |
|
$this->update(['nginx_conf' => $type ?? 'default']); |
|
191
|
|
|
|
|
192
|
1 |
|
$this->buildFiles(); |
|
193
|
|
|
|
|
194
|
1 |
|
$this->getPorter()->restartServing(); |
|
195
|
1 |
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Get the first site based on name, or create a new record. |
|
199
|
|
|
* |
|
200
|
|
|
* |
|
201
|
|
|
* @param $name |
|
202
|
|
|
* @return mixed |
|
203
|
|
|
*/ |
|
204
|
1 |
|
public static function firstOrCreateForName($name) |
|
205
|
|
|
{ |
|
206
|
1 |
|
$result = static::where('name', $name)->first(); |
|
207
|
|
|
|
|
208
|
1 |
|
if ($result) { |
|
209
|
1 |
|
return $result; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
1 |
|
return static::createForName($name); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Create an new site based on the name |
|
217
|
|
|
* |
|
218
|
|
|
* @param $name |
|
219
|
|
|
* @return mixed |
|
220
|
|
|
*/ |
|
221
|
2 |
|
public static function createForName($name) |
|
222
|
|
|
{ |
|
223
|
2 |
|
return static::create([ |
|
224
|
2 |
|
'name' => $name, |
|
225
|
2 |
|
'nginx_conf' => 'default', |
|
226
|
2 |
|
'php_version_id' => PhpVersion::defaultVersion()->id, |
|
227
|
|
|
'secure' => false, |
|
228
|
|
|
]); |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
/** |
|
232
|
|
|
* Get Certificate builder |
|
233
|
|
|
* |
|
234
|
|
|
* @return CertificateBuilder |
|
235
|
|
|
*/ |
|
236
|
5 |
|
protected function getCertificateBuilder() |
|
237
|
|
|
{ |
|
238
|
5 |
|
return app(CertificateBuilder::class); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Get Site Config Builder |
|
243
|
|
|
* |
|
244
|
|
|
* @return \App\Support\Nginx\SiteConfBuilder |
|
245
|
|
|
*/ |
|
246
|
8 |
|
protected function getSiteConfigBuilder() |
|
247
|
|
|
{ |
|
248
|
8 |
|
return app(SiteConfBuilder::class); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* Get Porter |
|
253
|
|
|
* |
|
254
|
|
|
* @return Porter |
|
255
|
|
|
*/ |
|
256
|
6 |
|
protected function getPorter() |
|
257
|
|
|
{ |
|
258
|
6 |
|
return app(Porter::class); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Get PorterLibrary |
|
263
|
|
|
* |
|
264
|
|
|
* @return PorterLibrary |
|
265
|
|
|
*/ |
|
266
|
2 |
|
protected function getPorterLibrary() |
|
267
|
|
|
{ |
|
268
|
2 |
|
return app(PorterLibrary::class); |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
/** |
|
272
|
|
|
* Build Certificate for site |
|
273
|
|
|
*/ |
|
274
|
2 |
|
public function buildCertificate() |
|
275
|
|
|
{ |
|
276
|
2 |
|
$this->getCertificateBuilder()->build($this->url); |
|
277
|
2 |
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* Destroy Certificate for site |
|
281
|
|
|
*/ |
|
282
|
3 |
|
public function destroyCertificate() |
|
283
|
|
|
{ |
|
284
|
3 |
|
$this->getCertificateBuilder()->destroy($this->url); |
|
285
|
3 |
|
} |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* Return a site directory name from a path, after checking it is within the |
|
289
|
|
|
* home directory. |
|
290
|
|
|
* |
|
291
|
|
|
* @param $path |
|
292
|
|
|
* @return null|string |
|
293
|
|
|
*/ |
|
294
|
6 |
|
public static function nameFromPath($path) |
|
295
|
|
|
{ |
|
296
|
6 |
|
$home = setting('home'); |
|
297
|
|
|
|
|
298
|
6 |
|
if (strpos($path, $home) !== 0) { |
|
299
|
1 |
|
return null; |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
5 |
|
$path = trim(str_after($path, $home), DIRECTORY_SEPARATOR); |
|
303
|
|
|
|
|
304
|
5 |
|
return str_before($path, DIRECTORY_SEPARATOR); |
|
305
|
|
|
} |
|
306
|
|
|
} |
|
307
|
|
|
|