WatchCommand.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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\Watcher\Command;
  12. use Hyperf\Command\Command;
  13. use Hyperf\Command\Concerns\NullDisableEventDispatcher;
  14. use Hyperf\Watcher\Option;
  15. use Hyperf\Watcher\Watcher;
  16. use Psr\Container\ContainerInterface;
  17. use Symfony\Component\Console\Input\InputOption;
  18. use function Hyperf\Support\make;
  19. class WatchCommand extends Command
  20. {
  21. use NullDisableEventDispatcher;
  22. public function __construct(protected ContainerInterface $container)
  23. {
  24. parent::__construct('server:watch');
  25. $this->setDescription('watch command');
  26. $this->addOption('config', 'C', InputOption::VALUE_OPTIONAL, '', '.watcher.php');
  27. $this->addOption('file', 'F', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, '', []);
  28. $this->addOption('dir', 'D', InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, '', []);
  29. $this->addOption('no-restart', 'N', InputOption::VALUE_NONE, 'Whether no need to restart server');
  30. }
  31. public function handle()
  32. {
  33. $options = (array) include dirname(__DIR__, 2) . '/publish/watcher.php';
  34. if (file_exists($configFile = $this->input->getOption('config'))) {
  35. $options = array_replace($options, (array) include $configFile);
  36. } elseif (file_exists($configFile = BASE_PATH . '/config/autoload/watcher.php')) { // Compatible with old version, will be removed in the v3.1.
  37. $options = array_replace($options, (array) include $configFile);
  38. }
  39. $option = make(Option::class, [
  40. 'options' => $options,
  41. 'dir' => $this->input->getOption('dir'),
  42. 'file' => $this->input->getOption('file'),
  43. 'restart' => ! $this->input->getOption('no-restart'),
  44. ]);
  45. $watcher = make(Watcher::class, [
  46. 'option' => $option,
  47. 'output' => $this->output,
  48. ]);
  49. $watcher->run();
  50. }
  51. }