Skip to content

Commit b2ee9dd

Browse files
committed
up: use new flags replace the input object
1 parent fb34f32 commit b2ee9dd

File tree

3 files changed

+53
-31
lines changed

3 files changed

+53
-31
lines changed

src/BuiltIn/DevServerCommand.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace Inhere\Console\BuiltIn;
1010

1111
use Exception;
12-
use Inhere\Console\Attr\CmdOption;
12+
use Inhere\Console\Annotate\Attr\CmdOption;
1313
use Inhere\Console\Command;
1414
use Inhere\Console\IO\Input;
1515
use Inhere\Console\IO\Output;
@@ -45,8 +45,9 @@ public static function aliases(): array
4545
* -H,--host The server host address(<comment>127.0.0.1</comment>)
4646
* -p,--port The server port number(<comment>8552</comment>)
4747
* -b,--php-bin The php binary file(<comment>php</comment>)
48+
*
4849
* @arguments
49-
* file=STRING The entry file for server. e.g web/index.php
50+
* file The entry file for server. e.g web/index.php
5051
*
5152
* @param Input $input
5253
* @param Output $output
@@ -59,23 +60,22 @@ public static function aliases(): array
5960
#[CmdOption('dev-serve', 'start a php built-in http server for developmentd')]
6061
public function execute(Input $input, Output $output)
6162
{
62-
$serveAddr = $input->getSameStringOpt('s,S,addr');
63+
$serveAddr = $this->flags->getOpt('addr');
6364
if (!$serveAddr) {
64-
$serveAddr = $input->getSameStringOpt(['H', 'host']);
65+
$serveAddr = $this->flags->getOpt('host');
6566
}
6667

67-
$port = $input->getSameStringOpt(['p', 'port']);
68+
$port = $this->flags->getOpt('port');
6869
if ($port && strpos($serveAddr, ':') === false) {
6970
$serveAddr .= ':' . $port;
7071
}
7172

72-
$docRoot = $input->getSameStringOpt('t,doc-root');
73-
$hceFile = $input->getStringOpt('hce-file');
74-
$hceEnv = $input->getStringOpt('hce-env');
75-
$phpBin = $input->getStringOpt('php-bin');
73+
$docRoot = $this->flags->getOpt('doc-root');
74+
$hceFile = $this->flags->getOpt('hce-file');
75+
$hceEnv = $this->flags->getOpt('hce-env');
76+
$phpBin = $this->flags->getOpt('php-bin');
7677

77-
$input->bindArgument('file', 0);
78-
$entryFile = $input->getStringArg('file');
78+
$entryFile = $this->flags->getArg('file');
7979

8080
$pds = PhpDevServe::new($serveAddr, $docRoot, $entryFile);
8181
$pds->setPhpBin($phpBin);

src/BuiltIn/PharController.php

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Inhere\Console\Util\Helper;
1818
use Inhere\Console\Util\Show;
1919
use RuntimeException;
20+
use Toolkit\PFlag\FlagsParser;
2021
use Toolkit\Stdlib\Str;
2122
use function basename;
2223
use function file_exists;
@@ -77,8 +78,9 @@ protected function packConfigure(Input $input): void
7778
* --files Only pack the list files to the exist phar, multi use ',' split
7879
* --no-progress bool;Disable output progress on the runtime
7980
*
80-
* @param Input $input
81+
* @param Input $input
8182
* @param Output $output
83+
* @param FlagsParser $fs
8284
*
8385
* @return int
8486
* @throws Exception
@@ -92,16 +94,16 @@ protected function packConfigure(Input $input): void
9294
* only update the input files:
9395
* php -d phar.readonly=0 {binFile} phar:pack -o=mycli.phar --debug --files app/Command/ServeCommand.php
9496
*/
95-
public function packCommand(Input $input, Output $output): int
97+
public function packCommand(Input $input, Output $output, FlagsParser $fs): int
9698
{
9799
$startAt = microtime(true);
98100
$workDir = $input->getPwd();
99101

100-
$dir = $input->getOpt('dir') ?: $workDir;
102+
$dir = $fs->getOpt('dir') ?: $workDir;
101103
$cpr = $this->configCompiler($dir);
102104

103-
$refresh = $input->boolOpt('refresh');
104-
$outFile = $input->getSameStringOpt(['o', 'output'], $this->defPkgName);
105+
$refresh = $fs->getOpt('refresh');
106+
$outFile = $fs->getOpt('output', $this->defPkgName);
105107
$pharFile = $workDir . '/' . $outFile;
106108

107109
Show::aList([
@@ -111,13 +113,13 @@ public function packCommand(Input $input, Output $output): int
111113
], 'Building Information');
112114

113115
// use fast build
114-
if ($this->input->boolOpt('fast')) {
116+
if ($fs->getOpt('fast')) {
115117
$cpr->setModifies($cpr->findChangedByGit());
116118
$this->output->liteNote('Use fast build, will only pack changed or new files(from git status)');
117119
}
118120

119121
// Manual append some files
120-
if ($files = $input->getStringOpt('files')) {
122+
if ($files = $fs->getOpt('files')) {
121123
$cpr->setModifies(Str::explode($files));
122124
$output->liteInfo("will only pack input files to the exists phar: $outFile");
123125
}
@@ -130,7 +132,10 @@ public function packCommand(Input $input, Output $output): int
130132
});
131133

132134
$output->colored('Collect Pack files', 'comment');
133-
$this->outputProgress($cpr, $input);
135+
136+
if (!$fs->getOpt('no-progress')) {
137+
$this->outputProgress($cpr);
138+
}
134139

135140
// packing ...
136141
$cpr->pack($pharFile, $refresh);
@@ -178,17 +183,12 @@ protected function configCompiler(string $dir): PharCompiler
178183

179184
/**
180185
* @param PharCompiler $cpr
181-
* @param Input $input
182186
*
183187
* @return void
184188
*/
185-
private function outputProgress(PharCompiler $cpr,Input $input): void
189+
private function outputProgress(PharCompiler $cpr): void
186190
{
187-
if ($input->getBoolOpt('no-progress')) {
188-
return;
189-
}
190-
191-
if ($input->getOpt('debug')) {
191+
if ($this->isDebug()) {
192192
// $output->info('Pack file to Phar ... ...');
193193
$cpr->onAdd(function (string $path) {
194194
$this->writeln(" <info>+</info> $path");
@@ -228,15 +228,16 @@ public function setCompilerConfiger(Closure $compilerConfiger): void
228228
* -y, --yes bool;Whether display goon tips message.
229229
* --overwrite bool;Whether overwrite exists files on extract phar
230230
*
231-
* @param Input $in
231+
* @param Input $in
232232
* @param Output $out
233+
* @param FlagsParser $fs
233234
*
234235
* @return int
235236
* @example {fullCommand} -f myapp.phar -d var/www/app
236237
*/
237-
public function unpackCommand($in, $out): int
238+
public function unpackCommand(Input $in, Output $out, FlagsParser $fs): int
238239
{
239-
if (!$path = $in->getSameOpt(['f', 'file'])) {
240+
if (!$path = $fs->getOpt('file')) {
240241
return $out->error("Please input the phar file path by option '-f|--file'");
241242
}
242243

@@ -247,8 +248,8 @@ public function unpackCommand($in, $out): int
247248
return $out->error("The phar file not exists. File: $file");
248249
}
249250

250-
$dir = $in->getSameOpt(['d', 'dir']) ?: $basePath;
251-
$overwrite = $in->getBoolOpt('overwrite');
251+
$dir = $fs->getOpt('dir') ?: $basePath;
252+
$overwrite = $fs->getOpt('overwrite');
252253

253254
if (!is_dir($dir)) {
254255
Helper::mkdir($dir);

src/Concern/AttachApplicationTrait.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Inhere\Console\Application;
77
use Inhere\Console\Console;
88
use Inhere\Console\GlobalOption;
9+
use Toolkit\Stdlib\OS;
910

1011
/**
1112
* Trait AttachApplicationTrait
@@ -101,6 +102,26 @@ public function getVerbLevel(): int
101102
return (int)$this->input->getLongOpt('debug', Console::VERB_ERROR);
102103
}
103104

105+
/**
106+
* @param int $level
107+
*
108+
* @return bool
109+
*/
110+
public function isDebug(int $level = Console::VERB_DEBUG): bool
111+
{
112+
if ($this->app) {
113+
return $this->app->isDebug();
114+
}
115+
116+
$setVal = Console::VERB_ERROR;
117+
$envVal = OS::getEnvStrVal(Console::DEBUG_ENV_KEY);
118+
if ($envVal !== '') {
119+
$setVal = (int)$envVal;
120+
}
121+
122+
return $level <= $setVal;
123+
}
124+
104125
/**
105126
* @param string $format
106127
* @param mixed ...$args

0 commit comments

Comments
 (0)