This commit is contained in:
2021-08-01 15:25:59 +08:00
parent c2aa3ae44a
commit 0eea1d7963
5 changed files with 77 additions and 77 deletions
+71 -72
View File
@@ -22,87 +22,86 @@ use Snowflake\Snowflake;
/** /**
* Inject constructor. * Inject constructor.
* @param string $value * @param string $value
* @param bool $withContext
* @param array $args * @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 $class
* @param mixed|null $method * @param mixed|null $method
* @return bool * @return bool
* @throws ReflectionException * @throws ReflectionException
* @throws Exception * @throws Exception
*/ */
public function execute(mixed $class, mixed $method = null): bool public function execute(mixed $class, mixed $method = null): bool
{ {
if (!($method = $this->getProperty($class, $method))) { if (!($method = $this->getProperty($class, $method))) {
return false; return false;
} }
/** @var ReflectionProperty $class */ /** @var ReflectionProperty $class */
$injectValue = $this->parseInjectValue(); $injectValue = $this->parseInjectValue();
if ($method->isPrivate() || $method->isProtected()) { if ($method->isPrivate() || $method->isProtected()) {
$this->setter($class, $method, $injectValue); $this->setter($class, $method, $injectValue);
} else { } else {
$class->{$method->getName()} = $injectValue; $class->{$method->getName()} = $injectValue;
} }
return true; return true;
} }
/** /**
* @param $class * @param $class
* @param $method * @param $method
* @param $injectValue * @param $injectValue
*/ */
private function setter($class, $method, $injectValue) private function setter($class, $method, $injectValue)
{ {
$method = 'set' . ucfirst(Str::convertUnderline($method->getName())); $method = 'set' . ucfirst(Str::convertUnderline($method->getName()));
if (!method_exists($class, $method)) { if (!method_exists($class, $method)) {
return; return;
} }
$class->$method($injectValue); $class->$method($injectValue);
} }
/** /**
* @param $class * @param $class
* @param $method * @param $method
* @return ReflectionProperty|bool * @return ReflectionProperty|bool
*/ */
private function getProperty($class, $method): ReflectionProperty|bool private function getProperty($class, $method): ReflectionProperty|bool
{ {
if ($method instanceof ReflectionProperty && !$method->isStatic()) { if ($method instanceof ReflectionProperty && !$method->isStatic()) {
return $method; return $method;
} }
if (is_object($class)) $class = $class::class; if (is_object($class)) $class = $class::class;
$method = Snowflake::getDi()->getClassReflectionProperty($class, $method); $method = Snowflake::getDi()->getClassReflectionProperty($class, $method);
if (!$method || $method->isStatic()) { if (!$method || $method->isStatic()) {
return false; return false;
} }
return $method; return $method;
} }
/** /**
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
private function parseInjectValue(): mixed private function parseInjectValue(): mixed
{ {
if ($this->withContext) { if (Context::hasContext($this->value)) {
return Context::getContext($this->value); return Context::getContext($this->value);
} }
if (class_exists($this->value)) { if (class_exists($this->value)) {
return Snowflake::getDi()->get($this->value, $this->args); return Snowflake::getDi()->get($this->value, $this->args);
} else if (Snowflake::app()->has($this->value)) { } else if (Snowflake::app()->has($this->value)) {
return Snowflake::app()->get($this->value); return Snowflake::app()->get($this->value);
} else { } else {
return $this->value; return $this->value;
} }
} }
} }
+2 -2
View File
@@ -26,7 +26,7 @@ class Controller
* *
* @var Request|null * @var Request|null
*/ */
#[Inject(value: 'request')] #[Inject(value: Request::class)]
public ?Request $request = null; public ?Request $request = null;
@@ -49,7 +49,7 @@ class Controller
* *
* @var Response|null * @var Response|null
*/ */
#[Inject(value: 'response')] #[Inject(value: Response::class)]
public ?Response $response = null; public ?Response $response = null;
+1 -1
View File
@@ -440,7 +440,7 @@ class Request extends HttpService
$httpRequest->params->setGets($request->get); $httpRequest->params->setGets($request->get);
$httpRequest->params->setRawContent($request->rawContent(), $httpRequest); $httpRequest->params->setRawContent($request->rawContent(), $httpRequest);
return Context::setContext('request', $httpRequest); return Context::setContext(Request::class, $httpRequest);
} }
+1 -1
View File
@@ -147,7 +147,7 @@ class Service extends \Server\Abstracts\Server
$sRequest->headers->setRequestUri('rpc/p' . $server_port . '/' . ltrim($cmd, '/')); $sRequest->headers->setRequestUri('rpc/p' . $server_port . '/' . ltrim($cmd, '/'));
$sRequest->headers->setRequestMethod(Request::HTTP_CMD); $sRequest->headers->setRequestMethod(Request::HTTP_CMD);
return Context::setContext('request', $sRequest); return Context::setContext(Request::class, $sRequest);
} }
+2 -1
View File
@@ -4,6 +4,7 @@ defined('APP_PATH') or define('APP_PATH', realpath(__DIR__ . '/../../'));
use Annotation\Annotation; use Annotation\Annotation;
use HttpServer\Http\Context;
use HttpServer\Http\HttpParams; use HttpServer\Http\HttpParams;
use HttpServer\Http\Request; use HttpServer\Http\Request;
use HttpServer\Http\Response; use HttpServer\Http\Response;
@@ -483,7 +484,7 @@ if (!function_exists('request')) {
*/ */
function request(): Request function request(): Request
{ {
return Snowflake::app()->get('request'); return Context::getContext(Request::class);
} }
} }