Context.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * This file is part of webman.
  4. *
  5. * Licensed under The MIT License
  6. * For full copyright and license information, please see the MIT-LICENSE.txt
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @author walkor<walkor@workerman.net>
  10. * @copyright walkor<walkor@workerman.net>
  11. * @link http://www.workerman.net/
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Webman;
  15. use Fiber;
  16. use SplObjectStorage;
  17. use StdClass;
  18. use Swow\Coroutine;
  19. use WeakMap;
  20. use Workerman\Events\Revolt;
  21. use Workerman\Events\Swoole;
  22. use Workerman\Events\Swow;
  23. use Workerman\Worker;
  24. use function property_exists;
  25. /**
  26. * Class Context
  27. * @package Webman
  28. */
  29. class Context
  30. {
  31. /**
  32. * @var SplObjectStorage|WeakMap
  33. */
  34. protected static $objectStorage;
  35. /**
  36. * @var StdClass
  37. */
  38. protected static $object;
  39. /**
  40. * @return StdClass
  41. */
  42. protected static function getObject(): StdClass
  43. {
  44. if (!static::$objectStorage) {
  45. static::$objectStorage = class_exists(WeakMap::class) ? new WeakMap() : new SplObjectStorage();
  46. static::$object = new StdClass;
  47. }
  48. $key = static::getKey();
  49. if (!isset(static::$objectStorage[$key])) {
  50. static::$objectStorage[$key] = new StdClass;
  51. }
  52. return static::$objectStorage[$key];
  53. }
  54. /**
  55. * @return mixed
  56. */
  57. protected static function getKey()
  58. {
  59. switch (Worker::$eventLoopClass) {
  60. case Revolt::class:
  61. return Fiber::getCurrent();
  62. case Swoole::class:
  63. return \Swoole\Coroutine::getContext();
  64. case Swow::class:
  65. return Coroutine::getCurrent();
  66. }
  67. return static::$object;
  68. }
  69. /**
  70. * @param string|null $key
  71. * @return mixed
  72. */
  73. public static function get(string $key = null)
  74. {
  75. $obj = static::getObject();
  76. if ($key === null) {
  77. return $obj;
  78. }
  79. return $obj->$key ?? null;
  80. }
  81. /**
  82. * @param string $key
  83. * @param $value
  84. * @return void
  85. */
  86. public static function set(string $key, $value): void
  87. {
  88. $obj = static::getObject();
  89. $obj->$key = $value;
  90. }
  91. /**
  92. * @param string $key
  93. * @return void
  94. */
  95. public static function delete(string $key): void
  96. {
  97. $obj = static::getObject();
  98. unset($obj->$key);
  99. }
  100. /**
  101. * @param string $key
  102. * @return bool
  103. */
  104. public static function has(string $key): bool
  105. {
  106. $obj = static::getObject();
  107. return property_exists($obj, $key);
  108. }
  109. /**
  110. * @return void
  111. */
  112. public static function destroy(): void
  113. {
  114. unset(static::$objectStorage[static::getKey()]);
  115. }
  116. }