Skip to content

Commit 5b887a4

Browse files
committed
perf(io): update some use io class logic
1 parent 07a1e51 commit 5b887a4

File tree

7 files changed

+42
-72
lines changed

7 files changed

+42
-72
lines changed

resource/deprecated/AbstractInput.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
*
2828
* @package Inhere\Console\IO
2929
*/
30-
abstract class AbstractInput implements InputInterface
30+
abstract class AbstractInput // implements InputInterface
3131
{
3232
use InputArgumentsTrait, InputOptionsTrait;
3333

src/AbstractApplication.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
use Inhere\Console\Concern\StyledOutputAwareTrait;
1919
use Inhere\Console\Contract\ApplicationInterface;
2020
use Inhere\Console\Contract\ErrorHandlerInterface;
21-
use Inhere\Console\Contract\InputInterface;
22-
use Inhere\Console\Contract\OutputInterface;
2321
use Inhere\Console\IO\Input;
2422
use Inhere\Console\IO\Output;
2523
use Inhere\Console\Util\Helper;
@@ -370,10 +368,10 @@ public function runWithArgs(array $args): mixed
370368
}
371369

372370
/**
373-
* @param InputInterface $input
374-
* @param OutputInterface $output
371+
* @param Input $input
372+
* @param Output $output
375373
*/
376-
public function runWithIO(InputInterface $input, OutputInterface $output): void
374+
public function runWithIO(Input $input, Output $output): void
377375
{
378376
$app = $this->copy();
379377
$app->setInput($input);
@@ -384,13 +382,13 @@ public function runWithIO(InputInterface $input, OutputInterface $output): void
384382
}
385383

386384
/**
387-
* @param string $command
388-
* @param InputInterface $input
389-
* @param OutputInterface $output
385+
* @param string $command
386+
* @param Input $input
387+
* @param Output $output
390388
*
391389
* @return mixed
392390
*/
393-
public function subRun(string $command, InputInterface $input, OutputInterface $output): mixed
391+
public function subRun(string $command, Input $input, Output $output): mixed
394392
{
395393
$app = $this->copy();
396394
$app->setInput($input);

src/Concern/FormatOutputAwareTrait.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,6 @@ public function write($messages, $nl = true, $quit = false, array $opts = []): i
3939
], $opts));
4040
}
4141

42-
// public function print(...$args): void
43-
// {
44-
// if (count($args) > 1) {
45-
// echo ;
46-
// }
47-
//
48-
//
49-
// }
50-
5142
/**
5243
* @param ...$args
5344
*/

src/Concern/InputOutputAwareTrait.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@
1010
namespace Inhere\Console\Concern;
1111

1212
use Inhere\Console\IO\Input;
13-
use Inhere\Console\Contract\InputInterface;
1413
use Inhere\Console\IO\Output;
15-
use Inhere\Console\Contract\OutputInterface;
1614
use Toolkit\PFlag\FlagsParser;
17-
use Toolkit\PFlag\SFlags;
1815

1916
/**
2017
* Class InputOutputAwareTrait
@@ -29,14 +26,14 @@ trait InputOutputAwareTrait
2926
protected ?FlagsParser $flags;
3027

3128
/**
32-
* @var InputInterface|null
29+
* @var Input|null
3330
*/
34-
protected ?InputInterface $input;
31+
protected ?Input $input;
3532

3633
/**
37-
* @var OutputInterface|null
34+
* @var Output|null
3835
*/
39-
protected ?OutputInterface $output;
36+
protected ?Output $output;
4037

4138
/**
4239
* @return string
@@ -64,7 +61,7 @@ public function getScriptName(): string
6461

6562
/**
6663
* @param string $question
67-
* @param bool $nl
64+
* @param bool $nl
6865
*
6966
* @return string
7067
*/
@@ -94,47 +91,47 @@ public function writeln(mixed $message): int
9491
}
9592

9693
/**
97-
* @return Input|InputInterface
94+
* @return Input
9895
*/
99-
public function getInput(): InputInterface
96+
public function getInput(): Input
10097
{
10198
return $this->input;
10299
}
103100

104101
/**
105-
* @param InputInterface $input
102+
* @param Input $input
106103
*/
107-
public function setInput(InputInterface $input): void
104+
public function setInput(Input $input): void
108105
{
109106
$this->input = $input;
110107
}
111108

112109
/**
113-
* @return Output|OutputInterface
110+
* @return Output
114111
*/
115-
public function getOutput(): OutputInterface
112+
public function getOutput(): Output
116113
{
117114
return $this->output;
118115
}
119116

120117
/**
121-
* @param Output|OutputInterface $output
118+
* @param Output $output
122119
*/
123-
public function setOutput(OutputInterface $output): void
120+
public function setOutput(Output $output): void
124121
{
125122
$this->output = $output;
126123
}
127124

128125
/**
129-
* @return FlagsParser|SFlags
126+
* @return FlagsParser
130127
*/
131128
public function getFlags(): FlagsParser
132129
{
133130
return $this->flags;
134131
}
135132

136133
/**
137-
* @param FlagsParser|SFlags $flags
134+
* @param FlagsParser $flags
138135
*/
139136
public function setFlags(FlagsParser $flags): void
140137
{

src/Contract/OutputInterface.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* Class OutputInterface
1414
*
1515
* @package Inhere\Console\Contract
16-
* @method error(string $string)
1716
*/
1817
interface OutputInterface
1918
{

src/IO/Output.php

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,22 +31,12 @@ class Output extends StreamOutput
3131
*/
3232
protected $errorStream;
3333

34-
/**
35-
* 控制台窗口(字体/背景)颜色添加处理
36-
* window colors
37-
*
38-
* @var Style|null
39-
*/
40-
protected ?Style $style = null;
41-
4234
/**
4335
* Output constructor.
4436
*/
4537
public function __construct()
4638
{
4739
parent::__construct(Cli::getOutputStream());
48-
49-
$this->getStyle();
5040
}
5141

5242
/***************************************************************************
@@ -136,7 +126,7 @@ public function readln(string $question = '', bool $nl = false): string
136126
*/
137127
public function stderr(string $text = '', bool $nl = true): int
138128
{
139-
return Console::write($text, $nl, [
129+
return Console::write($text, $nl, false, [
140130
'steam' => $this->errorStream,
141131
]);
142132
}
@@ -150,11 +140,7 @@ public function stderr(string $text = '', bool $nl = true): int
150140
*/
151141
public function getStyle(): Style
152142
{
153-
if (!$this->style) {
154-
$this->style = Style::instance();
155-
}
156-
157-
return $this->style;
143+
return Style::global();
158144
}
159145

160146
/**

src/Util/ProgressBar.php

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
use Closure;
1313
use Inhere\Console\IO\Output;
14-
use Inhere\Console\Contract\OutputInterface;
1514
use LogicException;
1615
use RuntimeException;
1716
use Toolkit\Stdlib\Helper\Format;
@@ -24,7 +23,7 @@
2423
* @package Inhere\Console\Util
2524
* @form \Symfony\Component\Console\Helper\ProgressBar
2625
*
27-
* ```
26+
* ```bash
2827
* 1 [->--------------------------]
2928
* 3 [■■■>------------------------]
3029
* 25/50 [==============>-------------] 50%
@@ -121,9 +120,9 @@ class ProgressBar
121120
private bool $firstRun = true;
122121

123122
/**
124-
* @var OutputInterface
123+
* @var Output
125124
*/
126-
private Output|OutputInterface $output;
125+
private Output $output;
127126

128127
/**
129128
* messages
@@ -143,21 +142,21 @@ class ProgressBar
143142
public const DEFAULT_FORMAT = '[{@bar}] {@percent:3s}%({@current}/{@max}) {@elapsed:6s}/{@estimated:-6s} {@memory:6s}';
144143

145144
/**
146-
* @param OutputInterface|null $output
147-
* @param int $maxSteps
145+
* @param Output|null $output
146+
* @param int $maxSteps
148147
*
149148
* @return ProgressBar
150149
*/
151-
public static function create(OutputInterface $output = null, int $maxSteps = 0): ProgressBar
150+
public static function create(Output $output = null, int $maxSteps = 0): ProgressBar
152151
{
153152
return new self($output, $maxSteps);
154153
}
155154

156155
/**
157-
* @param OutputInterface|null $output
156+
* @param Output|null $output
158157
* @param int $maxSteps
159158
*/
160-
public function __construct(OutputInterface $output = null, int $maxSteps = 0)
159+
public function __construct(Output $output = null, int $maxSteps = 0)
161160
{
162161
$this->output = $output ?: new Output;
163162

@@ -168,11 +167,11 @@ public function __construct(OutputInterface $output = null, int $maxSteps = 0)
168167
/**
169168
* 开始
170169
*
171-
* @param null $maxSteps
170+
* @param int|null $maxSteps
172171
*
173172
* @throws LogicException
174173
*/
175-
public function start($maxSteps = null): void
174+
public function start(int $maxSteps = null): void
176175
{
177176
if ($this->started) {
178177
throw new LogicException('Progress bar already started.');
@@ -260,7 +259,7 @@ public function finish(): void
260259
*/
261260
public function display(): void
262261
{
263-
if (null === $this->format) {
262+
if (!$this->format) {
264263
$this->format = self::DEFAULT_FORMAT;
265264
}
266265

@@ -306,10 +305,10 @@ public function render(string $text): void
306305
}
307306

308307
/**
309-
* @return mixed
308+
* @return string
310309
* @throws RuntimeException
311310
*/
312-
protected function buildLine(): mixed
311+
protected function buildLine(): string
313312
{
314313
// $regex = "{%([a-z\-_]+)(?:\:([^%]+))?%}i";
315314
return preg_replace_callback('/{@([\w]+)(?:\:([\w-]+))?}/i', function ($matches) {
@@ -543,17 +542,17 @@ public function getPercent(): float
543542
}
544543

545544
/**
546-
* @return mixed
545+
* @return int
547546
*/
548-
public function getStartTime(): mixed
547+
public function getStartTime(): int
549548
{
550549
return $this->startTime;
551550
}
552551

553552
/**
554-
* @return mixed
553+
* @return int
555554
*/
556-
public function getFinishTime(): mixed
555+
public function getFinishTime(): int
557556
{
558557
return $this->finishTime;
559558
}

0 commit comments

Comments
 (0)