Context.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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\Context;
  12. use ArrayObject;
  13. use Closure;
  14. use Hyperf\Engine\Coroutine;
  15. use function Hyperf\Support\value;
  16. /**
  17. * @template TKey of string
  18. * @template TValue
  19. */
  20. class Context
  21. {
  22. /**
  23. * @var array<TKey, TValue>
  24. */
  25. protected static array $nonCoContext = [];
  26. /**
  27. * @param TKey $id
  28. * @param TValue $value
  29. * @return TValue
  30. */
  31. public static function set(string $id, mixed $value, ?int $coroutineId = null): mixed
  32. {
  33. if (Coroutine::id() > 0) {
  34. Coroutine::getContextFor($coroutineId)[$id] = $value;
  35. } else {
  36. static::$nonCoContext[$id] = $value;
  37. }
  38. return $value;
  39. }
  40. /**
  41. * @param TKey $id
  42. * @return TValue
  43. */
  44. public static function get(string $id, mixed $default = null, ?int $coroutineId = null): mixed
  45. {
  46. if (Coroutine::id() > 0) {
  47. return Coroutine::getContextFor($coroutineId)[$id] ?? $default;
  48. }
  49. return static::$nonCoContext[$id] ?? $default;
  50. }
  51. /**
  52. * @param TKey $id
  53. */
  54. public static function has(string $id, ?int $coroutineId = null): bool
  55. {
  56. if (Coroutine::id() > 0) {
  57. return isset(Coroutine::getContextFor($coroutineId)[$id]);
  58. }
  59. return isset(static::$nonCoContext[$id]);
  60. }
  61. /**
  62. * Release the context when you are not in coroutine environment.
  63. *
  64. * @param TKey $id
  65. */
  66. public static function destroy(string $id, ?int $coroutineId = null): void
  67. {
  68. if (Coroutine::id() > 0) {
  69. unset(Coroutine::getContextFor($coroutineId)[$id]);
  70. }
  71. unset(static::$nonCoContext[$id]);
  72. }
  73. /**
  74. * Copy the context from a coroutine to current coroutine.
  75. * This method will delete the origin values in current coroutine.
  76. */
  77. public static function copy(int $fromCoroutineId, array $keys = []): void
  78. {
  79. $from = Coroutine::getContextFor($fromCoroutineId);
  80. if ($from === null) {
  81. return;
  82. }
  83. $current = Coroutine::getContextFor();
  84. if ($keys) {
  85. $map = array_intersect_key($from->getArrayCopy(), array_flip($keys));
  86. } else {
  87. $map = $from->getArrayCopy();
  88. }
  89. $current->exchangeArray($map);
  90. }
  91. /**
  92. * Retrieve the value and override it by closure.
  93. *
  94. * @param TKey $id
  95. * @param (Closure(TValue):TValue) $closure
  96. */
  97. public static function override(string $id, Closure $closure, ?int $coroutineId = null): mixed
  98. {
  99. $value = null;
  100. if (self::has($id, $coroutineId)) {
  101. $value = self::get($id, null, $coroutineId);
  102. }
  103. $value = $closure($value);
  104. self::set($id, $value, $coroutineId);
  105. return $value;
  106. }
  107. /**
  108. * Retrieve the value and store it if not exists.
  109. *
  110. * @param TKey $id
  111. * @param TValue $value
  112. * @return TValue
  113. */
  114. public static function getOrSet(string $id, mixed $value, ?int $coroutineId = null): mixed
  115. {
  116. if (! self::has($id, $coroutineId)) {
  117. return self::set($id, value($value), $coroutineId);
  118. }
  119. return self::get($id, null, $coroutineId);
  120. }
  121. /**
  122. * @return null|array<TKey, TValue>|ArrayObject<TKey, TValue>
  123. */
  124. public static function getContainer(?int $coroutineId = null)
  125. {
  126. if (Coroutine::id() > 0) {
  127. return Coroutine::getContextFor($coroutineId);
  128. }
  129. return static::$nonCoContext;
  130. }
  131. }