Skip to content

Commit 95a1545

Browse files
authored
Support setup command traits before running (#6577)
1 parent 72631c2 commit 95a1545

4 files changed

Lines changed: 76 additions & 0 deletions

File tree

src/Command.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
use function Hyperf\Collection\collect;
2525
use function Hyperf\Coroutine\run;
26+
use function Hyperf\Support\class_basename;
27+
use function Hyperf\Support\class_uses_recursive;
2628
use function Hyperf\Support\swoole_hook_flags;
2729
use function Hyperf\Tappable\tap;
2830

@@ -99,6 +101,8 @@ public function run(InputInterface $input, OutputInterface $output): int
99101
{
100102
$this->output = new SymfonyStyle($input, $output);
101103

104+
$this->setUpTraits();
105+
102106
return parent::run($this->input = $input, $this->output);
103107
}
104108

@@ -203,4 +207,20 @@ protected function execute(InputInterface $input, OutputInterface $output): int
203207

204208
return $this->exitCode >= 0 && $this->exitCode <= 255 ? $this->exitCode : self::INVALID;
205209
}
210+
211+
/**
212+
* Setup traits of command.
213+
*/
214+
protected function setUpTraits(): array
215+
{
216+
$uses = array_flip(class_uses_recursive(static::class));
217+
218+
foreach ($uses as $trait) {
219+
if (method_exists($this, $method = 'setUp' . class_basename($trait))) {
220+
$this->{$method}($this->input, $this->output);
221+
}
222+
}
223+
224+
return $uses;
225+
}
206226
}

tests/Command/FooTraitCommand.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact group@hyperf.io
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace HyperfTest\Command\Command;
13+
14+
class FooTraitCommand extends \Hyperf\Command\Command
15+
{
16+
use Traits\Foo;
17+
18+
public function __construct(string $name = null)
19+
{
20+
parent::__construct($name);
21+
}
22+
23+
public function handle()
24+
{
25+
}
26+
}

tests/Command/Traits/Foo.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact group@hyperf.io
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace HyperfTest\Command\Command\Traits;
13+
14+
use Symfony\Component\Console\Input\InputInterface;
15+
use Symfony\Component\Console\Output\OutputInterface;
16+
17+
trait Foo
18+
{
19+
private ?string $propertyFoo = null;
20+
21+
protected function setUpFoo(?InputInterface $input, ?OutputInterface $output)
22+
{
23+
$this->propertyFoo = 'foo';
24+
}
25+
}

tests/CommandTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use HyperfTest\Command\Command\FooCommand;
1818
use HyperfTest\Command\Command\FooExceptionCommand;
1919
use HyperfTest\Command\Command\FooExitCommand;
20+
use HyperfTest\Command\Command\FooTraitCommand;
2021
use HyperfTest\Command\Command\SwooleFlagsCommand;
2122
use Mockery;
2223
use PHPUnit\Framework\Attributes\CoversNothing;
@@ -72,6 +73,10 @@ public function testExitCodeWhenThrowException()
7273
$command = new ClassInvoker(new FooCommand());
7374
$exitCode = $command->execute($input, $output);
7475
$this->assertSame(0, $exitCode);
76+
77+
$command = new FooTraitCommand();
78+
$this->assertArrayHasKey(\HyperfTest\Command\Command\Traits\Foo::class, (fn () => $this->setUpTraits())->call($command));
79+
$this->assertSame('foo', (fn () => $this->propertyFoo)->call($command));
7580
}
7681

7782
public function testExitCodeWhenThrowExceptionInCoroutine()

0 commit comments

Comments
 (0)