GlobalValue.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * @author luolongf <[email protected]>
  4. * @date 2024-01-25
  5. * @time 18:22
  6. */
  7. namespace Luolongfei\App\Console;
  8. class GlobalValue extends Base
  9. {
  10. /**
  11. * @var GlobalValue
  12. */
  13. private static $instance;
  14. /**
  15. * @var array $values
  16. */
  17. private $values = [];
  18. /**
  19. * @return GlobalValue|self
  20. */
  21. public static function getInstance()
  22. {
  23. if (!self::$instance instanceof self) {
  24. self::$instance = new self();
  25. }
  26. return self::$instance;
  27. }
  28. private function __construct()
  29. {
  30. }
  31. private function __clone()
  32. {
  33. }
  34. /**
  35. * @param string $name
  36. * @param string $value
  37. *
  38. * @return void
  39. */
  40. public function set(string $name, string $value)
  41. {
  42. $this->values[$name] = $value;
  43. }
  44. /**
  45. * @param string $name
  46. * @param string|null $default
  47. *
  48. * @return string|null
  49. */
  50. public function get(string $name, ?string $default = null)
  51. {
  52. return isset($this->values[$name]) ? $this->values[$name] : $default;
  53. }
  54. /**
  55. * @param string $name
  56. *
  57. * @return void
  58. */
  59. public function del(string $name)
  60. {
  61. unset($this->values[$name]);
  62. }
  63. /**
  64. * @param string $name
  65. *
  66. * @return bool
  67. */
  68. public function has(string $name)
  69. {
  70. return isset($this->values[$name]);
  71. }
  72. }