|
11 | 11 | use function explode; |
12 | 12 | use function file_exists; |
13 | 13 | use function file_get_contents; |
| 14 | +use function is_dir; |
| 15 | +use function is_file; |
14 | 16 | use function random_int; |
15 | 17 | use function strpos; |
16 | 18 |
|
@@ -173,28 +175,51 @@ public function config(Closure $fn): self |
173 | 175 | * @throws Exception |
174 | 176 | */ |
175 | 177 | 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 |
176 | 200 | { |
177 | 201 | $phpBin = $this->getPhpBin(); |
178 | 202 | $svrAddr = $this->getServerAddr(); |
179 | 203 | // command eg: "php -S 127.0.0.1:8080 -t web web/index.php"; |
180 | 204 | $command = "$phpBin -S {$svrAddr}"; |
181 | 205 |
|
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}"; |
184 | 212 | } |
185 | 213 |
|
186 | 214 | 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 | + } |
189 | 218 |
|
190 | | - if ($fn = $this->beforeStart) { |
191 | | - $fn($this); |
192 | | - } else { |
193 | | - $this->printDefaultMessage(); |
| 219 | + $command .= " $entryFile"; |
194 | 220 | } |
195 | 221 |
|
196 | | - Cli::write("<cyan>></cyan> <darkGray>$command</darkGray>"); |
197 | | - Sys::execute($command); |
| 222 | + return $command; |
198 | 223 | } |
199 | 224 |
|
200 | 225 | /** |
|
0 commit comments