You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

104 rivejä
1.9 KiB

  1. <?php
  2. /**
  3. * @Author: 杰少Pakey
  4. * @Email : Pakey@qq.com
  5. * @File : registry.php
  6. */
  7. namespace Kuxin;
  8. use Kuxin\Helper\Arr;
  9. /**
  10. * Class Registry
  11. *
  12. * @package Kuxin
  13. * @author Pakey <pakey@qq.com>
  14. */
  15. class Registry
  16. {
  17. protected static $_data;
  18. /**
  19. * 获取
  20. *
  21. * @param $key
  22. * @param $default
  23. * @return mixed
  24. */
  25. public static function get(string $key, $default = null)
  26. {
  27. return isset(self::$_data[$key]) ? self::$_data[$key] : (is_callable($default) ? $default($key) : $default);
  28. }
  29. /**
  30. * 设置
  31. *
  32. * @param string $key
  33. * @param mixed $value
  34. */
  35. public static function set(string $key, $value): void
  36. {
  37. if (is_array($key)) {
  38. self::$_data = Arr::merge(self::$_data, $key);
  39. } else {
  40. self::$_data[$key] = $value;
  41. }
  42. }
  43. /**
  44. * 合并信息
  45. *
  46. * @param $key
  47. * @param $value
  48. */
  49. public static function merge($key, $value)
  50. {
  51. $data = (array)self::get($key);
  52. $data[] = $value;
  53. self::set($key, $data);
  54. }
  55. /**
  56. * 移除
  57. *
  58. * @param $key
  59. */
  60. public static function remove(string $key): void
  61. {
  62. if (isset(self::$_data[$key])) {
  63. unset(self::$_data[$key]);
  64. }
  65. }
  66. /**
  67. * 对值增加
  68. *
  69. * @param $key
  70. * @param int $num
  71. */
  72. public static function setInc(string $key, int $num = 1): void
  73. {
  74. if (isset(self::$_data[$key])) {
  75. self::$_data[$key] += $num;
  76. } else {
  77. self::$_data[$key] = $num;
  78. }
  79. }
  80. /**
  81. * 对值减少
  82. *
  83. * @param $key
  84. * @param $num
  85. */
  86. public static function setDec(string $key, int $num = 1): void
  87. {
  88. if (isset(self::$_data[$key])) {
  89. self::$_data[$key] -= $num;
  90. } else {
  91. self::$_data[$key] = $num;
  92. }
  93. }
  94. }