Option.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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;
  12. use Hyperf\Watcher\Driver\ScanFileDriver;
  13. class Option
  14. {
  15. protected string $driver = ScanFileDriver::class;
  16. protected string $bin = PHP_BINARY;
  17. protected string $command = 'vendor/hyperf/watcher/watcher.php start';
  18. /**
  19. * @var string[]
  20. */
  21. protected array $watchDir = ['app', 'config'];
  22. /**
  23. * @var string[]
  24. */
  25. protected array $watchFile = ['.env'];
  26. /**
  27. * @var string[]
  28. */
  29. protected array $ext = ['.php', '.env'];
  30. protected int $scanInterval = 2000;
  31. public function __construct(array $options = [], array $dir = [], array $file = [], protected bool $restart = true)
  32. {
  33. isset($options['driver']) && $this->driver = $options['driver'];
  34. isset($options['bin']) && $this->bin = $options['bin'];
  35. isset($options['command']) && $this->command = $options['command'];
  36. isset($options['watch']['dir']) && $this->watchDir = (array) $options['watch']['dir'];
  37. isset($options['watch']['file']) && $this->watchFile = (array) $options['watch']['file'];
  38. isset($options['watch']['scan_interval']) && $this->scanInterval = (int) $options['watch']['scan_interval'];
  39. isset($options['ext']) && $this->ext = (array) $options['ext'];
  40. $this->watchDir = array_unique(array_merge($this->watchDir, $dir));
  41. $this->watchFile = array_unique(array_merge($this->watchFile, $file));
  42. }
  43. public function getDriver(): string
  44. {
  45. return $this->driver;
  46. }
  47. public function getBin(): string
  48. {
  49. return $this->bin;
  50. }
  51. public function getCommand(): string
  52. {
  53. return $this->command;
  54. }
  55. public function getWatchDir(): array
  56. {
  57. return $this->watchDir;
  58. }
  59. public function getWatchFile(): array
  60. {
  61. return $this->watchFile;
  62. }
  63. public function getExt(): array
  64. {
  65. return $this->ext;
  66. }
  67. public function getScanInterval(): int
  68. {
  69. return $this->scanInterval > 0 ? $this->scanInterval : 2000;
  70. }
  71. public function getScanIntervalSeconds(): float
  72. {
  73. return $this->getScanInterval() / 1000;
  74. }
  75. public function isRestart(): bool
  76. {
  77. return $this->restart;
  78. }
  79. }