.gitattributes000064400000000054151360551330007440 0ustar00/tests export-ignore /.github export-ignore LICENSE000064400000002054151360551330005554 0ustar00The MIT License (MIT) Copyright (c) Hyperf Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. composer.json000064400000001632151360551330007272 0ustar00{ "name": "hyperf/context", "description": "A coroutine/application context library.", "license": "MIT", "keywords": [ "php", "swoole", "hyperf", "context" ], "homepage": "https://hyperf.io", "support": { "docs": "https://hyperf.wiki", "issues": "https://github.com/hyperf/hyperf/issues", "pull-request": "https://github.com/hyperf/hyperf/pulls", "source": "https://github.com/hyperf/hyperf" }, "require": { "php": ">=8.0", "hyperf/engine": "^1.2|^2.0" }, "autoload": { "psr-4": { "Hyperf\\Context\\": "src/" } }, "autoload-dev": { "psr-4": { "HyperfTest\\Context\\": "tests/" } }, "config": { "sort-packages": true }, "extra": { "branch-alias": { "dev-master": "3.0-dev" } } } src/ApplicationContext.php000064400000001474151360551330011664 0ustar00 0) { Coroutine::getContextFor($coroutineId)[$id] = $value; } else { static::$nonCoContext[$id] = $value; } return $value; } public static function get(string $id, mixed $default = null, ?int $coroutineId = null): mixed { if (Coroutine::id() > 0) { return Coroutine::getContextFor($coroutineId)[$id] ?? $default; } return static::$nonCoContext[$id] ?? $default; } public static function has(string $id, ?int $coroutineId = null): bool { if (Coroutine::id() > 0) { return isset(Coroutine::getContextFor($coroutineId)[$id]); } return isset(static::$nonCoContext[$id]); } /** * Release the context when you are not in coroutine environment. */ public static function destroy(string $id, ?int $coroutineId = null): void { if (Coroutine::id() > 0) { unset(Coroutine::getContextFor($coroutineId)[$id]); } unset(static::$nonCoContext[$id]); } /** * Copy the context from a coroutine to current coroutine. * This method will delete the origin values in current coroutine. */ public static function copy(int $fromCoroutineId, array $keys = []): void { $from = Coroutine::getContextFor($fromCoroutineId); if ($from === null) { return; } $current = Coroutine::getContextFor(); if ($keys) { $map = array_intersect_key($from->getArrayCopy(), array_flip($keys)); } else { $map = $from->getArrayCopy(); } $current->exchangeArray($map); } /** * Retrieve the value and override it by closure. */ public static function override(string $id, Closure $closure, ?int $coroutineId = null): mixed { $value = null; if (self::has($id, $coroutineId)) { $value = self::get($id, null, $coroutineId); } $value = $closure($value); self::set($id, $value, $coroutineId); return $value; } /** * Retrieve the value and store it if not exists. */ public static function getOrSet(string $id, mixed $value, ?int $coroutineId = null): mixed { if (! self::has($id, $coroutineId)) { return self::set($id, value($value), $coroutineId); } return self::get($id, null, $coroutineId); } public static function getContainer(?int $coroutineId = null) { if (Coroutine::id() > 0) { return Coroutine::getContextFor($coroutineId); } return static::$nonCoContext; } } src/Traits/CoroutineProxy.php000064400000001763151360551330012334 0ustar00getTargetObject(); return $target->{$name}(...$arguments); } public function __get($name) { $target = $this->getTargetObject(); return $target->{$name}; } public function __set($name, $value) { $target = $this->getTargetObject(); return $target->{$name} = $value; } protected function getTargetObject() { if (! isset($this->proxyKey)) { throw new RuntimeException(sprintf('Missing $proxyKey property in %s.', $this::class)); } return Context::get($this->proxyKey); } }