1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Support\Valet; |
4
|
|
|
|
5
|
|
|
use App\Models\Setting; |
6
|
|
|
use App\Models\Site; |
7
|
|
|
use App\Porter; |
8
|
|
|
use App\Support\Console\Cli; |
9
|
|
|
use App\Support\Console\ConsoleWriter; |
10
|
|
|
use Illuminate\Support\Facades\Artisan; |
11
|
|
|
use Illuminate\Support\Str; |
12
|
|
|
|
13
|
|
|
class Valet |
14
|
|
|
{ |
15
|
|
|
/** @var Porter */ |
16
|
|
|
protected $porter; |
17
|
|
|
|
18
|
|
|
/** @var Cli */ |
19
|
|
|
protected $cli; |
20
|
|
|
|
21
|
|
|
/** @var ConsoleWriter */ |
22
|
|
|
protected $writer; |
23
|
|
|
|
24
|
|
|
/** @var bool hasWarned */ |
25
|
|
|
protected $hasWarned; |
26
|
|
|
|
27
|
|
|
protected const HTTP_PORT = 80; |
28
|
|
|
protected const HTTPS_PORT = 443; |
29
|
|
|
protected const COMPAT_HTTP_PORT = 8008; // 8080 is needed by the node container at the moment |
30
|
|
|
protected const COMPAT_HTTPS_PORT = 8443; |
31
|
|
|
|
32
|
204 |
|
public function __construct(Porter $porter, Cli $cli, ConsoleWriter $writer) |
33
|
|
|
{ |
34
|
204 |
|
$this->porter = $porter; |
35
|
204 |
|
$this->cli = $cli; |
36
|
204 |
|
$this->writer = $writer; |
37
|
204 |
|
} |
38
|
|
|
|
39
|
1 |
|
public function turnOn() |
40
|
|
|
{ |
41
|
1 |
|
$this->sudoWarning(); |
42
|
|
|
|
43
|
1 |
|
Setting::updateOrCreate('valet', 'on'); |
44
|
1 |
|
Setting::updateOrCreate('http_port', static::COMPAT_HTTP_PORT); |
45
|
1 |
|
Setting::updateOrCreate('https_port', static::COMPAT_HTTPS_PORT); |
46
|
|
|
|
47
|
1 |
|
Artisan::call('dns:off'); |
48
|
1 |
|
$this->cli->execRealTime('valet start'); |
49
|
|
|
|
50
|
1 |
|
$this->porter->compose(); |
51
|
1 |
|
$this->porter->restart(); |
52
|
|
|
|
53
|
|
|
// Run through all the porter sites and set up a proxy though valet... |
54
|
|
|
Site::all()->each(function (Site $site) { |
55
|
1 |
|
$this->addSite($site); |
56
|
1 |
|
}); |
57
|
|
|
|
58
|
1 |
|
$this->writer->line('Completed setting up valet compatibility'); |
59
|
1 |
|
} |
60
|
|
|
|
61
|
1 |
|
public function turnOff() |
62
|
|
|
{ |
63
|
1 |
|
$this->sudoWarning(); |
64
|
|
|
|
65
|
|
|
// Run through all the porter sites and set up a proxy though valet... |
66
|
|
|
Site::all()->each(function (Site $site) { |
67
|
1 |
|
$this->removeSite($site); |
68
|
1 |
|
}); |
69
|
|
|
|
70
|
1 |
|
Setting::updateOrCreate('valet', 'off'); |
71
|
1 |
|
Setting::updateOrCreate('http_port', static::HTTP_PORT); |
72
|
1 |
|
Setting::updateOrCreate('https_port', static::HTTPS_PORT); |
73
|
|
|
|
74
|
1 |
|
$this->cli->execRealTime('valet stop'); |
75
|
|
|
|
76
|
1 |
|
Artisan::call('dns:on'); |
77
|
|
|
|
78
|
1 |
|
$this->porter->compose(); |
79
|
1 |
|
$this->porter->restart(); |
80
|
|
|
|
81
|
1 |
|
$this->writer->line('Completed removing valet compatibility'); |
82
|
1 |
|
} |
83
|
|
|
|
84
|
3 |
|
public function addSite(Site $site) |
85
|
|
|
{ |
86
|
3 |
|
$this->sudoWarning(); |
87
|
|
|
|
88
|
3 |
|
if ($this->isProxied($site)) { |
89
|
1 |
|
$this->removeSite($site); |
90
|
|
|
} |
91
|
|
|
|
92
|
3 |
|
$port = $site->secure ? static::COMPAT_HTTPS_PORT : static::COMPAT_HTTP_PORT; |
93
|
3 |
|
$protocol = $site->secure ? 'https://' : 'http://'; |
94
|
|
|
|
95
|
3 |
|
$this->cli->exec("valet proxy {$site->name} {$protocol}127.0.0.1:{$port}"); |
96
|
|
|
|
97
|
3 |
|
$this->writer->line("Added {$site->name} proxy for Valet"); |
98
|
3 |
|
} |
99
|
|
|
|
100
|
3 |
|
public function removeSite(Site $site) |
101
|
|
|
{ |
102
|
3 |
|
$this->sudoWarning(); |
103
|
|
|
|
104
|
3 |
|
$this->cli->exec("valet unproxy {$site->name}"); |
105
|
|
|
|
106
|
3 |
|
$this->writer->line("Removed Valet proxy for {$site->name}"); |
107
|
3 |
|
} |
108
|
|
|
|
109
|
5 |
|
public function listSites() |
110
|
|
|
{ |
111
|
5 |
|
$this->sudoWarning(); |
112
|
|
|
|
113
|
5 |
|
return $this->cli->exec('valet proxies'); |
114
|
|
|
} |
115
|
|
|
|
116
|
4 |
|
public function isProxied(Site $site) |
117
|
|
|
{ |
118
|
4 |
|
return Str::contains($this->listSites(), $site->name); |
119
|
|
|
} |
120
|
|
|
|
121
|
7 |
|
protected function sudoWarning() |
122
|
|
|
{ |
123
|
7 |
|
if ($this->hasWarned) { |
124
|
5 |
|
return; |
125
|
|
|
} |
126
|
|
|
|
127
|
7 |
|
$this->writer->info('Requires Sudo permissions for Valet'); |
128
|
7 |
|
$this->hasWarned = true; |
129
|
7 |
|
} |
130
|
|
|
} |
131
|
|
|
|