Passed
Push — master ( 619655...cd9add )
by Caen
03:39 queued 14s
created

ServeCommand::printStartMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 5
rs 10
c 2
b 0
f 0
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
    protected ConsoleOutput $console;
31
32
    public function handle(): int
33
    {
34
        $this->configureOutput();
35
        $this->printStartMessage();
36
37
        $this->runServerProcess(sprintf('php -S %s:%d %s',
38
            $this->getHostSelection(),
39
            $this->getPortSelection(),
40
            $this->getExecutablePath()
41
        ));
42
43
        return Command::SUCCESS;
44
    }
45
46
    protected function getPortSelection(): int
47
    {
48
        return (int) ($this->option('port') ?: Config::getInt('hyde.server.port', 8080));
49
    }
50
51
    protected function getHostSelection(): string
52
    {
53
        return (string) $this->option('host') ?: Config::getString('hyde.server.host', 'localhost');
54
    }
55
56
    protected function getExecutablePath(): string
57
    {
58
        return Hyde::path('vendor/hyde/realtime-compiler/bin/server.php');
59
    }
60
61
    protected function runServerProcess(string $command): void
62
    {
63
        Process::forever()->env($this->getEnvironmentVariables())->run($command, $this->getOutputHandler());
64
    }
65
66
    protected function getEnvironmentVariables(): array
67
    {
68
        return [
69
            'HYDE_RC_REQUEST_OUTPUT' => ! $this->option('no-ansi'),
70
        ];
71
    }
72
73
    protected function configureOutput(): void
74
    {
75
        if (! $this->useBasicOutput()) {
76
            $this->console = new ConsoleOutput($this->output->isVerbose());
77
        }
78
    }
79
80
    protected function printStartMessage(): void
81
    {
82
        $this->useBasicOutput()
83
            ? $this->output->writeln('<info>Starting the HydeRC server...</info> Press Ctrl+C to stop')
84
            : $this->console->printStartMessage($this->getHostSelection(), $this->getPortSelection());
85
    }
86
87
    protected function getOutputHandler(): Closure
88
    {
89
        return $this->useBasicOutput() ? function (string $type, string $line): void {
90
            $this->output->write($line);
91
        } : $this->console->getFormatter();
92
    }
93
94
    protected function useBasicOutput(): bool
95
    {
96
        return $this->option('no-ansi') || ! class_exists(ConsoleOutput::class);
97
    }
98
}
99