Skip to content

Commit 0e5b928

Browse files
committed
Merge branch 'master' of https://github.com/inhere/php-console
2 parents eba4f39 + 54c945b commit 0e5b928

File tree

7 files changed

+211
-2
lines changed

7 files changed

+211
-2
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
- 消息文本的多种颜色风格输出支持(`info`, `comment`, `success`, `danger`, `error` ... ...)
1515
- 丰富的特殊格式信息显示(`section`, `panel`, `padding`, `help-panel`, `table`, `title`, `list`, `multiList`, `progressBar`)
1616
- 常用的用户信息交互支持(`select`, `multiSelect`, `confirm`, `ask/question`, `askPassword/askHiddenInput`)
17-
- 命令方法注释自动解析(提取为参数 `arguments` 和 选项 `options` 等信息)
17+
- 命令方法注释自动解析为帮助信息(提取为参数 `arguments` 和 选项 `options` 等信息)
1818
- 类似 `symfony/console` 的预定义参数定义支持(按位置赋予参数值)
1919
- 输出是 windows,linux 兼容的,不支持颜色的环境会自动去除相关CODE
2020

examples/demo/exec_compare.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: 2018-01-10
6+
* Time: 19:36
7+
*/
8+
9+
use Inhere\Console\Components\ExecComparator;
10+
use Inhere\Console\Utils\CliUtil;
11+
12+
require dirname(__DIR__, 2) . '/tests/boot.php';
13+
14+
$common = <<<CODE
15+
\$text = 'hello, world!';
16+
CODE;
17+
18+
// preg_match()
19+
$code1 = <<<CODE
20+
preg_match('/wor/', \$text);
21+
CODE;
22+
23+
// strpos()
24+
$code2 = <<<CODE
25+
strpos(\$text, 'wor');
26+
CODE;
27+
// var_dump(CliUtil::getTempDir());die;
28+
$ec = new ExecComparator();
29+
$ec
30+
->setCommon($common)
31+
->setLoops(1000000)
32+
->setSample1($code1)
33+
->setSample2($code2);
34+
35+
$ret = $ec->compare();
36+
37+
print_r($ret);

src/BuiltIn/GenController.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ class GenController extends Controller
1919
protected static $name = 'gen';
2020
protected static $description = 'generate code template file tool.';
2121

22+
protected static function commandAliases()
23+
{
24+
return [
25+
'ac' => 'autoComplete'
26+
];
27+
}
28+
2229
/**
2330
* generate a alone console command class
2431
*/
@@ -34,4 +41,14 @@ public function groupCommand()
3441
{
3542

3643
}
44+
45+
/**
46+
* generate a bash/zsh auto-completion script file for current application.
47+
* @options
48+
* --env linux shell env name. allow: bash,zsh
49+
*/
50+
public function autoCompleteCommand()
51+
{
52+
53+
}
3754
}

src/BuiltIn/Resources/templates/alone-command.tpl

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<?php

src/BuiltIn/Resources/templates/group-commands.tpl

Whitespace-only changes.

src/Components/ExecComparator.php

Lines changed: 155 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,174 @@
88

99
namespace Inhere\Console\Components;
1010

11+
use Inhere\Console\Utils\CliUtil;
12+
1113
/**
1214
* Class ExecComparator - PHP code exec speed comparator
1315
* @package Inhere\Console\Components
1416
*/
1517
class ExecComparator
1618
{
19+
/**
20+
* @var string
21+
*/
22+
public $tmpDir;
23+
1724
/**
1825
* @var array
1926
*/
2027
private $vars = [];
2128

22-
public function compare($code1, $code2, $loops = 10000)
29+
/** @var string[] */
30+
private $sample1;
31+
32+
/** @var string[] */
33+
private $sample2;
34+
35+
/** @var string */
36+
private $common;
37+
38+
/** @var int */
39+
private $loops = 0;
40+
41+
/** @var string */
42+
private $time;
43+
44+
/**
45+
* ExecComparator constructor.
46+
* @param string|null $tmpDir
47+
*/
48+
public function __construct(string $tmpDir = null)
49+
{
50+
$this->tmpDir = $tmpDir ?? CliUtil::getTempDir();
51+
}
52+
53+
/**
54+
* @param string $code
55+
* @return $this
56+
*/
57+
public function setCommon(string $code)
58+
{
59+
$this->common = $code;
60+
61+
return $this;
62+
}
63+
64+
/**
65+
* @param int $times
66+
* @return $this
67+
*/
68+
public function setLoops(int $times)
69+
{
70+
if ($times <= 0) {
71+
throw new \InvalidArgumentException('The time must be gt zero');
72+
}
73+
74+
$this->loops = $times;
75+
76+
return $this;
77+
}
78+
79+
/**
80+
* @param string $code
81+
* @return $this
82+
*/
83+
public function setSample1(string $code)
84+
{
85+
$this->sample1['code'] = $code;
86+
87+
return $this;
88+
}
89+
90+
/**
91+
* @param string $code
92+
* @return $this
93+
*/
94+
public function setSample2(string $code)
95+
{
96+
$this->sample2['code'] = $code;
97+
98+
return $this;
99+
}
100+
101+
/**
102+
* @param array $context
103+
* @param int $loops
104+
* @return array
105+
*/
106+
public function compare(array $context = [], int $loops = 0)
107+
{
108+
if ($loops) {
109+
$this->setLoops($loops);
110+
}
111+
112+
$sTime = microtime(1);
113+
$this->time = date('ymdH');
114+
115+
$id = 1;
116+
$file1 = $this->dump($this->sample1['code'], $id);
117+
$info1 = $this->runSampleFile($file1, $id);
118+
119+
$id = 2;
120+
$file2 = $this->dump($this->sample2['code'], $id);
121+
$info2 = $this->runSampleFile($file2, $id);
122+
$eTime = microtime(1);
123+
124+
return [
125+
$info1,
126+
$info2,
127+
'total' => [
128+
'startTime' => $sTime,
129+
'endTime' => $eTime,
130+
]
131+
];
132+
}
133+
134+
public function runSampleFile(string $file, int $id)
23135
{
136+
$func = 'sample_func_' . $id;
137+
$sMem = memory_get_usage();
138+
$sTime = microtime(1);
139+
140+
// load and running
141+
ob_start();
142+
require $file;
143+
$ret = $func();
144+
$out = ob_get_clean();
145+
146+
$eMem = memory_get_usage();
147+
$eTime = microtime(1);
148+
149+
return [
150+
'startTime' => $sTime,
151+
'endTime' => $eTime,
152+
'startMem' => $sMem,
153+
'endMem' => $eMem,
154+
'output' => $out,
155+
'return' => $ret,
156+
];
157+
}
158+
159+
public function dump(string $code, int $id, array $context = [])
160+
{
161+
$file = $this->tmpDir . '/' . $this->time . '_' . md5($code . random_int(1000, 100000)) . '.php';
162+
$common = $this->common;
163+
164+
$content = <<<CODE
165+
function sample_func_{$id}() {
166+
// prepare
167+
$common
168+
169+
// exec
170+
for (\$i = 0; \$i < $this->loops; \$i++) {
171+
$code
172+
}
173+
}
174+
CODE;
175+
176+
file_put_contents($file, '<?php' . PHP_EOL . $content);
24177

178+
return $file;
25179
}
26180

27181
/**

0 commit comments

Comments
 (0)