Skip to content

Commit 4729979

Browse files
committed
up: update some for output class
1 parent 43e0495 commit 4729979

11 files changed

+198
-136
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
> require php 8.0+
66
7+
- feat: allow attach multi level sub-commands to command,group
8+
- feat: support add flags config for add alone Closure command
9+
710
## v4.0.x
811

912
> require php 7.3+

TODO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# TODO
22

33
- [x] Add more events supports
4-
- [ ] Add nested command supports
4+
- [x] Add nested command supports
55
- [ ] Simpler bash command completion support

src/Concern/AbstractOutput.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Inhere\Console\Concern;
1111

1212
use Inhere\Console\Contract\OutputInterface;
13+
use Toolkit\Stdlib\Obj\Traits\AutoConfigTrait;
1314

1415
/**
1516
* Class AbstractOutput
@@ -18,6 +19,8 @@
1819
*/
1920
abstract class AbstractOutput implements OutputInterface
2021
{
22+
use AutoConfigTrait;
23+
2124
/**
2225
* @return bool
2326
*/

src/Handler/AbstractHandler.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ public static function aliases(): array
134134
* @param Input|null $input
135135
* @param Output|null $output
136136
*/
137-
// public function __construct(Input $input, Output $output)
138137
public function __construct(Input $input = null, Output $output = null)
139138
{
140139
$this->input = $input;
@@ -148,7 +147,6 @@ public function __construct(Input $input = null, Output $output = null)
148147

149148
protected function init(): void
150149
{
151-
// $this->commentsVars = $this->annotationVars();
152150
$this->afterInit();
153151
$this->debugf('attach inner subcommands to "%s"', $this->getRealName());
154152
$this->addCommands($this->subCommands());
@@ -402,7 +400,7 @@ protected function beforeExecute(): bool
402400
* @param Input $input
403401
* @param Output $output
404402
*
405-
* @return void|mixed
403+
* @return mixed|void
406404
*/
407405
abstract protected function execute(Input $input, Output $output);
408406

src/IO/Output.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,13 @@ class Output extends StreamOutput
3434
/**
3535
* Output constructor.
3636
*/
37-
public function __construct()
37+
public function __construct(array $config = [])
3838
{
39-
parent::__construct(Cli::getOutputStream());
39+
if (!isset($config['stream'])) {
40+
$config['stream'] = Cli::getOutputStream();
41+
}
42+
43+
parent::__construct($config);
4044
}
4145

4246
/***************************************************************************
@@ -64,8 +68,8 @@ public function clearBuffer(): void
6468
*
6569
* @param bool $flush
6670
* @param bool $nl
67-
* @param bool|int $quit
68-
* @param array $opts
71+
* @param bool $quit
72+
* @param array{quitCode:int} $opts
6973
*
7074
* @see Console::stopBuffer()
7175
*/

src/IO/Output/BufferedOutput.php

Lines changed: 92 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@
99

1010
namespace Inhere\Console\IO\Output;
1111

12-
use Inhere\Console\Concern\AbstractOutput;
12+
use Inhere\Console\IO\Output;
13+
use function implode;
14+
use function is_array;
15+
use function sprintf;
1316
use function strlen;
1417
use const PHP_EOL;
1518

1619
/**
1720
* Class BufferedOutput
1821
* @package Inhere\Console\IO\Output
1922
*/
20-
class BufferedOutput extends AbstractOutput
23+
class BufferedOutput extends Output
2124
{
2225
/**
2326
* @var string
@@ -40,37 +43,114 @@ public function fetch(bool $reset = true): string
4043
return $str;
4144
}
4245

46+
/**
47+
* @return string
48+
*/
49+
public function toString(): string
50+
{
51+
return $this->fetch();
52+
}
53+
54+
public function reset(): void
55+
{
56+
$this->buffer = '';
57+
}
58+
59+
/**
60+
* @return string
61+
*/
4362
public function __toString(): string
4463
{
4564
return $this->fetch();
4665
}
4766

4867
/**
49-
* @param string $content
68+
* @param mixed $messages
69+
* @param bool $nl
70+
* @param bool $quit
71+
* @param array $opts
5072
*
5173
* @return int
5274
*/
53-
public function write(string $content): int
75+
public function write($messages, $nl = true, $quit = false, array $opts = []): int
5476
{
55-
$this->buffer .= $content;
56-
return strlen($content);
77+
if (is_array($messages)) {
78+
$str = implode($nl ? PHP_EOL : '', $messages);
79+
} else {
80+
$str = (string)$messages;
81+
}
82+
83+
if ($nl) {
84+
$str .= PHP_EOL;
85+
}
86+
87+
$this->buffer .= $str;
88+
return strlen($str);
5789
}
5890

5991
/**
60-
* @param string $content
92+
* @param mixed $messages
6193
* @param bool $quit
6294
* @param array $opts
6395
*
6496
* @return int
6597
*/
66-
public function writeln($content, bool $quit = false, array $opts = []): int
98+
public function writeln($messages, bool $quit = false, array $opts = []): int
6799
{
68-
$this->buffer .= $content . PHP_EOL;
69-
return strlen($content) + 1;
100+
return $this->write($messages, true, $quit, $opts);
70101
}
71102

72-
public function reset(): void
103+
/**
104+
* Write a message to output with format.
105+
*
106+
* @param string $format
107+
* @param mixed ...$args
108+
*
109+
* @return int
110+
*/
111+
public function writef(string $format, ...$args): int
112+
{
113+
return $this->write(sprintf($format, ...$args));
114+
}
115+
116+
/**
117+
* start buffering
118+
*/
119+
public function startBuffer(): void
120+
{
121+
}
122+
123+
/**
124+
* clear buffering
125+
*/
126+
public function clearBuffer(): void
127+
{
128+
$this->reset();
129+
}
130+
131+
/**
132+
* stop buffering and flush buffer text
133+
*
134+
* @param bool $flush
135+
* @param bool $nl
136+
* @param bool $quit
137+
* @param array{quitCode:int} $opts
138+
*
139+
* @see Console::stopBuffer()
140+
*/
141+
public function stopBuffer(bool $flush = true, bool $nl = false, bool $quit = false, array $opts = []): void
142+
{
143+
144+
}
145+
146+
/**
147+
* stop buffering and flush buffer text
148+
*
149+
* @param bool $nl
150+
* @param bool $quit
151+
* @param array $opts
152+
*/
153+
public function flush(bool $nl = false, bool $quit = false, array $opts = []): void
73154
{
74-
$this->buffer = '';
75155
}
76156
}

src/IO/Output/MemoryOutput.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace Inhere\Console\IO\Output;
1111

12+
use Toolkit\FsUtil\File;
1213
use function fopen;
1314

1415
/**
@@ -18,16 +19,33 @@
1819
*/
1920
class MemoryOutput extends StreamOutput
2021
{
21-
public function __construct()
22+
/**
23+
* Class constructor.
24+
*
25+
* @param array{stream: resource} $config
26+
*/
27+
public function __construct(array $config = [])
28+
{
29+
if (!isset($config['stream'])) {
30+
$config['stream'] = fopen('php://memory', 'rwb');
31+
}
32+
33+
parent::__construct($config);
34+
}
35+
36+
/**
37+
* @return string
38+
*/
39+
public function fetch(): string
2240
{
23-
parent::__construct(fopen('php://memory', 'rwb'));
41+
return File::streamReadAll($this->stream);
2442
}
2543

2644
/**
2745
* @return string
2846
*/
2947
public function getBuffer(): string
3048
{
31-
return '';
49+
return $this->fetch();
3250
}
3351
}

src/IO/Output/StreamOutput.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,7 @@ class StreamOutput extends AbstractOutput
3030
*
3131
* @var resource
3232
*/
33-
protected $stream;
34-
35-
/**
36-
* StreamInput constructor.
37-
*
38-
* @param resource $stream
39-
*/
40-
public function __construct($stream = STDOUT)
41-
{
42-
$this->setStream($stream);
43-
}
33+
protected $stream = STDOUT;
4434

4535
/**
4636
* @param string $content
@@ -90,13 +80,13 @@ public function getStream()
9080
/**
9181
* @param resource $stream
9282
*/
93-
protected function setStream($stream): void
83+
public function setStream($stream): void
9484
{
9585
File::assertStream($stream);
9686

9787
$meta = stream_get_meta_data($stream);
9888
if (!str_contains($meta['mode'], 'w') && !str_contains($meta['mode'], '+')) {
99-
throw new InvalidArgumentException('Expected a readable stream');
89+
throw new InvalidArgumentException('Expected a writeable stream');
10090
}
10191

10292
$this->stream = $stream;

src/IO/Output/TempOutput.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* The file is part of inhere/console
4+
*
5+
* @author https://github.com/inhere
6+
* @homepage https://github.com/inhere/php-console
7+
* @license https://github.com/inhere/php-console/blob/master/LICENSE
8+
*/
9+
10+
namespace Inhere\Console\IO\Output;
11+
12+
use Toolkit\FsUtil\File;
13+
use function fopen;
14+
15+
/**
16+
* Class BufferOutput
17+
*
18+
* @package Inhere\Console\IO\Output
19+
*/
20+
class TempOutput extends StreamOutput
21+
{
22+
/**
23+
* Class constructor.
24+
*
25+
* @param array{stream: resource} $config
26+
*/
27+
public function __construct(array $config = [])
28+
{
29+
if (!isset($config['stream'])) {
30+
// Set the limit to 5 MB.
31+
$fiveMbs = 5 * 1024 * 1024;
32+
// open
33+
$config['stream'] = fopen("php://temp/maxmemory:$fiveMbs", 'rwb');
34+
}
35+
36+
parent::__construct($config);
37+
}
38+
39+
/**
40+
* @return string
41+
*/
42+
public function fetch(): string
43+
{
44+
return File::streamReadAll($this->stream);
45+
}
46+
47+
/**
48+
* @return string
49+
*/
50+
public function getBuffer(): string
51+
{
52+
return $this->fetch();
53+
}
54+
}

0 commit comments

Comments
 (0)