|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Hyde\RealtimeCompiler; |
|
6
|
|
|
|
|
7
|
|
|
use Closure; |
|
8
|
|
|
use Hyde\Facades\Vite; |
|
9
|
|
|
use Hyde\Hyde; |
|
10
|
|
|
use Illuminate\Support\Str; |
|
11
|
|
|
use Illuminate\Support\Arr; |
|
12
|
|
|
use Illuminate\Support\Carbon; |
|
13
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput as SymfonyOutput; |
|
14
|
|
|
|
|
15
|
|
|
use function Termwind\render; |
|
16
|
|
|
|
|
17
|
|
|
class ConsoleOutput |
|
18
|
|
|
{ |
|
19
|
|
|
protected bool $verbose; |
|
20
|
|
|
protected SymfonyOutput $output; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(bool $verbose = false, ?SymfonyOutput $output = null) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->verbose = $verbose; |
|
25
|
|
|
$this->output = $output ?? new SymfonyOutput(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function printStartMessage(string $host, int $port, array $environment = [], ?bool $willUseVite = false): void |
|
29
|
|
|
{ |
|
30
|
|
|
$url = sprintf('%s://%s:%d', $port === 443 ? 'https' : 'http', $host, $port); |
|
31
|
|
|
|
|
32
|
|
|
$lines = Arr::whereNotNull([ |
|
33
|
|
|
'', |
|
34
|
|
|
sprintf('<span class="text-blue-500">%s</span> <span class="text-gray">%s</span>', 'HydePHP Realtime Compiler', 'v'.Hyde::getInstance()->version()), |
|
|
|
|
|
|
35
|
|
|
'', |
|
36
|
|
|
sprintf('<span class="text-white">Listening on:</span> <a href="%s" class="text-yellow-500">%s</a>', $url, $url), |
|
37
|
|
|
(config('hyde.server.dashboard.enabled') || Arr::has($environment, 'HYDE_SERVER_DASHBOARD')) && Arr::get($environment, 'HYDE_SERVER_DASHBOARD') === 'enabled' ? |
|
38
|
|
|
sprintf('<span class="text-white">Live dashboard:</span> <a href="%s/dashboard" class="text-yellow-500">%s/dashboard</a>', $url, $url) : null, |
|
39
|
|
|
(Vite::running() || $willUseVite) ? |
|
40
|
|
|
sprintf('<span class="text-white">Vite HMR server:</span> <a href="http://%s:5173" class="text-yellow-500">http://%s:5173</a>', $host, $host) : null, |
|
41
|
|
|
'', |
|
42
|
|
|
]); |
|
43
|
|
|
|
|
44
|
|
|
$lineLength = max(array_map('strlen', array_map('strip_tags', $lines))); |
|
45
|
|
|
|
|
46
|
|
|
$lines = array_map(function (string $line) use ($lineLength): string { |
|
47
|
|
|
return sprintf(' │ <span class="text-white">%s</span>%s│', |
|
48
|
|
|
$line, str_repeat(' ', ($lineLength - strlen(strip_tags($line))) + 1) |
|
49
|
|
|
); |
|
50
|
|
|
}, $lines); |
|
51
|
|
|
|
|
52
|
|
|
$topLine = sprintf(' ╭%s╮', str_repeat('─', $lineLength + 2)); |
|
53
|
|
|
$bottomLine = sprintf(' ╰%s╯', str_repeat('─', $lineLength + 2)); |
|
54
|
|
|
|
|
55
|
|
|
$body = implode('<br>', array_merge([''], [$topLine], $lines, [$bottomLine], [''])); |
|
56
|
|
|
|
|
57
|
|
|
render("<div class=\"text-green-500\">$body</div>"); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function getFormatter(): Closure |
|
61
|
|
|
{ |
|
62
|
|
|
return function (string $type, string $line): void { |
|
63
|
|
|
$this->handleOutput($line); |
|
64
|
|
|
}; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function printMessage(string $message, string $context): void |
|
68
|
|
|
{ |
|
69
|
|
|
$this->output->writeln(sprintf('%s ::context=[%s]', $message, $context)); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
protected function handleOutput(string $buffer): void |
|
73
|
|
|
{ |
|
74
|
|
|
str($buffer)->trim()->explode("\n")->each(function (string $line): void { |
|
75
|
|
|
$this->renderLine($this->formatLineForOutput($line)); |
|
76
|
|
|
}); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
protected function renderLine(?string $line): void |
|
80
|
|
|
{ |
|
81
|
|
|
if ($line !== null) { |
|
82
|
|
|
render($line); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
protected function formatLineForOutput(string $line): ?string |
|
87
|
|
|
{ |
|
88
|
|
|
if (str_contains($line, 'Development Server (http:')) { |
|
89
|
|
|
return $this->formatServerStartedLine($line); |
|
90
|
|
|
} |
|
91
|
|
|
if (str_contains($line, ']: ')) { |
|
92
|
|
|
return $this->formatRequestLine($line); |
|
93
|
|
|
} |
|
94
|
|
|
if (str_ends_with(trim($line), 'Accepted') || str_ends_with(trim($line), 'Closing')) { |
|
95
|
|
|
return $this->verbose ? $this->formatRequestStatusLine($line) : null; |
|
96
|
|
|
} |
|
97
|
|
|
if (str_contains($line, '::context=')) { |
|
98
|
|
|
return $this->formatContextLine($line); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $this->formatLine($line, Carbon::now()); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
protected function formatServerStartedLine(string $line): string |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->formatLine(sprintf('PHP %s Development Server started. <span class="text-yellow-500">Use Ctrl+C to stop.</span>', PHP_VERSION), $this->parseDate($line), 'green-500'); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
protected function formatRequestLine(string $line): string |
|
110
|
|
|
{ |
|
111
|
|
|
$dateString = Str::betweenFirst($line, '[', ']'); |
|
112
|
|
|
$message = substr($line, strlen($dateString) + 3); |
|
113
|
|
|
|
|
114
|
|
|
$statusCode = Str::between($message, ' [', ']:'); |
|
115
|
|
|
if ($statusCode >= 400) { |
|
116
|
|
|
$message = Str::replaceLast($statusCode, sprintf('<span class="text-red-500">%s</span>', $statusCode), $message); |
|
117
|
|
|
$iconColor = 'yellow-500'; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $this->formatLine($message, $this->parseDate($line), $iconColor ?? 'blue-500'); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
protected function formatRequestStatusLine(string $line): string |
|
124
|
|
|
{ |
|
125
|
|
|
$address = trim(Str::between($line, ']', ' ')); |
|
126
|
|
|
$status = str_contains($line, 'Accepted') ? 'Accepted' : 'Closing'; |
|
127
|
|
|
|
|
128
|
|
|
return $this->formatLine(sprintf('%s %s', $address, $status), $this->parseDate($line)); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
protected function formatContextLine(string $line): string |
|
132
|
|
|
{ |
|
133
|
|
|
$message = trim(Str::before($line, '::context='), '[]'); |
|
134
|
|
|
$context = Str::between($line, '::context=[', ']'); |
|
135
|
|
|
$success = str_contains($message, 'Created') || str_contains($message, 'Updated'); |
|
136
|
|
|
|
|
137
|
|
|
return $this->formatLine($message, Carbon::now(), $success ? 'green-500' : 'blue-500', $context); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
protected function formatLine(string $message, Carbon $date, string $iconColor = 'blue-500', string $context = ''): string |
|
141
|
|
|
{ |
|
142
|
|
|
if ($context) { |
|
143
|
|
|
$context = "$context "; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return sprintf(<<<'HTML' |
|
147
|
|
|
<div class="flex w-full justify-between"> |
|
148
|
|
|
<span> |
|
149
|
|
|
<span class="text-%s">i</span> |
|
150
|
|
|
%s |
|
151
|
|
|
</span> |
|
152
|
|
|
<span class="text-gray">%s%s</span> |
|
153
|
|
|
</div> |
|
154
|
|
|
HTML, |
|
155
|
|
|
$iconColor, $message, $context, $date->format('Y-m-d H:i:s') |
|
156
|
|
|
); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
protected function parseDate(string $line): Carbon |
|
160
|
|
|
{ |
|
161
|
|
|
return Carbon::parse(Str::betweenFirst($line, '[', ']')); |
|
162
|
|
|
} |
|
163
|
|
|
} |
|
164
|
|
|
|