Skip to content

Commit a77e97a

Browse files
committed
up: support check env before start php serve listen
1 parent be33564 commit a77e97a

File tree

1 file changed

+35
-10
lines changed

1 file changed

+35
-10
lines changed

src/Util/PhpDevServe.php

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use function explode;
1212
use function file_exists;
1313
use function file_get_contents;
14+
use function is_dir;
15+
use function is_file;
1416
use function random_int;
1517
use function strpos;
1618

@@ -173,28 +175,51 @@ public function config(Closure $fn): self
173175
* @throws Exception
174176
*/
175177
public function listen(): void
178+
{
179+
if ($fn = $this->beforeStart) {
180+
$fn($this);
181+
} else {
182+
$this->printDefaultMessage();
183+
}
184+
185+
$command = $this->getCommand();
186+
187+
Cli::write("<cyan>></cyan> <darkGray>$command</darkGray>");
188+
Sys::execute($command);
189+
}
190+
191+
/**
192+
* build full command line string
193+
*
194+
* @param bool $checkEnv
195+
*
196+
* @return string
197+
* @throws Exception
198+
*/
199+
public function getCommand(bool $checkEnv = true): string
176200
{
177201
$phpBin = $this->getPhpBin();
178202
$svrAddr = $this->getServerAddr();
179203
// command eg: "php -S 127.0.0.1:8080 -t web web/index.php";
180204
$command = "$phpBin -S {$svrAddr}";
181205

182-
if ($this->docRoot) {
183-
$command .= " -t {$this->docRoot}";
206+
if ($docRoot = $this->docRoot) {
207+
if ($checkEnv && !is_dir($docRoot)) {
208+
throw new RuntimeException("the document root is not exists. path: $docRoot");
209+
}
210+
211+
$command .= " -t {$docRoot}";
184212
}
185213

186214
if ($entryFile = $this->getEntryFile()) {
187-
$command .= " $entryFile";
188-
}
215+
if ($checkEnv && !is_file($entryFile)) {
216+
throw new RuntimeException("the entry file is not exists. path: $entryFile");
217+
}
189218

190-
if ($fn = $this->beforeStart) {
191-
$fn($this);
192-
} else {
193-
$this->printDefaultMessage();
219+
$command .= " $entryFile";
194220
}
195221

196-
Cli::write("<cyan>></cyan> <darkGray>$command</darkGray>");
197-
Sys::execute($command);
222+
return $command;
198223
}
199224

200225
/**

0 commit comments

Comments
 (0)