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