-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver
145 lines (112 loc) · 3.85 KB
/
server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env php
<?php
use Dotenv\Dotenv;
use Spatie\Watcher\Watch;
use Symfony\Component\Process\Process;
use Spatie\Watcher\Exceptions\CouldNotStartWatcher;
require_once __DIR__ . '/vendor/autoload.php';
if (php_sapi_name() !== 'cli') {
exit;
}
$config = Dotenv::createArrayBacked(__DIR__, '.env')->load();
class Watcher extends Watch
{
protected string $host;
protected int $port;
protected int|null $pid;
protected Process $serverProcess;
public function __construct(
array $paths,
array $config = []
) {
parent::__construct();
$this->setPaths($paths);
$this->host = $config['APP_URL'] ?? 'http://127.0.0.1';
$this->port = $config['APP_PORT'] ?? 1337;
}
public function start(): void
{
$watcher = $this->getWatchProcess();
while (true) {
if (! $watcher->isRunning()) {
throw CouldNotStartWatcher::make($watcher);
}
if ($output = $watcher->getIncrementalOutput()) {
$this->actOnOutput($output);
}
if ($this->serverProcess->isRunning()) {
echo $this->serverProcess->getIncrementalOutput();
echo $this->serverProcess->getIncrementalErrorOutput();
}
if (! ($this->shouldContinue)()) {
break;
}
usleep($this->interval);
}
}
public function watch(): void {
echo "Watching for changes..." . PHP_EOL . PHP_EOL;
$this->onAnyChange(function (): void {
$this->killExistingProcess();
$this->runServer();
})
->start();
}
public function systemIsReady(): bool
{
$packageName = 'chokidar';
$checkCommand = 'npm list ' . escapeshellarg($packageName) . ' --depth=0';
$process = Process::fromShellCommandline($checkCommand);
$process->run();
if ($process->isSuccessful() && strpos($process->getOutput(), $packageName) !== false) {
return true;
} else {
echo "Chokidar is not installed. Installing..." . PHP_EOL;
$installCommand = 'npm install ' . escapeshellarg($packageName);
$installProcess = Process::fromShellCommandline($installCommand);
$installProcess->run();
if ($installProcess->isSuccessful()) {
echo "Chokidar installed successfully." . PHP_EOL;
return true;
} else {
echo "Failed to install chokidar. Please check your npm configuration." . PHP_EOL;
echo $installProcess->getErrorOutput();
return false;
}
}
}
public function runServer(): void {
$this->serverProcess = Process::fromShellCommandline("php public/index.php");
$this->serverProcess->setTimeout(null);
$this->serverProcess->start();
$this->pid = $this->serverProcess->getPid();
echo "Server started on {$this->host}:{$this->port}" . PHP_EOL . PHP_EOL;
}
protected function killExistingProcess()
{
if ($this->pid) {
echo "Restarting server..." . PHP_EOL . PHP_EOL;
$killProcess = Process::fromShellCommandline('kill ' . escapeshellarg($this->pid));
$killProcess->run();
echo "Server was stopped (PID {$this->pid})" . PHP_EOL . PHP_EOL;
}
}
}
try {
$watcher = new Watcher([
__DIR__ . '/app',
__DIR__ . '/config',
__DIR__ . '/routes',
__DIR__ . '/database',
__DIR__ . '/composer.json',
__DIR__ . '/.env',
], $config);
if ($watcher->systemIsReady()) {
$watcher->runServer();
$watcher->watch();
} else {
echo "System is not ready. Exiting..." . PHP_EOL . PHP_EOL;
}
} catch (Throwable $th) {
echo $th->getMessage();
}