-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Coroutine added context extend
- Loading branch information
Showing
5 changed files
with
150 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php declare(strict_types=1); | ||
/** | ||
* Copyright © 2024 cclilshy | ||
* Email: [email protected] | ||
* | ||
* This software is licensed under the MIT License. | ||
* For full license details, please visit: https://opensource.org/licenses/MIT | ||
* | ||
* By using this software, you agree to the terms of the license. | ||
* Contributions, suggestions, and feedback are always welcome! | ||
*/ | ||
|
||
namespace Ripple\Coroutine; | ||
|
||
use Revolt\EventLoop\Suspension; | ||
use Ripple\Types\Undefined; | ||
|
||
use function Co\getSuspension; | ||
use function spl_object_hash; | ||
use function array_merge; | ||
use function is_array; | ||
|
||
/** | ||
* | ||
*/ | ||
class Context | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected static array $context = []; | ||
|
||
/** | ||
* @param array|string $key | ||
* @param mixed $value | ||
* | ||
* @return void | ||
*/ | ||
public static function define(array|string $key, mixed $value = null): void | ||
{ | ||
$hash = Context::getHash(); | ||
if (is_array($key)) { | ||
Context::$context[$hash] = array_merge(Context::$context[$hash] ?? [], $key); | ||
} | ||
|
||
Context::$context[$hash][$key] = $value; | ||
} | ||
|
||
/** | ||
* @param string|null $key | ||
* | ||
* @return mixed | ||
*/ | ||
public static function get(string|null $key = null): mixed | ||
{ | ||
$hash = Context::getHash(); | ||
if (!$key) { | ||
return Context::$context[$hash] ?? new Undefined(); | ||
} | ||
return Context::$context[$hash][$key] ?? new Undefined(); | ||
} | ||
|
||
/** | ||
* @param string $key | ||
* | ||
* @return void | ||
*/ | ||
public static function remove(string $key): void | ||
{ | ||
$hash = Context::getHash(); | ||
unset(Context::$context[$hash][$key]); | ||
} | ||
|
||
/** | ||
* @param \Revolt\EventLoop\Suspension $targetSuspension | ||
* | ||
* @return void | ||
*/ | ||
public static function extend(Suspension $targetSuspension): void | ||
{ | ||
$currentSuspension = getSuspension(); | ||
if ($currentSuspension === $targetSuspension) { | ||
return; | ||
} | ||
|
||
$currentHash = Context::getHash($currentSuspension); | ||
$targetHash = Context::getHash($targetSuspension); | ||
Context::$context[$currentHash] = (Context::$context[$targetHash] ?? []); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public static function clear(): void | ||
{ | ||
unset(Context::$context[Context::getHash()]); | ||
} | ||
|
||
/** | ||
* @param \Revolt\EventLoop\Suspension|null $suspension | ||
* | ||
* @return string | ||
*/ | ||
public static function getHash(Suspension|null $suspension = null): string | ||
{ | ||
if (!$suspension) { | ||
$suspension = getSuspension(); | ||
} | ||
return spl_object_hash($suspension); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php declare(strict_types=1); | ||
/** | ||
* Copyright © 2024 cclilshy | ||
* Email: [email protected] | ||
* | ||
* This software is licensed under the MIT License. | ||
* For full license details, please visit: https://opensource.org/licenses/MIT | ||
* | ||
* By using this software, you agree to the terms of the license. | ||
* Contributions, suggestions, and feedback are always welcome! | ||
*/ | ||
|
||
namespace Ripple\Types; | ||
|
||
use stdClass; | ||
|
||
class Undefined extends stdClass | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters