mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-11-29 18:27:44 +08:00
Split hyperf/context
from hyperf/utils
. (#4529)
This commit is contained in:
parent
68324564f4
commit
ce0cb38294
@ -4,6 +4,7 @@
|
||||
|
||||
- [#4514](https://github.com/hyperf/hyperf/pull/4514) Improved some performance by using lowercase headers.
|
||||
- [#4521](https://github.com/hyperf/hyperf/pull/4521) Try to connect to another one when connected redis sentinel failed.
|
||||
- [#4529](https://github.com/hyperf/hyperf/pull/4529) Split `hyperf/context` from `hyperf/utils`.
|
||||
|
||||
# v2.2.25 - 2022-01-30
|
||||
|
||||
|
@ -99,6 +99,7 @@
|
||||
"hyperf/config-zookeeper": "*",
|
||||
"hyperf/constants": "*",
|
||||
"hyperf/consul": "*",
|
||||
"hyperf/context": "*",
|
||||
"hyperf/contract": "*",
|
||||
"hyperf/crontab": "*",
|
||||
"hyperf/dag": "*",
|
||||
@ -200,6 +201,7 @@
|
||||
"Hyperf\\Config\\": "src/config/src/",
|
||||
"Hyperf\\Constants\\": "src/constants/src/",
|
||||
"Hyperf\\Consul\\": "src/consul/src/",
|
||||
"Hyperf\\Context\\": "src/context/src/",
|
||||
"Hyperf\\Contract\\": "src/contract/src/",
|
||||
"Hyperf\\Crontab\\": "src/crontab/src/",
|
||||
"Hyperf\\DB\\": "src/db/src/",
|
||||
@ -293,6 +295,7 @@
|
||||
"HyperfTest\\Config\\": "src/config/tests/",
|
||||
"HyperfTest\\Constants\\": "src/constants/tests/",
|
||||
"HyperfTest\\Consul\\": "src/consul/tests/",
|
||||
"HyperfTest\\Context\\": "src/context/tests/",
|
||||
"HyperfTest\\Crontab\\": "src/crontab/tests/",
|
||||
"HyperfTest\\DB\\": "src/db/tests/",
|
||||
"HyperfTest\\Dag\\": "src/dag/tests/",
|
||||
|
@ -20,6 +20,7 @@
|
||||
<directory suffix=".php">./src/config-nacos/src</directory>
|
||||
<directory suffix=".php">./src/constants/src</directory>
|
||||
<directory suffix=".php">./src/consul/src</directory>
|
||||
<directory suffix=".php">./src/context/src</directory>
|
||||
<directory suffix=".php">./src/crontab/src</directory>
|
||||
<directory suffix=".php">./src/dag/src</directory>
|
||||
<directory suffix=".php">./src/database/src</directory>
|
||||
@ -74,6 +75,7 @@
|
||||
<directory suffix="Test.php">./src/config-zookeeper/tests</directory>
|
||||
<directory suffix="Test.php">./src/constants/tests</directory>
|
||||
<directory suffix="Test.php">./src/consul/tests</directory>
|
||||
<directory suffix="Test.php">./src/context/tests</directory>
|
||||
<directory suffix="Test.php">./src/crontab/tests</directory>
|
||||
<directory suffix="Test.php">./src/dag/tests</directory>
|
||||
<directory suffix="Test.php">./src/database/tests</directory>
|
||||
|
2
src/context/.gitattributes
vendored
Normal file
2
src/context/.gitattributes
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
/tests export-ignore
|
||||
/.github export-ignore
|
25
src/context/.github/workflows/release.yml
vendored
Normal file
25
src/context/.github/workflows/release.yml
vendored
Normal file
@ -0,0 +1,25 @@
|
||||
on:
|
||||
push:
|
||||
# Sequence of patterns matched against refs/tags
|
||||
tags:
|
||||
- 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10
|
||||
|
||||
name: Release
|
||||
|
||||
jobs:
|
||||
release:
|
||||
name: Release
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v2
|
||||
- name: Create Release
|
||||
id: create_release
|
||||
uses: actions/create-release@v1
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
with:
|
||||
tag_name: ${{ github.ref }}
|
||||
release_name: Release ${{ github.ref }}
|
||||
draft: false
|
||||
prerelease: false
|
21
src/context/LICENSE
Normal file
21
src/context/LICENSE
Normal file
@ -0,0 +1,21 @@
|
||||
The 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.
|
40
src/context/composer.json
Normal file
40
src/context/composer.json
Normal file
@ -0,0 +1,40 @@
|
||||
{
|
||||
"name": "hyperf/context",
|
||||
"description": "A coroutine 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": ">=7.2",
|
||||
"hyperf/engine": "^1.1"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Hyperf\\Context\\": "src/"
|
||||
}
|
||||
},
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"HyperfTest\\Context\\": "tests/"
|
||||
}
|
||||
},
|
||||
"config": {
|
||||
"sort-packages": true
|
||||
},
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "2.2-dev"
|
||||
}
|
||||
}
|
||||
}
|
112
src/context/src/Context.php
Normal file
112
src/context/src/Context.php
Normal file
@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace Hyperf\Context;
|
||||
|
||||
use Hyperf\Engine\Coroutine;
|
||||
|
||||
class Context
|
||||
{
|
||||
protected static $nonCoContext = [];
|
||||
|
||||
public static function set(string $id, $value)
|
||||
{
|
||||
if (Coroutine::id() > 0) {
|
||||
Coroutine::getContextFor()[$id] = $value;
|
||||
} else {
|
||||
static::$nonCoContext[$id] = $value;
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
public static function get(string $id, $default = null, $coroutineId = null)
|
||||
{
|
||||
if (Coroutine::id() > 0) {
|
||||
return Coroutine::getContextFor($coroutineId)[$id] ?? $default;
|
||||
}
|
||||
|
||||
return static::$nonCoContext[$id] ?? $default;
|
||||
}
|
||||
|
||||
public static function has(string $id, $coroutineId = null)
|
||||
{
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
$value = null;
|
||||
if (self::has($id)) {
|
||||
$value = self::get($id);
|
||||
}
|
||||
$value = $closure($value);
|
||||
self::set($id, $value);
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the value and store it if not exists.
|
||||
* @param mixed $value
|
||||
*/
|
||||
public static function getOrSet(string $id, $value)
|
||||
{
|
||||
if (! self::has($id)) {
|
||||
return self::set($id, value($value));
|
||||
}
|
||||
return self::get($id);
|
||||
}
|
||||
|
||||
public static function getContainer()
|
||||
{
|
||||
if (Coroutine::id() > 0) {
|
||||
return Coroutine::getContextFor();
|
||||
}
|
||||
|
||||
return static::$nonCoContext;
|
||||
}
|
||||
}
|
111
src/context/tests/ContextTest.php
Normal file
111
src/context/tests/ContextTest.php
Normal file
@ -0,0 +1,111 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* This file is part of Hyperf.
|
||||
*
|
||||
* @link https://www.hyperf.io
|
||||
* @document https://hyperf.wiki
|
||||
* @contact group@hyperf.io
|
||||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
|
||||
*/
|
||||
namespace HyperfTest\Context;
|
||||
|
||||
use Hyperf\Context\Context;
|
||||
use Hyperf\Utils\Coroutine;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class ContextTest extends TestCase
|
||||
{
|
||||
public function testOverride()
|
||||
{
|
||||
Context::set('override.id', 1);
|
||||
|
||||
$this->assertSame(2, Context::override('override.id', function ($id) {
|
||||
return $id + 1;
|
||||
}));
|
||||
|
||||
$this->assertSame(2, Context::get('override.id'));
|
||||
}
|
||||
|
||||
public function testGetOrSet()
|
||||
{
|
||||
Context::set('test.store.id', null);
|
||||
$this->assertSame(1, Context::getOrSet('test.store.id', function () {
|
||||
return 1;
|
||||
}));
|
||||
$this->assertSame(1, Context::getOrSet('test.store.id', function () {
|
||||
return 2;
|
||||
}));
|
||||
|
||||
Context::set('test.store.id', null);
|
||||
$this->assertSame(1, Context::getOrSet('test.store.id', 1));
|
||||
}
|
||||
|
||||
public function testCopy()
|
||||
{
|
||||
Context::set('test.store.id', $uid = uniqid());
|
||||
$id = Coroutine::id();
|
||||
parallel([function () use ($id, $uid) {
|
||||
Context::copy($id, ['test.store.id']);
|
||||
$this->assertSame($uid, Context::get('test.store.id'));
|
||||
}]);
|
||||
}
|
||||
|
||||
public function testCopyAfterSet()
|
||||
{
|
||||
Context::set('test.store.id', $uid = uniqid());
|
||||
$id = Coroutine::id();
|
||||
parallel([function () use ($id, $uid) {
|
||||
Context::set('test.store.name', 'Hyperf');
|
||||
Context::copy($id, ['test.store.id']);
|
||||
$this->assertSame($uid, Context::get('test.store.id'));
|
||||
|
||||
// TODO: Context::copy will delete origin values.
|
||||
$this->assertNull(Context::get('test.store.name'));
|
||||
}]);
|
||||
}
|
||||
|
||||
public function testContextChangeAfterCopy()
|
||||
{
|
||||
$obj = new \stdClass();
|
||||
$obj->id = $uid = uniqid();
|
||||
|
||||
Context::set('test.store.id', $obj);
|
||||
Context::set('test.store.useless.id', 1);
|
||||
$id = Coroutine::id();
|
||||
$tid = uniqid();
|
||||
parallel([function () use ($id, $uid, $tid) {
|
||||
Context::copy($id, ['test.store.id']);
|
||||
$obj = Context::get('test.store.id');
|
||||
$this->assertSame($uid, $obj->id);
|
||||
$obj->id = $tid;
|
||||
$this->assertFalse(Context::has('test.store.useless.id'));
|
||||
}]);
|
||||
|
||||
$this->assertSame($tid, Context::get('test.store.id')->id);
|
||||
}
|
||||
|
||||
public function testContextFromNull()
|
||||
{
|
||||
$res = Context::get('id', $default = 'Hello World!', -1);
|
||||
$this->assertSame($default, $res);
|
||||
|
||||
$res = Context::get('id', null, -1);
|
||||
$this->assertSame(null, $res);
|
||||
|
||||
$this->assertFalse(Context::has('id', -1));
|
||||
|
||||
Context::copy(-1);
|
||||
|
||||
parallel([function () {
|
||||
Context::set('id', $id = uniqid());
|
||||
Context::copy(-1, ['id']);
|
||||
$this->assertSame($id, Context::get('id'));
|
||||
}]);
|
||||
}
|
||||
}
|
@ -18,6 +18,7 @@
|
||||
"require": {
|
||||
"php": ">=7.2",
|
||||
"doctrine/inflector": "^2.0",
|
||||
"hyperf/context": "~2.2.0",
|
||||
"hyperf/contract": "~2.2.0",
|
||||
"hyperf/engine": "^1.1",
|
||||
"hyperf/macroable": "~2.2.0"
|
||||
|
@ -13,6 +13,9 @@ namespace Hyperf\Utils;
|
||||
|
||||
use Hyperf\Engine\Coroutine as Co;
|
||||
|
||||
/**
|
||||
* @deprecated v3.0 please use `hyperf/context` instead.
|
||||
*/
|
||||
class Context
|
||||
{
|
||||
protected static $nonCoContext = [];
|
||||
|
Loading…
Reference in New Issue
Block a user