Skip to content

Commit 75646b2

Browse files
committed
update some lgoic
1 parent cd2b998 commit 75646b2

20 files changed

+400
-127
lines changed

.php_cs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,39 @@
11
<?php
22

33
$header = <<<'EOF'
4+
The file is part of inhere/console
45
6+
@author https://github.com/inhere
7+
@homepage https://github.com/inhere/php-console
58
@license https://github.com/inhere/php-console/blob/master/LICENSE
69
EOF;
710

8-
return PhpCsFixer\Config::create()->setRiskyAllowed(true)->setRules([
11+
$rules = [
912
'@PSR2' => true,
10-
// 'header_comment' => [
11-
// 'comment_type' => 'PHPDoc',
12-
// 'header' => $header,
13-
// 'separate' => 'none'
14-
// ],
1513
'array_syntax' => [
16-
'syntax' => 'short'
14+
'syntax' => 'short'
1715
],
18-
'single_quote' => true,
1916
'class_attributes_separation' => true,
17+
'declare_strict_types' => true,
18+
'global_namespace_import' => true,
19+
'header_comment' => [
20+
'comment_type' => 'PHPDoc',
21+
'header' => $header,
22+
'separate' => 'bottom'
23+
],
2024
'no_unused_imports' => true,
25+
'single_quote' => true,
2126
'standardize_not_equals' => true,
22-
'declare_strict_types' => true,
23-
])->setFinder(PhpCsFixer\Finder::create()
27+
];
28+
29+
$finder = PhpCsFixer\Finder::create()
2430
// ->exclude('test')
25-
->exclude('docs')->exclude('vendor')->in(__DIR__))->setUsingCache(false);
31+
->exclude('docs')
32+
->exclude('vendor')
33+
->in(__DIR__);
34+
35+
return PhpCsFixer\Config::create()
36+
->setRiskyAllowed(true)
37+
->setRules($rules)
38+
->setFinder($finder)
39+
->setUsingCache(false);

src/AbstractApplication.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
use Inhere\Console\Component\Style\Style;
1414
use Inhere\Console\Contract\ApplicationInterface;
1515
use Inhere\Console\Contract\ErrorHandlerInterface;
16+
use Inhere\Console\Contract\InputInterface;
1617
use Inhere\Console\IO\Input;
17-
use Inhere\Console\IO\InputInterface;
1818
use Inhere\Console\IO\Output;
19-
use Inhere\Console\IO\OutputInterface;
19+
use Inhere\Console\Contract\OutputInterface;
2020
use Inhere\Console\Traits\ApplicationHelpTrait;
2121
use Inhere\Console\Traits\InputOutputAwareTrait;
2222
use Inhere\Console\Traits\SimpleEventTrait;

src/AbstractHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,6 @@ public function validateInput(): bool
400400
}
401401

402402
$in->setArgs($args);
403-
404403
$this->checkNotExistsOptions($def);
405404

406405
// check options
@@ -413,6 +412,8 @@ public function validateInput(): bool
413412
$shortNames = $conf['shortcut'] ? explode('|', $conf['shortcut']) : [];
414413
if ($srt = $in->findOneShortOpts($shortNames)) {
415414
$opts[$name] = $in->sOpt($srt);
415+
} elseif ($conf['default']) {
416+
416417
} elseif ($conf['required']) {
417418
$missingOpts[] = "--{$name}" . ($srt ? "|-{$srt}" : '');
418419
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Inhere\Console\Contract;
4+
5+
/**
6+
* Interface InputFlagInterface
7+
*
8+
* @package Inhere\Console\Contract
9+
*/
10+
interface InputFlagInterface
11+
{
12+
/**
13+
* @param int $mode
14+
*
15+
* @return bool
16+
*/
17+
public function hasMode(int $mode): bool;
18+
19+
/**
20+
* @return bool
21+
*/
22+
public function isArray(): bool;
23+
24+
/**
25+
* @return bool
26+
*/
27+
public function isRequired(): bool;
28+
29+
/**
30+
* @return bool
31+
*/
32+
public function isOptional(): bool;
33+
}

src/Contract/InputInterface.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* Created by PhpStorm.
4+
* User: Inhere
5+
* Date: 2016/4/23 0023
6+
* Time: 10:22
7+
*/
8+
9+
namespace Inhere\Console\Contract;
10+
11+
/**
12+
* Class Input
13+
*
14+
* @package Inhere\Console\Contract
15+
*/
16+
interface InputInterface
17+
{
18+
// fixed args and opts for a command/controller-command
19+
public const ARG_REQUIRED = 1;
20+
21+
public const ARG_OPTIONAL = 2;
22+
23+
public const ARG_IS_ARRAY = 4;
24+
25+
public const OPT_BOOLEAN = 1; // eq symfony InputOption::VALUE_NONE
26+
27+
public const OPT_REQUIRED = 2;
28+
29+
public const OPT_OPTIONAL = 4;
30+
31+
public const OPT_IS_ARRAY = 8;
32+
33+
/**
34+
* 读取输入信息
35+
*
36+
* @param string $question 若不为空,则先输出文本消息
37+
* @param bool $nl true 会添加换行符 false 原样输出,不添加换行符
38+
*
39+
* @return string
40+
*/
41+
public function read(string $question = '', bool $nl = false): string;
42+
43+
/**
44+
* @return string
45+
*/
46+
public function getScript(): string;
47+
48+
/**
49+
* @return string
50+
*/
51+
public function getCommand(): string;
52+
53+
/**
54+
* @return array
55+
*/
56+
public function getArgs(): array;
57+
58+
/**
59+
* get Argument
60+
*
61+
* @param null|int|string $name
62+
* @param mixed $default
63+
*
64+
* @return mixed
65+
*/
66+
public function getArg($name, $default = null);
67+
68+
/**
69+
* @return array
70+
*/
71+
public function getOpts(): array;
72+
73+
/**
74+
* @param string $name
75+
* @param null $default
76+
*
77+
* @return bool|mixed|null
78+
*/
79+
public function getOpt(string $name, $default = null);
80+
}

src/Contract/OutputInterface.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* Created by PhpStorm.
4+
* User: Inhere
5+
* Date: 2016/4/23 0023
6+
* Time: 10:22
7+
*/
8+
9+
namespace Inhere\Console\Contract;
10+
11+
/**
12+
* Class OutputInterface
13+
*
14+
* @package Inhere\Console\Contract
15+
*/
16+
interface OutputInterface
17+
{
18+
/**
19+
* Write a message to standard output stream.
20+
*
21+
* @param mixed $messages Output message
22+
* @param bool $nl Output with newline
23+
* @param int|boolean $quit If is int, setting it is exit code.
24+
*
25+
* @return int
26+
*/
27+
public function write($messages, $nl = true, $quit = false): int;
28+
}

src/IO/AbstractInput.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*
2222
* @package Inhere\Console\IO
2323
*/
24-
abstract class AbstractInput implements InputInterface
24+
abstract class AbstractInput implements \Inhere\Console\Contract\InputInterface
2525
{
2626
/**
2727
* @var string
@@ -131,6 +131,11 @@ protected function findCommand(): void
131131
$this->args = $newArgs;
132132
}
133133

134+
public function hasMode(): bool
135+
{
136+
137+
}
138+
134139
/***********************************************************************************
135140
* arguments (eg: arg0 name=john city=chengdu)
136141
***********************************************************************************/

src/IO/Input/InputArgument.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88

99
namespace Inhere\Console\IO\Input;
1010

11+
use Inhere\Console\IO\Input;
12+
1113
/**
1214
* Class InputArgument
1315
* - definition a input argument
1416
*
1517
* @package Inhere\Console\IO\Input
1618
*/
17-
class InputArgument extends InputItem
19+
class InputArgument extends InputFlag
1820
{
1921
/**
2022
* The argument position
@@ -23,6 +25,30 @@ class InputArgument extends InputItem
2325
*/
2426
private $index = 0;
2527

28+
/**
29+
* @return bool
30+
*/
31+
public function isArray(): bool
32+
{
33+
return $this->hasMode(Input::ARG_IS_ARRAY);
34+
}
35+
36+
/**
37+
* @return bool
38+
*/
39+
public function isOptional(): bool
40+
{
41+
return $this->hasMode(Input::ARG_OPTIONAL);
42+
}
43+
44+
/**
45+
* @return bool
46+
*/
47+
public function isRequired(): bool
48+
{
49+
return $this->hasMode(Input::ARG_REQUIRED);
50+
}
51+
2652
/**
2753
* @return int
2854
*/

src/IO/Input/InputItem.php renamed to src/IO/Input/InputFlag.php

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,16 @@
88

99
namespace Inhere\Console\IO\Input;
1010

11+
use Inhere\Console\Contract\InputFlagInterface;
1112
use Inhere\Console\IO\Input;
1213

1314
/**
14-
* Class InputItem
15+
* Class InputFlag
1516
* - definition a input item(option|argument)
1617
*
1718
* @package Inhere\Console\IO\Input
1819
*/
19-
class InputItem
20+
abstract class InputFlag implements InputFlagInterface
2021
{
2122
/**
2223
* @var string
@@ -34,11 +35,11 @@ class InputItem
3435
private $mode;
3536

3637
/**
37-
* The argument data type. (eg: 'string', 'array', 'mixed')
38+
* The argument data type. (eg: 'int', 'bool', 'string', 'array', 'mixed')
3839
*
3940
* @var string
4041
*/
41-
private $type;
42+
private $type = '';
4243

4344
/**
4445
* The default value
@@ -79,14 +80,25 @@ public function __construct(string $name, int $mode = 0, string $description = '
7980
$this->setDescription($description);
8081
}
8182

83+
/******************************************************************
84+
* mode value
85+
*****************************************************************/
86+
8287
/**
88+
* @param int $mode
89+
*
8390
* @return bool
8491
*/
85-
public function isArray(): bool
92+
public function hasMode(int $mode): bool
8693
{
87-
return $this->mode === Input::ARG_IS_ARRAY;
94+
return ($this->mode & $mode) > 0;
8895
}
8996

97+
98+
/******************************************************************
99+
*
100+
*****************************************************************/
101+
90102
/**
91103
* @return string
92104
*/
@@ -166,4 +178,21 @@ public function setDescription(string $description): void
166178
{
167179
$this->description = $description;
168180
}
181+
182+
/**
183+
* @return array
184+
*/
185+
public function toArray(): array
186+
{
187+
return [
188+
'name' => $this->name,
189+
'mode' => $this->mode,
190+
'type' => $this->type,
191+
'default' => $this->default,
192+
'isArray' => $this->isArray(),
193+
'isOptional' => $this->isOptional(),
194+
'isRequired' => $this->isRequired(),
195+
'description' => $this->description,
196+
];
197+
}
169198
}

0 commit comments

Comments
 (0)