Skip to content

Commit 6127703

Browse files
committed
update readme. complete a simple template generator
1 parent 53e4950 commit 6127703

File tree

8 files changed

+227
-19
lines changed

8 files changed

+227
-19
lines changed

README.md

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ $app->command('demo', function (Input $in, Output $out) {
114114
}, 'this is message for the command');
115115
```
116116

117-
### 继承 `Inhere\Console\Command`
117+
### 独立命令
118118

119119
通过继承 `Inhere\Console\Command` 添加独立命令
120120

@@ -165,7 +165,7 @@ $app->command(TestCommand::class);
165165
// $app->command('test1', TestCommand::class);
166166
```
167167

168-
### 继承 `Inhere\Console\Controller`
168+
### 命令组
169169

170170
通过继承 `Inhere\Console\Controller` 添加一组命令. 即是命令行的控制器
171171

@@ -181,13 +181,29 @@ class HomeController extends Controller
181181
protected static $description = 'default command controller. there are some command usage examples';
182182

183183
/**
184-
* this is a command's description message <info>a color text</info>
184+
* this is a command's description message, <cyan>color text</cyan>
185185
* the second line text
186-
* @usage usage message
186+
* @usage {command} [arg ...] [--opt ...]
187+
* @arguments
188+
* arg1 argument description 1
189+
* the second line
190+
* a2,arg2 argument description 2
191+
* the second line
192+
* @options
193+
* -s, --long option description 1
194+
* --opt option description 2
187195
* @example example text one
188196
* the second line example
189197
*/
190-
public function indexCommand()
198+
public function testCommand()
199+
{
200+
$this->write('hello, welcome!! this is ' . __METHOD__);
201+
}
202+
203+
/**
204+
* a example for use color text output on command
205+
*/
206+
public function otherCommand()
191207
{
192208
$this->write('hello, welcome!! this is ' . __METHOD__);
193209
}
@@ -202,13 +218,14 @@ class HomeController extends Controller
202218

203219
- 支持的tag有 `@usage` `@arguments` `@options` `@example`
204220
- 当你使用 `php examples/app home -h` 时,可以查看到 `HomeController` 的所有命令描述注释信息
205-
- 当使用 `php examples/app home:index -h` 时,可以查看到关于 `HomeController::indexCommand` 更详细的信息。包括描述注释文本、`@usage``@example`
221+
222+
![group-command-list](docs/screenshots/group-command-list.png)
223+
- 当使用 `php examples/app home:test -h` 时,可以查看到关于 `HomeController::testCommand` 更详细的信息。包括描述注释文本、`@usage``@example`
206224

207-
> 小提示:注释里面同样支持带颜色的文本输出 `eg: this is a command's description <info>message</info>`
225+
![group-command-list](docs/screenshots/group-command-help.png)
208226

209-
- 运行效果(by `php examples/app home`):
227+
> 小提示:注释里面同样支持带颜色的文本输出 `eg: this is a command's description <info>message</info>`
210228
211-
![group-command-list](docs/screenshots/group-command-list.png)
212229

213230
更多请查看 [examples](./examples) 中的示例代码和在目录下运行示例 `php examples/app` 来查看效果
214231

352 KB
Loading

examples/Controllers/HomeController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ protected function afterExecute()
5858
/**
5959
* this is a command's description message
6060
* the second line text
61-
* @usage usage message
61+
* @usage {command} [arg ...] [--opt ...]
6262
* @arguments
6363
* arg1 argument description 1
6464
* the second line

src/Base/AbstractCommand.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,14 @@ public function __construct(Input $input, Output $output, InputDefinition $defin
7676
{
7777
$this->input = $input;
7878
$this->output = $output;
79-
$this->annotationVars = $this->annotationVars();
8079

8180
if ($definition) {
8281
$this->definition = $definition;
8382
}
8483

8584
$this->init();
85+
86+
$this->annotationVars = $this->annotationVars();
8687
}
8788

8889
protected function init()
@@ -116,12 +117,13 @@ protected function createDefinition()
116117
*/
117118
public function annotationVars()
118119
{
119-
// e.g: `more info see {name}/index`
120+
// e.g: `more info see {name}:index`
120121
return [
121-
'script' => $this->input->getScript(),
122-
'command' => $this->input->getCommand(),
123-
'fullCommand' => $this->input->getScript() . ' ' . $this->input->getCommand(),
124122
'name' => self::getName(),
123+
'group' => self::getName(),
124+
'script' => $this->input->getScript(), // bin/app
125+
'command' => $this->input->getCommand(), // demo OR home:test
126+
'fullCommand' => $this->input->getScript() . ' ' . $this->input->getCommand(),
125127
];
126128
}
127129

src/BuiltIn/GenController.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Inhere
5+
* Date: 2017/12/24 0024
6+
* Time: 16:48
7+
*/
8+
9+
namespace Inhere\Console\BuiltIn;
10+
11+
use Inhere\Console\Controller;
12+
13+
/**
14+
* Class GenController
15+
* @package Inhere\Console\BuiltIn
16+
*/
17+
class GenController extends Controller
18+
{
19+
protected static $name = 'gen';
20+
protected static $description = 'generate code template file tool.';
21+
22+
/**
23+
* generate a alone console command class
24+
*/
25+
public function aloneCommand()
26+
{
27+
28+
}
29+
30+
/**
31+
* generate a group commands(console controller) class
32+
*/
33+
public function groupCommand()
34+
{
35+
36+
}
37+
}

src/Components/AutoComplete/ScriptGenerator.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
namespace Inhere\Console\Components\AutoComplete;
1010

11+
use Inhere\Console\Components\TextTemplate;
12+
1113
/**
1214
* Class ScriptGenerator
1315
* - ref linux system '/etc/bash_completion.d/'
@@ -30,6 +32,9 @@ class ScriptGenerator
3032
/** @var int The mode */
3133
private $mode = 1;
3234

35+
/** @var TextTemplate */
36+
private $renderer;
37+
3338
/**
3439
* @return array
3540
*/
@@ -46,6 +51,28 @@ public static function modeList()
4651
return [self::MODE_SIMPLE, self::MODE_FULL];
4752
}
4853

54+
/**
55+
* @param string $tplFile
56+
* @param string $dstFile
57+
* @param array $vars
58+
* @param bool $override
59+
* @return bool
60+
*/
61+
public function gen(string $tplFile, string $dstFile, array $vars = [], $override = false)
62+
{
63+
if (!\is_file($tplFile)) {
64+
throw new \InvalidArgumentException("Template file not exists. FILE: $tplFile");
65+
}
66+
67+
if (!$override && is_file($dstFile)) {
68+
return true;
69+
}
70+
71+
$tt = $this->getRenderer();
72+
73+
return $tt->render(file_get_contents($tplFile), $vars, $dstFile);
74+
}
75+
4976
/**
5077
* @return int
5178
*/
@@ -81,4 +108,24 @@ public function setMode(int $mode)
81108
$this->mode = $mode;
82109
}
83110
}
111+
112+
/**
113+
* @return TextTemplate
114+
*/
115+
public function getRenderer(): TextTemplate
116+
{
117+
if (!$this->renderer) {
118+
$this->renderer = new TextTemplate();
119+
}
120+
121+
return $this->renderer;
122+
}
123+
124+
/**
125+
* @param TextTemplate $renderer
126+
*/
127+
public function setRenderer(TextTemplate $renderer)
128+
{
129+
$this->renderer = $renderer;
130+
}
84131
}

src/Components/TextTemplate.php

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,39 @@ class TextTemplate
2323
/** @var string */
2424
private $closeChar = '}';
2525

26+
/**
27+
* TextTemplate constructor.
28+
* @param array $vars
29+
*/
30+
public function __construct(array $vars = [])
31+
{
32+
if ($vars) {
33+
$this->setVars($vars);
34+
}
35+
}
36+
37+
/**
38+
* @param string $tplFile
39+
* @param array $vars
40+
* @param null|string $saveAs
41+
* @return string|bool
42+
*/
43+
public function renderFile(string $tplFile, array $vars = [], $saveAs = null)
44+
{
45+
if (!\is_file($tplFile)) {
46+
throw new \InvalidArgumentException("Template file not exists. FILE: $tplFile");
47+
}
48+
49+
return $this->render(file_get_contents($tplFile), $vars, $saveAs);
50+
}
51+
2652
/**
2753
* @param string $template
2854
* @param array $vars
55+
* @param null|string $saveAs
2956
* @return string
3057
*/
31-
public function render(string $template, array $vars = [])
58+
public function render(string $template, array $vars = [], $saveAs = null)
3259
{
3360
if (!$template || false === strpos($template, $this->openChar)) {
3461
return $template;
@@ -38,14 +65,47 @@ public function render(string $template, array $vars = [])
3865
$vars = array_merge($this->vars, $vars);
3966
}
4067

41-
$pairs = [];
68+
$pairs = $map = [];
69+
$this->expandVars($vars, $map);
4270

43-
foreach ($vars as $name => $value) {
71+
foreach ($map as $name => $value) {
4472
$key = $this->openChar . $name . $this->closeChar;
4573
$pairs[$key] = $value;
4674
}
4775

48-
return strtr($template, $pairs);
76+
// replace vars to values.
77+
$rendered = strtr($template, $pairs);
78+
79+
if (!$saveAs) {
80+
return $rendered;
81+
}
82+
83+
$dstDir = \dirname($saveAs);
84+
85+
if (!is_dir($dstDir) && !mkdir($dstDir, 0775, true) && !is_dir($dstDir)) {
86+
throw new \RuntimeException(sprintf('Directory "%s" was not created', $dstDir));
87+
}
88+
89+
return (bool)file_put_contents($saveAs, $rendered);
90+
}
91+
92+
/**
93+
* Multidimensional array expansion to one dimension array
94+
* @param array $vars
95+
* @param null|string $prefix
96+
* @param array $map
97+
*/
98+
protected function expandVars(array $vars, array &$map = [], $prefix = null)
99+
{
100+
foreach ($vars as $name => $value) {
101+
$key = $prefix !== null ? $prefix . '.' . $name : $name;
102+
103+
if (is_scalar($value)) {
104+
$map[$key] = $value;
105+
} elseif (\is_array($value)) {
106+
$this->expandVars($value, $map, (string)$key);
107+
}
108+
}
49109
}
50110

51111
/**
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Inhere
5+
* Date: 2017/12/24 0024
6+
* Time: 15:32
7+
*/
8+
9+
namespace Inhere\Console\Tests\Components;
10+
11+
use Inhere\Console\Components\TextTemplate;
12+
use PHPUnit\Framework\TestCase;
13+
14+
/**
15+
* Class TextTemplateTest
16+
* @package Inhere\Console\Tests\Components
17+
* @covers TextTemplate
18+
*/
19+
class TextTemplateTest extends TestCase
20+
{
21+
public function testRender()
22+
{
23+
$tpl = <<<EOF
24+
test tpl on date {\$date}
25+
26+
use array {\$map.0} {\$map.key1}
27+
EOF;
28+
$date = date('Ymd');
29+
30+
$tt = new TextTemplate([
31+
'name' => 'test',
32+
'date' => $date,
33+
'map' => [
34+
'VAL0',
35+
'key1' => 'VAL1',
36+
],
37+
]);
38+
39+
$ret = $tt->render($tpl);
40+
$this->assertNotEmpty($ret);
41+
$this->assertTrue((bool)strpos($ret, $date));
42+
$this->assertTrue((bool)strpos($ret, 'VAL0'));
43+
$this->assertStringEndsWith('VAL1', $ret);
44+
}
45+
}

0 commit comments

Comments
 (0)