Skip to content

Commit 1766838

Browse files
committed
up: support config serve:run by swoftcli.yml
1 parent d52ab5b commit 1766838

2 files changed

Lines changed: 54 additions & 29 deletions

File tree

app/Command/ServeCommand.php

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Swoft\Console\Annotation\Mapping\CommandOption;
1212
use Swoft\Console\Helper\Show;
1313
use Swoft\Console\Input\Input;
14-
use Swoft\Console\Output\Output;
1514
use Swoft\Stdlib\Helper\Sys;
1615
use Swoole\Process;
1716
use function array_filter;
@@ -39,16 +38,6 @@
3938
*/
4039
class 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;
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace Swoft\Cli\Listener;
1212

13+
use Swoft\Cli\Helper\CliHelper;
1314
use Swoft\Console\Application;
1415
use Swoft\Console\ConsoleEvent;
1516
use Swoft\Event\Annotation\Mapping\Listener;
@@ -18,11 +19,11 @@
1819
use Symfony\Component\Yaml\Yaml;
1920

2021
/**
21-
* Class BeforeConsoleRunListener - event handler
22+
* Class BeforeDispatchListener - event handler
2223
*
23-
* @Listener(ConsoleEvent::RUN_BEFORE)
24+
* @Listener(ConsoleEvent::DISPATCH_BEFORE)
2425
*/
25-
class BeforeConsoleRunListener implements EventHandlerInterface
26+
class BeforeDispatchListener implements EventHandlerInterface
2627
{
2728
/**
2829
* @param EventInterface $event
@@ -32,6 +33,8 @@ public function handle(EventInterface $event): void
3233
$configFile = getcwd() . '/swoftcli.yml';
3334

3435
if (file_exists($configFile)) {
36+
CliHelper::info('find config file(swoftcli.yml) in work directory, will load it');
37+
3538
/** @var Application $app */
3639
$app = $event->getTarget();
3740
$data = Yaml::parseFile($configFile);

0 commit comments

Comments
 (0)