Skip to content

Commit e71af68

Browse files
authored
Add Hyperf\Command\Concerns\Prohibitable (#7265)
1 parent 4304727 commit e71af68

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

src/Concerns/Prohibitable.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
13+
namespace Hyperf\Command\Concerns;
14+
15+
trait Prohibitable
16+
{
17+
/**
18+
* Indicates if the command should be prohibited from running.
19+
*
20+
* @var bool
21+
*/
22+
protected static $prohibitedFromRunning = false;
23+
24+
/**
25+
* Indicate whether the command should be prohibited from running.
26+
*
27+
* @param bool $prohibit
28+
*/
29+
public static function prohibit($prohibit = true)
30+
{
31+
static::$prohibitedFromRunning = $prohibit;
32+
}
33+
34+
/**
35+
* Determine if the command is prohibited from running and display a warning if so.
36+
*
37+
* @return bool
38+
*/
39+
protected function isProhibited(bool $quiet = false)
40+
{
41+
if (! static::$prohibitedFromRunning) {
42+
return false;
43+
}
44+
45+
if (! $quiet) {
46+
$this->output->warn('This command is prohibited from running in this environment.');
47+
}
48+
49+
return true;
50+
}
51+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
13+
namespace HyperfTest\Command\Command;
14+
15+
use Hyperf\Command\Command;
16+
use Hyperf\Command\Concerns\Prohibitable;
17+
18+
class FooProhibitableCommand extends Command
19+
{
20+
use Prohibitable;
21+
22+
public function handle()
23+
{
24+
if ($this->isProhibited()) {
25+
return self::FAILURE;
26+
}
27+
28+
return self::SUCCESS;
29+
}
30+
}

tests/CommandTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use HyperfTest\Command\Command\FooCommand;
1919
use HyperfTest\Command\Command\FooExceptionCommand;
2020
use HyperfTest\Command\Command\FooExitCommand;
21+
use HyperfTest\Command\Command\FooProhibitableCommand;
2122
use HyperfTest\Command\Command\FooTraitCommand;
2223
use HyperfTest\Command\Command\SwooleFlagsCommand;
2324
use HyperfTest\Command\Command\Traits\Foo;
@@ -101,4 +102,31 @@ public function testExitCodeWhenThrowExceptionInCoroutine()
101102
{
102103
$this->testExitCodeWhenThrowException();
103104
}
105+
106+
public function testProhibitableCommand()
107+
{
108+
$application = Mockery::mock(ConsoleApplication::class);
109+
$application->shouldReceive('getHelperSet');
110+
111+
$output = Mockery::mock(OutputInterface::class);
112+
$command = new ClassInvoker(new FooProhibitableCommand());
113+
$command->setApplication($application);
114+
$command->setOutput($output);
115+
$input = Mockery::mock(InputInterface::class);
116+
$input->shouldReceive('getOption')->andReturnFalse();
117+
$result = $command->execute($input, $output);
118+
$this->assertSame(FooProhibitableCommand::SUCCESS, $result);
119+
120+
FooProhibitableCommand::prohibit(true);
121+
122+
$output = Mockery::mock(OutputInterface::class);
123+
$output->shouldReceive('warn')->andReturn();
124+
$command = new ClassInvoker(new FooProhibitableCommand());
125+
$command->setApplication($application);
126+
$command->setOutput($output);
127+
$input = Mockery::mock(InputInterface::class);
128+
$input->shouldReceive('getOption')->andReturnFalse();
129+
$result = $command->execute($input, $output);
130+
$this->assertSame(FooProhibitableCommand::FAILURE, $result);
131+
}
104132
}

0 commit comments

Comments
 (0)