Skip to content

Commit

Permalink
up: support config serve:run by swoftcli.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 30, 2019
1 parent d52ab5b commit 1766838
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 29 deletions.
74 changes: 48 additions & 26 deletions app/Command/ServeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -39,16 +38,6 @@
*/
class ServeCommand
{
/**
* @var int
*/
private $pid = 0;

/**
* @var int
*/
private $retry = 0;

/**
* @var bool
*/
Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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
*
Expand All @@ -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 <cyan>targetPath</cyan>"
* )
* @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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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);
Expand Down

0 comments on commit 1766838

Please sign in to comment.