Skip to content

Commit 9fd4143

Browse files
authored
Support event dispatcher for command by default. (#4678)
1 parent 8c385ea commit 9fd4143

File tree

4 files changed

+53
-36
lines changed

4 files changed

+53
-36
lines changed

src/Command.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
abstract class Command extends SymfonyCommand
3131
{
32-
use EnableEventDispatcher;
32+
use DisableEventDispatcher;
3333

3434
/**
3535
* The name of the command.
@@ -96,7 +96,7 @@ public function __construct(string $name = null)
9696
! empty($this->description) && $this->setDescription($this->description);
9797
}
9898

99-
$this->addEnableDispatcherOption();
99+
$this->addDisableDispatcherOption();
100100
}
101101

102102
/**
@@ -413,7 +413,8 @@ protected function configure()
413413

414414
protected function execute(InputInterface $input, OutputInterface $output)
415415
{
416-
$this->enableDispatcher($input);
416+
$this->disableDispatcher($input);
417+
417418
$callback = function () {
418419
try {
419420
$this->eventDispatcher && $this->eventDispatcher->dispatch(new Event\BeforeHandle($this));

src/DisableEventDispatcher.php

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 [email protected]
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace Hyperf\Command;
13+
14+
use Hyperf\Utils\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()
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+
}

src/EnableEventDispatcher.php

Lines changed: 0 additions & 33 deletions
This file was deleted.

tests/CommandTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212
namespace HyperfTest\Command;
1313

14+
use Hyperf\Utils\ApplicationContext;
1415
use Hyperf\Utils\Reflection\ClassInvoker;
1516
use HyperfTest\Command\Command\DefaultSwooleFlagsCommand;
1617
use HyperfTest\Command\Command\FooCommand;
@@ -19,6 +20,8 @@
1920
use HyperfTest\Command\Command\SwooleFlagsCommand;
2021
use Mockery;
2122
use PHPUnit\Framework\TestCase;
23+
use Psr\Container\ContainerInterface;
24+
use Psr\EventDispatcher\EventDispatcherInterface;
2225
use Symfony\Component\Console\Input\InputInterface;
2326
use Symfony\Component\Console\Output\OutputInterface;
2427

@@ -47,6 +50,9 @@ public function testHookFlags()
4750
*/
4851
public function testExitCodeWhenThrowException()
4952
{
53+
ApplicationContext::setContainer($container = Mockery::mock(ContainerInterface::class));
54+
$container->shouldReceive('has')->with(EventDispatcherInterface::class)->andReturnFalse();
55+
5056
/** @var FooExceptionCommand $command */
5157
$command = new ClassInvoker(new FooExceptionCommand('foo'));
5258
$input = Mockery::mock(InputInterface::class);

0 commit comments

Comments
 (0)