Files

49 lines
795 B
PHP
Raw Permalink Normal View History

2022-12-12 17:31:11 +08:00
<?php
2023-04-16 01:45:33 +08:00
declare(strict_types=1);
2022-12-12 17:31:11 +08:00
namespace Kiri\Di;
2023-04-03 11:08:11 +08:00
use Kiri\Di\Context\AsyncContext;
use Kiri\Di\Context\ContextInterface;
use Kiri\Di\Context\CoroutineContext;
2022-12-12 17:31:11 +08:00
use Swoole\Coroutine;
/**
* Class Context
* @package Yoc\http
2023-04-03 11:08:11 +08:00
* @mixin ContextInterface
2022-12-12 17:31:11 +08:00
*/
class Context
{
2023-02-06 12:05:44 +08:00
2023-04-03 11:08:11 +08:00
2023-02-06 12:05:44 +08:00
/**
2023-04-03 11:08:11 +08:00
* @param string $name
* @param array $arguments
2022-12-12 17:31:11 +08:00
* @return mixed
*/
2023-04-03 11:08:11 +08:00
public static function __callStatic(string $name, array $arguments): mixed
2022-12-12 17:31:11 +08:00
{
2023-04-03 11:08:11 +08:00
// TODO: Implement __callStatic() method.
if (static::inCoroutine()) {
return call_user_func([CoroutineContext::class, $name], ...$arguments);
2022-12-12 17:31:11 +08:00
} else {
2023-04-03 11:08:11 +08:00
return call_user_func([AsyncContext::class, $name], ...$arguments);
2022-12-12 17:31:11 +08:00
}
}
2023-04-03 11:08:11 +08:00
2022-12-12 17:31:11 +08:00
/**
* @return bool
*/
public static function inCoroutine(): bool
{
2023-04-24 21:47:52 +08:00
return Coroutine::getCid() > -1;
2022-12-12 17:31:11 +08:00
}
2023-04-03 11:08:11 +08:00
2022-12-12 17:31:11 +08:00
}