Passed
Push — master ( f44c53...21c685 )
by Caen
03:57 queued 12s
created

ServeCommand::getHostSelection()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Console\Commands;
6
7
use Illuminate\Support\Facades\Process;
8
use function config;
9
use Hyde\Hyde;
10
use LaravelZero\Framework\Commands\Command;
11
use function sprintf;
12
13
/**
14
 * Start the realtime compiler server.
15
 *
16
 * @see https://github.com/hydephp/realtime-compiler
17
 */
18
class ServeCommand extends Command
19
{
20
    /** @var string */
21
    protected $signature = 'serve {--host= : <comment>[default: "localhost"]</comment>}} {--port= : <comment>[default: 8080]</comment>}';
22
23
    /** @var string */
24
    protected $description = 'Start the realtime compiler server.';
25
26
    public function handle(): int
27
    {
28
        $this->line('<info>Starting the HydeRC server...</info> Press Ctrl+C to stop');
29
30
        $this->runServerProcess(sprintf('php -S %s:%d %s',
31
            $this->getHostSelection() ?: 'localhost',
32
            $this->getPortSelection() ?: 8080,
33
            $this->getExecutablePath()
34
        ));
35
36
        return Command::SUCCESS;
37
    }
38
39
    protected function getPortSelection(): int
40
    {
41
        return (int) ($this->option('port') ?: config('hyde.server.port', 8080));
42
    }
43
44
    protected function getHostSelection(): string
45
    {
46
        return $this->option('host') ?: config('hyde.server.host', 'localhost');
47
    }
48
49
    protected function getExecutablePath(): string
50
    {
51
        return Hyde::path('vendor/hyde/realtime-compiler/bin/server.php');
52
    }
53
54
    protected function runServerProcess(string $command): void
55
    {
56
        Process::run($command, function ($type, $line) {
57
            $this->output->write($line);
58
        });
59
    }
60
}
61