88
99namespace Inhere \Console \BuiltIn ;
1010
11+ use Exception ;
1112use Inhere \Console \Command ;
1213use Inhere \Console \IO \Input ;
1314use Inhere \Console \IO \Output ;
14- use Toolkit \ Sys \ Sys ;
15+ use Inhere \ Console \ Util \ PhpDevServe ;
1516use function strpos ;
16- use const PHP_VERSION ;
1717
1818/**
1919 * Class DevServerCommand
@@ -39,56 +39,49 @@ public static function aliases(): array
3939 * {command} [-H HOST] [-p PORT]
4040 * {command} [-S HOST:PORT] [file=]web/index.php
4141 * @options
42- * -S STRING The http server address. e.g 127.0.0.1:8552
43- * -t STRING The document root dir for server(<comment>web </comment>)
42+ * -s, -S, --addr STRING The http server address. e.g 127.0.0.1:8552
43+ * -t, --doc-root STRING The document root dir for server(<comment>public </comment>)
4444 * -H,--host STRING The server host address(<comment>127.0.0.1</comment>)
4545 * -p,--port INTEGER The server port number(<comment>8552</comment>)
4646 * -b,--php-bin STRING The php binary file(<comment>php</comment>)
4747 * @arguments
4848 * file=STRING The entry file for server. e.g web/index.php
4949 *
50- * @param Input $in
51- * @param Output $out
50+ * @param Input $input
51+ * @param Output $output
5252 *
5353 * @return int|mixed|void
54+ * @throws Exception
5455 * @example
5556 * {command} -S 127.0.0.1:8552 web/index.php
5657 */
57- public function execute ($ in , $ out )
58+ public function execute ($ input , $ output )
5859 {
59- if (!$ server = $ this ->getOpt ('S ' )) {
60- $ server = $ this ->getSameOpt (['H ' , 'host ' ], '127.0.0.1 ' );
60+ $ serveAddr = $ input ->getSameStringOpt ('s,S,addr ' );
61+ if (!$ serveAddr ) {
62+ $ serveAddr = $ this ->getSameOpt (['H ' , 'host ' ], '127.0.0.1 ' );
6163 }
6264
63- if (! strpos ( $ server , ': ' )) {
64- $ port = $ this -> getSameOpt ([ ' p ' , 'port ' ], 8552 );
65- $ server .= ': ' . $ port ;
65+ $ port = $ input -> getSameStringOpt ([ ' p ' , 'port ' ]);
66+ if ( $ port && strpos ( $ serveAddr , ': ' ) === false ) {
67+ $ serveAddr .= ': ' . $ port ;
6668 }
6769
68- $ version = PHP_VERSION ;
69- $ workDir = $ this ->input ->getPwd ();
70- $ docDir = $ this ->getOpt ('t ' );
71- $ docRoot = $ docDir ? $ workDir . '/ ' . $ docDir : $ workDir ;
70+ $ hceFile = $ input ->getStringOpt ('hce-file ' );
71+ $ hceEnv = $ input ->getStringOpt ('hce-env ' );
72+ $ docRoot = $ input ->getSameStringOpt ('t,doc-root ' );
7273
73- $ this ->write ([
74- "PHP $ version Development Server started \nServer listening on http://<info> $ server</info> " ,
75- "Document root is <comment> $ docRoot</comment> " ,
76- 'You can use <comment>CTRL + C</comment> to stop run. ' ,
77- ]);
74+ $ input ->bindArgument ('file ' , 0 );
75+ $ entryFile = $ input ->getStringArg ('file ' );
7876
79- // $command = "php -S {$server} -t web web/index.php" ;
80- $ command = " php -S { $ server }" ;
77+ $ pds = PhpDevServe:: new ( $ serveAddr , $ docRoot ) ;
78+ $ pds -> setEntryFile ( $ entryFile ) ;
8179
82- if ($ docDir ) {
83- $ command .= " -t $ docDir " ;
80+ if ($ hceEnv && $ hceFile ) {
81+ $ pds ->loadHceFile ($ hceFile );
82+ $ pds ->useHceEnv ($ hceEnv );
8483 }
8584
86- if ($ entryFile = $ this ->getSameArg (['file ' , 0 ])) {
87- $ command .= " $ entryFile " ;
88- }
89-
90- $ this ->write ("<cyan>></cyan> <darkGray> $ command</darkGray> " );
91-
92- Sys::execute ($ command );
85+ $ pds ->listen ();
9386 }
9487}
0 commit comments