From 176683876aa09dd15e58729c0c696424bfcde590 Mon Sep 17 00:00:00 2001 From: inhere Date: Sat, 30 Nov 2019 22:51:00 +0800 Subject: [PATCH] up: support config serve:run by swoftcli.yml --- app/Command/ServeCommand.php | 74 ++++++++++++------- ...istener.php => BeforeDispatchListener.php} | 9 ++- 2 files changed, 54 insertions(+), 29 deletions(-) rename app/Listener/{BeforeConsoleRunListener.php => BeforeDispatchListener.php} (75%) diff --git a/app/Command/ServeCommand.php b/app/Command/ServeCommand.php index 608e98e..2de510d 100644 --- a/app/Command/ServeCommand.php +++ b/app/Command/ServeCommand.php @@ -11,7 +11,6 @@ use Swoft\Console\Annotation\Mapping\CommandOption; use Swoft\Console\Helper\Show; use Swoft\Console\Input\Input; -use Swoft\Console\Output\Output; use Swoft\Stdlib\Helper\Sys; use Swoole\Process; use function array_filter; @@ -39,16 +38,6 @@ */ class ServeCommand { - /** - * @var int - */ - private $pid = 0; - - /** - * @var int - */ - private $retry = 0; - /** * @var bool */ @@ -96,29 +85,39 @@ class ServeCommand */ private function collectInfo(Input $input): bool { + $config = []; $workDir = $input->getPwd(); - $this->debug = $input->getBoolOpt('debug'); - $this->phpBin = $input->getOpt('php-bin'); - - if ($this->phpBin === 'php') { - [$ok, $ret,] = Sys::run('which php'); - - if ($ok === 0) { - $this->phpBin = trim($ret); - } + $appParam = bean('cliApp')->get('commands'); + if (isset($appParam['serve:run'])) { + $config = $appParam['serve:run']; } + $this->debug = $input->getBoolOpt('debug'); + $this->phpBin = $this->findPhpBinFile($config['php-bin'] ?? '', $input); + $interval = (int)$input->getOpt('interval', 3); if ($interval < 0 || $interval > 15) { $interval = 3; } $this->interval = $interval; - $this->binFile = $input->getSameOpt(['bin-file', 'b']); - $this->startCmd = $input->getSameOpt(['start-cmd', 'c']); - if ($nameString = $input->getSameOpt(['watch-dir', 'w'])) { + if (!empty($config['bin-file'])) { + $this->binFile = $config['bin-file']; + } else { + $this->binFile = $input->getSameOpt(['bin-file', 'b']); + } + + if (!empty($config['start-cmd'])) { + $this->startCmd = $config['start-cmd']; + } else { + $this->startCmd = $input->getSameOpt(['start-cmd', 'c']); + } + + if (!empty($config['watch-dir'])) { + $this->watchDir = explode(',', $config['watch-dir']); + } elseif ($nameString = $input->getSameOpt(['watch-dir', 'w'])) { $this->watchDir = explode(',', str_replace(' ', '', $nameString)); } @@ -167,6 +166,30 @@ private function collectInfo(Input $input): bool return true; } + /** + * @param string $phpBin + * @param Input $input + * + * @return string + */ + private function findPhpBinFile(string $phpBin, Input $input): string + { + if (!$phpBin) { + $phpBin = $input->getStringOpt('php-bin'); + } + + if ($phpBin === 'php') { + // TODO use `type php` check and find + [$ok, $ret,] = Sys::run('which php'); + + if ($ok === 0) { + $phpBin = trim($ret); + } + } + + return $phpBin; + } + /** * Start the swoft server and monitor the file changes to restart the server * @@ -189,14 +212,13 @@ private function collectInfo(Input $input): bool * "watch", short="w", default="app,config", type="directories", * desc="List of directories you want to watch, relative the targetPath" * ) - * @param Input $input - * @param Output $output + * @param Input $input * * @example * {binFile} run Default, will start http server * {binFile} run -c ws:start -b bin/swoft /path/to/swoft */ - public function run(Input $input, Output $output): void + public function run(Input $input): void { if (!$this->collectInfo($input)) { return; diff --git a/app/Listener/BeforeConsoleRunListener.php b/app/Listener/BeforeDispatchListener.php similarity index 75% rename from app/Listener/BeforeConsoleRunListener.php rename to app/Listener/BeforeDispatchListener.php index dfb78b6..161c2ae 100644 --- a/app/Listener/BeforeConsoleRunListener.php +++ b/app/Listener/BeforeDispatchListener.php @@ -10,6 +10,7 @@ namespace Swoft\Cli\Listener; +use Swoft\Cli\Helper\CliHelper; use Swoft\Console\Application; use Swoft\Console\ConsoleEvent; use Swoft\Event\Annotation\Mapping\Listener; @@ -18,11 +19,11 @@ use Symfony\Component\Yaml\Yaml; /** - * Class BeforeConsoleRunListener - event handler + * Class BeforeDispatchListener - event handler * - * @Listener(ConsoleEvent::RUN_BEFORE) + * @Listener(ConsoleEvent::DISPATCH_BEFORE) */ -class BeforeConsoleRunListener implements EventHandlerInterface +class BeforeDispatchListener implements EventHandlerInterface { /** * @param EventInterface $event @@ -32,6 +33,8 @@ public function handle(EventInterface $event): void $configFile = getcwd() . '/swoftcli.yml'; if (file_exists($configFile)) { + CliHelper::info('find config file(swoftcli.yml) in work directory, will load it'); + /** @var Application $app */ $app = $event->getTarget(); $data = Yaml::parseFile($configFile);