Skip to content

Commit 478e46d

Browse files
committed
some logic update...
1 parent 66e12bb commit 478e46d

File tree

19 files changed

+377
-236
lines changed

19 files changed

+377
-236
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
!README.md
99
!.gitkeep
1010
composer.lock
11+
*.log
1112
*.swp
1213
*.swo
1314
*.zip
1415
*.phar
1516
.DS_Store
16-
.interactive_history
17+
.interactive_history

examples/Controllers/HomeController.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,17 @@ protected function afterExecute()
5858
* the second line text
5959
* @usage usage message
6060
* @arguments
61-
* arg1 argument description 1
62-
* arg2 argument description 2
61+
* arg1 argument description 1
62+
* the second line
63+
* a2,arg2 argument description 2
64+
* the second line
6365
* @options
6466
* -s, --long option description 1
6567
* --opt option description 2
6668
* @example example text one
6769
* the second line example
6870
*/
69-
public function indexCommand()
71+
public function testCommand()
7072
{
7173
$this->write('hello, welcome!! this is ' . __METHOD__);
7274
}

examples/Controllers/ProcessController.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Inhere\Console\Examples\Controllers;
1010

1111
use Inhere\Console\Controller;
12+
use Inhere\Console\Utils\CliUtil;
1213
use Inhere\Console\Utils\ProcessUtil;
1314

1415
/**
@@ -27,6 +28,8 @@ protected static function commandAliases()
2728
'cpr' => 'childProcess',
2829
'mpr' => 'multiProcess',
2930
'dr' => 'daemonRun',
31+
'rs' => 'runScript',
32+
'rb' => 'runInBackground',
3033
];
3134
}
3235

@@ -35,9 +38,41 @@ protected static function commandAliases()
3538
*/
3639
public function runScriptCommand()
3740
{
38-
$script = '<?php echo "foo"; ?>';
41+
/*$script = '<?php echo "foo"; ?>';*/
42+
$script = '<?php print_r($_SERVER); ?>';
3943

44+
// $tmpDir = CliUtil::getTempDir();
45+
// $tmpFile = $tmpDir . '/' . md5($script) . '.php';
46+
// file_put_contents($tmpFile, $script);
4047

48+
$descriptorSpec = [
49+
0 => ['pipe', 'r'], // 标准输入,子进程从此管道中读取数据
50+
1 => ['pipe', 'w'], // 标准输出,子进程向此管道中写入数据
51+
2 => ['file', $this->app->getRootPath() . '/examples/tmp/error-output.log', 'a'] // 标准错误,写入到一个文件
52+
];
53+
54+
$process = proc_open('php', $descriptorSpec, $pipes);
55+
56+
if (\is_resource($process)) {
57+
// $pipes 现在看起来是这样的:
58+
// 0 => 可以向子进程标准输入写入的句柄
59+
// 1 => 可以从子进程标准输出读取的句柄
60+
// 错误输出将被追加到文件 error-output.txt
61+
62+
fwrite($pipes[0], $script);
63+
fclose($pipes[0]);
64+
65+
$result = stream_get_contents($pipes[1]);
66+
67+
fclose($pipes[1]);
68+
69+
$this->write("RESULT:\n" . $result);
70+
71+
// 切记:在调用 proc_close 之前关闭所有的管道以避免死锁。
72+
$retVal = proc_close($process);
73+
74+
echo "command returned $retVal\n";
75+
}
4176
}
4277

4378
/**
@@ -70,6 +105,19 @@ public function daemonRunCommand()
70105
}
71106
}
72107

108+
/**
109+
* simple process example for run In Background
110+
*/
111+
public function runInBackgroundCommand()
112+
{
113+
$script = '<?php print_r($_SERVER); ?>';
114+
$ret = ProcessUtil::runInBackground("php $script");
115+
116+
if ($ret === false) {
117+
$this->output->liteError('current env is not support process create.');
118+
}
119+
}
120+
73121
/**
74122
* simple process example for multi-process
75123
* @options

examples/liteApp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ define('BASE_PATH', dirname(__DIR__));
66
require dirname(__DIR__) . '/tests/boot.php';
77

88
// create app instance
9-
$app = new \Inhere\Console\LiteApplication;
9+
$app = new \Inhere\Console\LiteApp;
1010

1111
// register commands
1212
$app->addCommand('test', function () {

src/BuiltIn/Resources/templates/auto-complete-scripts.tpl renamed to examples/tmp/.gitkeep

File renamed without changes.

src/Base/AbstractApplication.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ public function showCommandList($quit = true)
448448
ksort($internalCommands);
449449

450450
// built in options
451-
$internalOptions = FormatUtil::arrayOptions(self::$internalOptions);
451+
$internalOptions = FormatUtil::alignmentOptions(self::$internalOptions);
452452

453453
$this->output->mList([
454454
'Usage:' => "$script <info>{command}</info> [arg0 arg1=value1 arg2=value2 ...] [--opt -v -h ...]",
@@ -617,7 +617,7 @@ public function getLogoText()
617617
public function setLogo(string $logoTxt, string $style = null)
618618
{
619619
$this->meta['logoText'] = $logoTxt;
620-
620+
621621
if ($style) {
622622
$this->meta['logoStyle'] = $style;
623623
}
@@ -639,6 +639,14 @@ public function setLogoStyle(string $style)
639639
$this->meta['logoStyle'] = $style;
640640
}
641641

642+
/**
643+
* @return string
644+
*/
645+
public function getRootPath()
646+
{
647+
return $this->getMeta('rootPath');
648+
}
649+
642650
/**
643651
* @return array
644652
*/

src/Base/AbstractCommand.php

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -365,13 +365,13 @@ protected function parseAnnotationVars($str)
365365
}
366366

367367
/**
368-
* show help by parse method annotation
368+
* show help by parse method annotations
369369
* @param string $method
370370
* @param null|string $action
371371
* @param array $aliases
372372
* @return int
373373
*/
374-
protected function showHelpByMethodAnnotation($method, $action = null, array $aliases = [])
374+
protected function showHelpByMethodAnnotations($method, $action = null, array $aliases = [])
375375
{
376376
$ref = new \ReflectionClass($this);
377377
$name = $this->input->getCommand();
@@ -391,36 +391,23 @@ protected function showHelpByMethodAnnotation($method, $action = null, array $al
391391

392392
$doc = $ref->getMethod($method)->getDocComment();
393393
$tags = Annotation::getTags($this->parseAnnotationVars($doc));
394-
$comments = [];
394+
$help = [];
395395

396396
if ($aliases) {
397-
$comments[] = sprintf("<comment>Alias Name:</comment> %s\n", implode(',', $aliases));
397+
$help[] = sprintf("<comment>Alias Name:</comment> %s\n", implode(',', $aliases));
398398
}
399399

400-
foreach ($tags as $tag => $msg) {
401-
if (!$msg || !\is_string($msg)) {
400+
foreach (array_keys(self::$annotationTags) as $tag) {
401+
if (empty($tags[$tag]) || !\is_string($tags[$tag])) {
402402
continue;
403403
}
404404

405-
if (isset(self::$annotationTags[$tag])) {
406-
$msg = $this->parseAnnotationVars(trim($msg));
407-
408-
// need multi align
409-
// if (self::$annotationTags[$tag]) {
410-
// $lines = array_map(function ($line) {
411-
// // return trim($line);
412-
// return $line;
413-
// }, explode("\n", $msg));
414-
415-
// $msg = implode("\n", array_filter($lines, 'trim'));
416-
// }
417-
418-
$tag = ucfirst($tag);
419-
$comments[] = "<comment>$tag:</comment>\n $msg\n";
420-
}
405+
$msg = trim($tags[$tag]);
406+
$tag = ucfirst($tag);
407+
$help[] = "<comment>$tag:</comment>\n $msg\n";
421408
}
422409

423-
$this->output->write(implode("\n", $comments), false);
410+
$this->output->write(implode("\n", $help), false);
424411

425412
return 0;
426413
}
@@ -469,6 +456,16 @@ public static function getAnnotationTags(): array
469456
return self::$annotationTags;
470457
}
471458

459+
/**
460+
* @param string $name
461+
*/
462+
public static function addAnnotationTag(string $name)
463+
{
464+
if (!isset(self::$annotationTags[$name])) {
465+
self::$annotationTags[$name] = true;
466+
}
467+
}
468+
472469
/**
473470
* @param array $annotationTags
474471
* @param bool $replace
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env bash
2+
3+
_auto_completion()
4+
{
5+
local cur prev
6+
_get_comp_words_by_ref -n = cur prev
7+
8+
commands="commands list enumerate controller create-controller model \
9+
create-model all-models create-all-models project create-project scaffold \
10+
create-scaffold migration create-migration webtools create-webtools"
11+
12+
case "$prev" in
13+
project|create-project)
14+
COMPREPLY=($(compgen -W "--name --webtools --directory --type --template-path --use-config-ini --trace --help --namespace" -- "$cur"))
15+
return 0
16+
;;
17+
esac
18+
19+
COMPREPLY=($(compgen -W "$commands" -- "$cur"))
20+
21+
} &&
22+
complete -F _auto_completion app
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
3+
#
4+
# Auto generate by inhere/console.
5+
# console application name: {$name}
6+
# @date {$datetime}
7+
#
8+
9+
_{$name}_auto_completion()
10+
{
11+
local cur prev
12+
_get_comp_words_by_ref -n = cur prev
13+
14+
commands="{$commands}"
15+
16+
case "$prev" in
17+
project|create-project)
18+
COMPREPLY=($(compgen -W "--name --webtools --directory --type --template-path --use-config-ini --trace --help --namespace" -- "$cur"))
19+
return 0
20+
;;
21+
esac
22+
23+
COMPREPLY=($(compgen -W "$commands" -- "$cur"))
24+
25+
} &&
26+
complete -F _{$name}_auto_completion {$name}

src/Command.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,6 @@ protected function showHelp()
5858
return 0;
5959
}
6060

61-
return $this->showHelpByMethodAnnotation('execute');
61+
return $this->showHelpByMethodAnnotations('execute');
6262
}
6363
}

0 commit comments

Comments
 (0)