|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Hyde\Console\Commands; |
|
6
|
|
|
|
|
7
|
|
|
use Closure; |
|
8
|
|
|
use Hyde\Hyde; |
|
9
|
|
|
use Hyde\Facades\Config; |
|
10
|
|
|
use Hyde\RealtimeCompiler\ConsoleOutput; |
|
11
|
|
|
use Illuminate\Support\Facades\Process; |
|
12
|
|
|
use LaravelZero\Framework\Commands\Command; |
|
13
|
|
|
|
|
14
|
|
|
use function sprintf; |
|
15
|
|
|
use function class_exists; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Start the realtime compiler server. |
|
19
|
|
|
* |
|
20
|
|
|
* @see https://github.com/hydephp/realtime-compiler |
|
21
|
|
|
*/ |
|
22
|
|
|
class ServeCommand extends Command |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var string */ |
|
25
|
|
|
protected $signature = 'serve {--host= : <comment>[default: "localhost"]</comment>}} {--port= : <comment>[default: 8080]</comment>}'; |
|
26
|
|
|
|
|
27
|
|
|
/** @var string */ |
|
28
|
|
|
protected $description = 'Start the realtime compiler server.'; |
|
29
|
|
|
|
|
30
|
|
|
public function handle(): int |
|
31
|
|
|
{ |
|
32
|
|
|
$this->printStartMessage(); |
|
33
|
|
|
|
|
34
|
|
|
$this->runServerProcess(sprintf('php -S %s:%d %s', |
|
35
|
|
|
$this->getHostSelection(), |
|
36
|
|
|
$this->getPortSelection(), |
|
37
|
|
|
$this->getExecutablePath() |
|
38
|
|
|
)); |
|
39
|
|
|
|
|
40
|
|
|
return Command::SUCCESS; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
protected function getPortSelection(): int |
|
44
|
|
|
{ |
|
45
|
|
|
return (int) ($this->option('port') ?: Config::getInt('hyde.server.port', 8080)); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
protected function getHostSelection(): string |
|
49
|
|
|
{ |
|
50
|
|
|
return (string) $this->option('host') ?: Config::getString('hyde.server.host', 'localhost'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
protected function getExecutablePath(): string |
|
54
|
|
|
{ |
|
55
|
|
|
return Hyde::path('vendor/hyde/realtime-compiler/bin/server.php'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
protected function runServerProcess(string $command): void |
|
59
|
|
|
{ |
|
60
|
|
|
Process::forever()->env($this->getEnvironmentVariables())->run($command, $this->getOutputHandler()); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
protected function getEnvironmentVariables(): array |
|
64
|
|
|
{ |
|
65
|
|
|
return [ |
|
66
|
|
|
'HYDE_RC_REQUEST_OUTPUT' => ! $this->option('no-ansi'), |
|
67
|
|
|
]; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
protected function printStartMessage(): void |
|
71
|
|
|
{ |
|
72
|
|
|
$this->useBasicOutput() |
|
73
|
|
|
? $this->line('<info>Starting the HydeRC server...</info> Press Ctrl+C to stop') |
|
74
|
|
|
: ConsoleOutput::printStartMessage($this->getHostSelection(), $this->getPortSelection()); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
protected function getOutputHandler(): Closure |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->useBasicOutput() ? function (string $type, string $line): void { |
|
80
|
|
|
$this->output->write($line); |
|
81
|
|
|
} : ConsoleOutput::getFormatter($this->output->isVerbose()); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
protected function useBasicOutput(): bool |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->option('no-ansi') || ! class_exists(ConsoleOutput::class); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|