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
|
210 |
|
public function __construct(Porter $porter, Cli $cli, ConsoleWriter $writer) |
33
|
|
|
{ |
34
|
210 |
|
$this->porter = $porter; |
35
|
210 |
|
$this->cli = $cli; |
36
|
210 |
|
$this->writer = $writer; |
37
|
|
|
} |
38
|
|
|
|
39
|
1 |
|
public function turnOn() |
40
|
|
|
{ |
41
|
1 |
|
if (setting('use_valet', 'off') === 'on') { |
42
|
|
|
$this->writer->info('Valet compatibility already complete'); |
43
|
|
|
|
44
|
|
|
return; |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
$this->sudoWarning(); |
48
|
|
|
|
49
|
1 |
|
Setting::updateOrCreate('use_valet', 'on'); |
50
|
1 |
|
Setting::updateOrCreate('http_port', static::COMPAT_HTTP_PORT); |
51
|
1 |
|
Setting::updateOrCreate('https_port', static::COMPAT_HTTPS_PORT); |
52
|
|
|
|
53
|
1 |
|
Artisan::call('dns:off'); |
54
|
1 |
|
$this->cli->execRealTime('valet start'); |
55
|
|
|
|
56
|
1 |
|
$this->porter->compose(); |
57
|
1 |
|
$this->porter->restart(); |
58
|
|
|
|
59
|
|
|
// Run through all the porter sites and set up a proxy though valet... |
60
|
1 |
|
Site::all()->each(function (Site $site) { |
61
|
1 |
|
$this->addSite($site); |
62
|
|
|
}); |
63
|
|
|
|
64
|
|
|
$this->writer->line('Completed setting up valet compatibility'); |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
public function turnOff() |
68
|
|
|
{ |
69
|
2 |
|
if (setting('use_valet', 'off') === 'off') { |
70
|
1 |
|
$this->writer->info('Valet compatibility already off'); |
71
|
|
|
|
72
|
1 |
|
return; |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
$this->sudoWarning(); |
76
|
|
|
|
77
|
|
|
// Run through all the porter sites and set up a proxy though valet... |
78
|
1 |
|
Site::all()->each(function (Site $site) { |
79
|
1 |
|
$this->removeSite($site); |
80
|
|
|
}); |
81
|
|
|
|
82
|
|
|
Setting::updateOrCreate('use_valet', 'off'); |
83
|
|
|
Setting::updateOrCreate('http_port', static::HTTP_PORT); |
84
|
|
|
Setting::updateOrCreate('https_port', static::HTTPS_PORT); |
85
|
|
|
|
86
|
|
|
$this->cli->execRealTime('valet stop'); |
87
|
|
|
$this->cli->execRealTime('sudo brew services stop dnsmasq'); |
88
|
|
|
|
89
|
|
|
Artisan::call('dns:on'); |
90
|
|
|
|
91
|
|
|
$this->porter->compose(); |
92
|
|
|
$this->porter->restart(); |
93
|
|
|
|
94
|
|
|
$this->writer->line('Completed removing valet compatibility'); |
95
|
|
|
} |
96
|
|
|
|
97
|
2 |
|
public function addSite(Site $site) |
98
|
|
|
{ |
99
|
2 |
|
$this->sudoWarning(); |
100
|
|
|
|
101
|
2 |
|
if ($this->isProxied($site)) { |
102
|
1 |
|
$this->removeSite($site); |
103
|
|
|
} |
104
|
|
|
|
105
|
2 |
|
$port = $site->secure ? static::COMPAT_HTTPS_PORT : static::COMPAT_HTTP_PORT; |
106
|
2 |
|
$protocol = $site->secure ? 'https://' : 'http://'; |
107
|
2 |
|
$secure = $site->secure ? ' --secure ' : ''; |
108
|
|
|
|
109
|
2 |
|
$this->cli->exec("valet proxy{$secure}{$site->name} {$protocol}127.0.0.1:{$port}"); |
110
|
|
|
|
111
|
|
|
$this->writer->line("Added {$site->name} proxy for Valet"); |
112
|
|
|
} |
113
|
|
|
|
114
|
2 |
|
public function removeSite(Site $site) |
115
|
|
|
{ |
116
|
2 |
|
if (! $this->isProxied($site)) { |
117
|
|
|
$this->writer->line("No proxy for {$site->name}"); |
118
|
|
|
return; |
119
|
|
|
} |
120
|
|
|
|
121
|
1 |
|
$this->sudoWarning(); |
122
|
|
|
|
123
|
1 |
|
$this->cli->exec("valet unproxy {$site->name}"); |
124
|
|
|
|
125
|
1 |
|
$this->writer->line("Removed Valet proxy for {$site->name}"); |
126
|
|
|
} |
127
|
|
|
|
128
|
5 |
|
public function listSites() |
129
|
|
|
{ |
130
|
5 |
|
$this->sudoWarning(); |
131
|
|
|
|
132
|
5 |
|
return $this->cli->exec('valet proxies'); |
133
|
|
|
} |
134
|
|
|
|
135
|
4 |
|
public function isProxied(Site $site) |
136
|
|
|
{ |
137
|
4 |
|
return Str::contains($this->listSites(), $site->name); |
138
|
|
|
} |
139
|
|
|
|
140
|
5 |
|
protected function sudoWarning() |
141
|
|
|
{ |
142
|
5 |
|
if ($this->hasWarned) { |
143
|
4 |
|
return; |
144
|
|
|
} |
145
|
|
|
|
146
|
5 |
|
$this->writer->info('Requires Sudo permissions for Valet'); |
147
|
5 |
|
$this->hasWarned = true; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|