Skip to content

Commit 06df0f8

Browse files
authored
Move ConfirmableTrait/DisableEventDispatcher/NullDisableEventDispatcher to Concerns (#5882)
1 parent d63a1a5 commit 06df0f8

7 files changed

Lines changed: 136 additions & 76 deletions

src/Command.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
abstract class Command extends SymfonyCommand
3737
{
38-
use DisableEventDispatcher;
38+
use Concerns\DisableEventDispatcher;
3939

4040
/**
4141
* The name of the command.

src/Concerns/Confirmable.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 Hyperf\Command\Concerns;
13+
14+
use Closure;
15+
16+
use function Hyperf\Support\value;
17+
18+
trait Confirmable
19+
{
20+
/**
21+
* Confirm before proceeding with the action.
22+
*
23+
* This method only asks for confirmation in production.
24+
*/
25+
public function confirmToProceed(string $warning = 'Application In Production!', null|bool|Closure $callback = null): bool
26+
{
27+
$callback ??= $this->isShouldConfirm();
28+
29+
$shouldConfirm = value($callback);
30+
31+
if ($shouldConfirm) {
32+
if ($this->input->getOption('force')) {
33+
return true;
34+
}
35+
36+
$this->alert($warning);
37+
38+
$confirmed = $this->confirm('Do you really wish to run this command?');
39+
40+
if (! $confirmed) {
41+
$this->comment('Command Cancelled!');
42+
43+
return false;
44+
}
45+
}
46+
47+
return true;
48+
}
49+
50+
protected function isShouldConfirm(): bool
51+
{
52+
return is_callable(['Composer\InstalledVersions', 'getRootPackage'])
53+
&& (\Composer\InstalledVersions::getRootPackage()['dev'] ?? false) === false;
54+
}
55+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 Hyperf\Command\Concerns;
13+
14+
use Hyperf\Context\ApplicationContext;
15+
use Psr\EventDispatcher\EventDispatcherInterface;
16+
use Symfony\Component\Console\Input\InputInterface;
17+
use Symfony\Component\Console\Input\InputOption;
18+
19+
trait DisableEventDispatcher
20+
{
21+
public function addDisableDispatcherOption(): void
22+
{
23+
$this->addOption('disable-event-dispatcher', null, InputOption::VALUE_NONE, 'Whether disable event dispatcher.');
24+
}
25+
26+
public function disableDispatcher(InputInterface $input)
27+
{
28+
if (! $input->getOption('disable-event-dispatcher')) {
29+
if (! ApplicationContext::hasContainer()) {
30+
return;
31+
}
32+
33+
$container = ApplicationContext::getContainer();
34+
if (! $container->has(EventDispatcherInterface::class)) {
35+
return;
36+
}
37+
38+
$dispatcher = $container->get(EventDispatcherInterface::class);
39+
40+
$this->eventDispatcher = $dispatcher instanceof EventDispatcherInterface ? $dispatcher : null;
41+
}
42+
}
43+
}
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 Hyperf\Command\Concerns;
13+
14+
use Symfony\Component\Console\Input\InputInterface;
15+
16+
trait NullDisableEventDispatcher
17+
{
18+
public function addDisableDispatcherOption(): void
19+
{
20+
}
21+
22+
public function disableDispatcher(InputInterface $input)
23+
{
24+
}
25+
}

src/ConfirmableTrait.php

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,45 +11,10 @@
1111
*/
1212
namespace Hyperf\Command;
1313

14-
use Closure;
15-
16-
use function Hyperf\Support\value;
17-
14+
/**
15+
* @deprecated since 3.0.27, remove in 3.1.0, use \Hyperf\Command\Concerns\Confirmable instead.
16+
*/
1817
trait ConfirmableTrait
1918
{
20-
/**
21-
* Confirm before proceeding with the action.
22-
*
23-
* This method only asks for confirmation in production.
24-
*/
25-
public function confirmToProceed(string $warning = 'Application In Production!', null|bool|Closure $callback = null): bool
26-
{
27-
$callback ??= $this->isShouldConfirm();
28-
29-
$shouldConfirm = value($callback);
30-
31-
if ($shouldConfirm) {
32-
if ($this->input->getOption('force')) {
33-
return true;
34-
}
35-
36-
$this->alert($warning);
37-
38-
$confirmed = $this->confirm('Do you really wish to run this command?');
39-
40-
if (! $confirmed) {
41-
$this->comment('Command Cancelled!');
42-
43-
return false;
44-
}
45-
}
46-
47-
return true;
48-
}
49-
50-
protected function isShouldConfirm(): bool
51-
{
52-
return is_callable(['Composer\InstalledVersions', 'getRootPackage'])
53-
&& (\Composer\InstalledVersions::getRootPackage()['dev'] ?? false) === false;
54-
}
19+
use Concerns\Confirmable;
5520
}

src/DisableEventDispatcher.php

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,10 @@
1111
*/
1212
namespace Hyperf\Command;
1313

14-
use Hyperf\Context\ApplicationContext;
15-
use Psr\EventDispatcher\EventDispatcherInterface;
16-
use Symfony\Component\Console\Input\InputInterface;
17-
use Symfony\Component\Console\Input\InputOption;
18-
14+
/**
15+
* @deprecated since 3.0.27, remove in 3.1.0, use \Hyperf\Command\Concerns\DisableEventDispatcher instead.
16+
*/
1917
trait DisableEventDispatcher
2018
{
21-
public function addDisableDispatcherOption(): void
22-
{
23-
$this->addOption('disable-event-dispatcher', null, InputOption::VALUE_NONE, 'Whether disable event dispatcher.');
24-
}
25-
26-
public function disableDispatcher(InputInterface $input)
27-
{
28-
if (! $input->getOption('disable-event-dispatcher')) {
29-
if (! ApplicationContext::hasContainer()) {
30-
return;
31-
}
32-
33-
$container = ApplicationContext::getContainer();
34-
if (! $container->has(EventDispatcherInterface::class)) {
35-
return;
36-
}
37-
38-
$dispatcher = $container->get(EventDispatcherInterface::class);
39-
40-
$this->eventDispatcher = $dispatcher instanceof EventDispatcherInterface ? $dispatcher : null;
41-
}
42-
}
19+
use Concerns\DisableEventDispatcher;
4320
}

src/NullDisableEventDispatcher.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,10 @@
1111
*/
1212
namespace Hyperf\Command;
1313

14-
use Symfony\Component\Console\Input\InputInterface;
15-
14+
/**
15+
* @deprecated since 3.0.27, remove in 3.1.0, use \Hyperf\Command\Concerns\NullDisableEventDispatcher instead.
16+
*/
1617
trait NullDisableEventDispatcher
1718
{
18-
public function addDisableDispatcherOption(): void
19-
{
20-
}
21-
22-
public function disableDispatcher(InputInterface $input)
23-
{
24-
}
19+
use Concerns\NullDisableEventDispatcher;
2520
}

0 commit comments

Comments
 (0)