diff --git a/Annotation/Inject.php b/Annotation/Inject.php index 047dbe86..78e22372 100644 --- a/Annotation/Inject.php +++ b/Annotation/Inject.php @@ -22,87 +22,86 @@ use Snowflake\Snowflake; /** * Inject constructor. * @param string $value - * @param bool $withContext * @param array $args */ - public function __construct(private string $value, public bool $withContext = false, private array $args = []) - { - } + public function __construct(private string $value, private array $args = []) + { + } - /** - * @param mixed $class - * @param mixed|null $method - * @return bool - * @throws ReflectionException - * @throws Exception - */ - public function execute(mixed $class, mixed $method = null): bool - { - if (!($method = $this->getProperty($class, $method))) { - return false; - } - /** @var ReflectionProperty $class */ - $injectValue = $this->parseInjectValue(); - if ($method->isPrivate() || $method->isProtected()) { - $this->setter($class, $method, $injectValue); - } else { - $class->{$method->getName()} = $injectValue; - } - return true; - } + /** + * @param mixed $class + * @param mixed|null $method + * @return bool + * @throws ReflectionException + * @throws Exception + */ + public function execute(mixed $class, mixed $method = null): bool + { + if (!($method = $this->getProperty($class, $method))) { + return false; + } + /** @var ReflectionProperty $class */ + $injectValue = $this->parseInjectValue(); + if ($method->isPrivate() || $method->isProtected()) { + $this->setter($class, $method, $injectValue); + } else { + $class->{$method->getName()} = $injectValue; + } + return true; + } - /** - * @param $class - * @param $method - * @param $injectValue - */ - private function setter($class, $method, $injectValue) - { - $method = 'set' . ucfirst(Str::convertUnderline($method->getName())); - if (!method_exists($class, $method)) { - return; - } - $class->$method($injectValue); - } + /** + * @param $class + * @param $method + * @param $injectValue + */ + private function setter($class, $method, $injectValue) + { + $method = 'set' . ucfirst(Str::convertUnderline($method->getName())); + if (!method_exists($class, $method)) { + return; + } + $class->$method($injectValue); + } - /** - * @param $class - * @param $method - * @return ReflectionProperty|bool - */ - private function getProperty($class, $method): ReflectionProperty|bool - { - if ($method instanceof ReflectionProperty && !$method->isStatic()) { - return $method; - } - if (is_object($class)) $class = $class::class; - $method = Snowflake::getDi()->getClassReflectionProperty($class, $method); - if (!$method || $method->isStatic()) { - return false; - } - return $method; - } + /** + * @param $class + * @param $method + * @return ReflectionProperty|bool + */ + private function getProperty($class, $method): ReflectionProperty|bool + { + if ($method instanceof ReflectionProperty && !$method->isStatic()) { + return $method; + } + if (is_object($class)) $class = $class::class; + $method = Snowflake::getDi()->getClassReflectionProperty($class, $method); + if (!$method || $method->isStatic()) { + return false; + } + return $method; + } - /** - * @return mixed - * @throws Exception - */ - private function parseInjectValue(): mixed - { - if ($this->withContext) { - return Context::getContext($this->value); - } - if (class_exists($this->value)) { - return Snowflake::getDi()->get($this->value, $this->args); - } else if (Snowflake::app()->has($this->value)) { - return Snowflake::app()->get($this->value); - } else { - return $this->value; - } - } + /** + * @return mixed + * @throws Exception + */ + private function parseInjectValue(): mixed + { + if (Context::hasContext($this->value)) { + return Context::getContext($this->value); + } + if (class_exists($this->value)) { + return Snowflake::getDi()->get($this->value, $this->args); + } else if (Snowflake::app()->has($this->value)) { + return Snowflake::app()->get($this->value); + } else { + return $this->value; + } + } } diff --git a/HttpServer/Controller.php b/HttpServer/Controller.php index e26732de..0139d220 100644 --- a/HttpServer/Controller.php +++ b/HttpServer/Controller.php @@ -26,7 +26,7 @@ class Controller * * @var Request|null */ - #[Inject(value: 'request')] + #[Inject(value: Request::class)] public ?Request $request = null; @@ -49,7 +49,7 @@ class Controller * * @var Response|null */ - #[Inject(value: 'response')] + #[Inject(value: Response::class)] public ?Response $response = null; diff --git a/HttpServer/Http/Request.php b/HttpServer/Http/Request.php index 6def065b..b9671b8e 100644 --- a/HttpServer/Http/Request.php +++ b/HttpServer/Http/Request.php @@ -440,7 +440,7 @@ class Request extends HttpService $httpRequest->params->setGets($request->get); $httpRequest->params->setRawContent($request->rawContent(), $httpRequest); - return Context::setContext('request', $httpRequest); + return Context::setContext(Request::class, $httpRequest); } diff --git a/Rpc/Service.php b/Rpc/Service.php index c906d032..df3ceb4b 100644 --- a/Rpc/Service.php +++ b/Rpc/Service.php @@ -147,7 +147,7 @@ class Service extends \Server\Abstracts\Server $sRequest->headers->setRequestUri('rpc/p' . $server_port . '/' . ltrim($cmd, '/')); $sRequest->headers->setRequestMethod(Request::HTTP_CMD); - return Context::setContext('request', $sRequest); + return Context::setContext(Request::class, $sRequest); } diff --git a/function.php b/function.php index cd34d731..164c449a 100644 --- a/function.php +++ b/function.php @@ -4,6 +4,7 @@ defined('APP_PATH') or define('APP_PATH', realpath(__DIR__ . '/../../')); use Annotation\Annotation; +use HttpServer\Http\Context; use HttpServer\Http\HttpParams; use HttpServer\Http\Request; use HttpServer\Http\Response; @@ -483,7 +484,7 @@ if (!function_exists('request')) { */ function request(): Request { - return Snowflake::app()->get('request'); + return Context::getContext(Request::class); } }