DisableEventDispatcher.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\Command\Concerns;
  12. use Hyperf\Context\ApplicationContext;
  13. use Psr\EventDispatcher\EventDispatcherInterface;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Input\InputOption;
  16. trait DisableEventDispatcher
  17. {
  18. public function addDisableDispatcherOption(): void
  19. {
  20. $this->addOption('disable-event-dispatcher', null, InputOption::VALUE_NONE, 'Whether disable event dispatcher.');
  21. }
  22. public function disableDispatcher(InputInterface $input)
  23. {
  24. if (! $input->getOption('disable-event-dispatcher')) {
  25. if (! ApplicationContext::hasContainer()) {
  26. return;
  27. }
  28. $container = ApplicationContext::getContainer();
  29. if (! $container->has(EventDispatcherInterface::class)) {
  30. return;
  31. }
  32. $dispatcher = $container->get(EventDispatcherInterface::class);
  33. $this->eventDispatcher = $dispatcher instanceof EventDispatcherInterface ? $dispatcher : null;
  34. }
  35. }
  36. }