1111use Swoft \Console \Annotation \Mapping \CommandOption ;
1212use Swoft \Console \Helper \Show ;
1313use Swoft \Console \Input \Input ;
14- use Swoft \Console \Output \Output ;
1514use Swoft \Stdlib \Helper \Sys ;
1615use Swoole \Process ;
1716use function array_filter ;
3938 */
4039class ServeCommand
4140{
42- /**
43- * @var int
44- */
45- private $ pid = 0 ;
46-
47- /**
48- * @var int
49- */
50- private $ retry = 0 ;
51-
5241 /**
5342 * @var bool
5443 */
@@ -96,29 +85,39 @@ class ServeCommand
9685 */
9786 private function collectInfo (Input $ input ): bool
9887 {
88+ $ config = [];
9989 $ workDir = $ input ->getPwd ();
10090
101- $ this ->debug = $ input ->getBoolOpt ('debug ' );
102- $ this ->phpBin = $ input ->getOpt ('php-bin ' );
103-
104- if ($ this ->phpBin === 'php ' ) {
105- [$ ok , $ ret ,] = Sys::run ('which php ' );
106-
107- if ($ ok === 0 ) {
108- $ this ->phpBin = trim ($ ret );
109- }
91+ $ appParam = bean ('cliApp ' )->get ('commands ' );
92+ if (isset ($ appParam ['serve:run ' ])) {
93+ $ config = $ appParam ['serve:run ' ];
11094 }
11195
96+ $ this ->debug = $ input ->getBoolOpt ('debug ' );
97+ $ this ->phpBin = $ this ->findPhpBinFile ($ config ['php-bin ' ] ?? '' , $ input );
98+
11299 $ interval = (int )$ input ->getOpt ('interval ' , 3 );
113100 if ($ interval < 0 || $ interval > 15 ) {
114101 $ interval = 3 ;
115102 }
116103
117104 $ this ->interval = $ interval ;
118- $ this ->binFile = $ input ->getSameOpt (['bin-file ' , 'b ' ]);
119- $ this ->startCmd = $ input ->getSameOpt (['start-cmd ' , 'c ' ]);
120105
121- if ($ nameString = $ input ->getSameOpt (['watch-dir ' , 'w ' ])) {
106+ if (!empty ($ config ['bin-file ' ])) {
107+ $ this ->binFile = $ config ['bin-file ' ];
108+ } else {
109+ $ this ->binFile = $ input ->getSameOpt (['bin-file ' , 'b ' ]);
110+ }
111+
112+ if (!empty ($ config ['start-cmd ' ])) {
113+ $ this ->startCmd = $ config ['start-cmd ' ];
114+ } else {
115+ $ this ->startCmd = $ input ->getSameOpt (['start-cmd ' , 'c ' ]);
116+ }
117+
118+ if (!empty ($ config ['watch-dir ' ])) {
119+ $ this ->watchDir = explode (', ' , $ config ['watch-dir ' ]);
120+ } elseif ($ nameString = $ input ->getSameOpt (['watch-dir ' , 'w ' ])) {
122121 $ this ->watchDir = explode (', ' , str_replace (' ' , '' , $ nameString ));
123122 }
124123
@@ -167,6 +166,30 @@ private function collectInfo(Input $input): bool
167166 return true ;
168167 }
169168
169+ /**
170+ * @param string $phpBin
171+ * @param Input $input
172+ *
173+ * @return string
174+ */
175+ private function findPhpBinFile (string $ phpBin , Input $ input ): string
176+ {
177+ if (!$ phpBin ) {
178+ $ phpBin = $ input ->getStringOpt ('php-bin ' );
179+ }
180+
181+ if ($ phpBin === 'php ' ) {
182+ // TODO use `type php` check and find
183+ [$ ok , $ ret ,] = Sys::run ('which php ' );
184+
185+ if ($ ok === 0 ) {
186+ $ phpBin = trim ($ ret );
187+ }
188+ }
189+
190+ return $ phpBin ;
191+ }
192+
170193 /**
171194 * Start the swoft server and monitor the file changes to restart the server
172195 *
@@ -189,14 +212,13 @@ private function collectInfo(Input $input): bool
189212 * "watch", short="w", default="app,config", type="directories",
190213 * desc="List of directories you want to watch, relative the <cyan>targetPath</cyan>"
191214 * )
192- * @param Input $input
193- * @param Output $output
215+ * @param Input $input
194216 *
195217 * @example
196218 * {binFile} run Default, will start http server
197219 * {binFile} run -c ws:start -b bin/swoft /path/to/swoft
198220 */
199- public function run (Input $ input, Output $ output ): void
221+ public function run (Input $ input ): void
200222 {
201223 if (!$ this ->collectInfo ($ input )) {
202224 return ;
0 commit comments