Skip to content

Commit f298e66

Browse files
committed
add a built in command for run php dev server
1 parent 1f8f94d commit f298e66

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

src/BuiltIn/DevServerCommand.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Inhere
5+
* Date: 2018/1/27 0027
6+
* Time: 21:54
7+
*/
8+
9+
namespace Inhere\Console\BuiltIn;
10+
11+
use Inhere\Console\Command;
12+
13+
/**
14+
* Class DevServerCommand
15+
* @package Inhere\Console\BuiltIn
16+
*/
17+
class DevServerCommand extends Command
18+
{
19+
protected static $name = 'dev:server';
20+
protected static $description = 'Start a php built-in server for development';
21+
22+
public static function aliases(): array
23+
{
24+
return ['dev-server'];
25+
}
26+
27+
/**
28+
* start a php built-in server for development
29+
* @usage
30+
* {command} [-S HOST:PORT]
31+
* {command} [-H HOST] [-p PORT]
32+
* {command} [-S HOST:PORT] [file=]web/index.php
33+
* @options
34+
* -S STRING The server address. e.g 127.0.0.1:8552
35+
* -t STRING The document root dir for server(<comment>web</comment>)
36+
* -H,--host STRING The server host address(<comment>127.0.0.1</comment>)
37+
* -p,--port INTEGER The server port address(<comment>8552</comment>)
38+
* @arguments
39+
* file=STRING The entry file for server. e.g web/index.php
40+
* @example
41+
* {command} -S 127.0.0.1:8552 web/index.php
42+
*
43+
* @param \Inhere\Console\IO\Input $in
44+
* @param \Inhere\Console\IO\Output $out
45+
*/
46+
public function execute($in, $out)
47+
{
48+
if (!$server = $this->getOpt('S')) {
49+
$server = $this->getSameOpt(['H', 'host'], '127.0.0.1');
50+
}
51+
52+
if (!strpos($server, ':')) {
53+
$port = $this->getSameOpt(['p', 'port'], 8552);
54+
$server .= ':' . $port;
55+
}
56+
57+
$version = PHP_VERSION;
58+
$workDir = $this->input->getPwd();
59+
$docDir = $this->getOpt('t');
60+
$docRoot = $docDir ? $workDir . '/' . $docDir : $workDir;
61+
62+
$this->write([
63+
"PHP $version Development Server started\nServer listening on <info>$server</info>",
64+
"Document root is <comment>$docRoot</comment>",
65+
'You can use <comment>CTRL + C</comment> to stop run.',
66+
]);
67+
68+
// $command = "php -S {$server} -t web web/index.php";
69+
$command = "php -S {$server}";
70+
71+
if ($docDir) {
72+
$command .= " -t $docDir";
73+
}
74+
75+
if ($entryFile = $this->getSameArg(['file', 0])) {
76+
$command .= " $entryFile";
77+
}
78+
79+
$this->write("<cyan>></cyan> <darkGray>$command</darkGray>");
80+
81+
CliUtil::runCommand($command);
82+
}
83+
}

0 commit comments

Comments
 (0)