Passed
Push — master ( 01c919...f44c53 )
by Caen
03:47 queued 12s
created

ServeCommand::runServerProcess()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
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=localhost} {--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->option('host'),
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 getExecutablePath(): string
45
    {
46
        return Hyde::path('vendor/hyde/realtime-compiler/bin/server.php');
47
    }
48
49
    protected function runServerProcess(string $command): void
50
    {
51
        Process::run($command, function ($type, $line) {
52
            $this->output->write($line);
53
        });
54
    }
55
}
56