modify
This commit is contained in:
+24
-9
@@ -5,6 +5,7 @@ namespace Annotation;
|
|||||||
|
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use HttpServer\Http\Context;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
use ReflectionProperty;
|
use ReflectionProperty;
|
||||||
use Snowflake\Core\Str;
|
use Snowflake\Core\Str;
|
||||||
@@ -23,7 +24,7 @@ use Snowflake\Snowflake;
|
|||||||
* @param string $className
|
* @param string $className
|
||||||
* @param array $args
|
* @param array $args
|
||||||
*/
|
*/
|
||||||
public function __construct(private string $className, private array $args = [])
|
public function __construct(private string $className, public bool $withContext = false, private array $args = [])
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,17 +38,13 @@ use Snowflake\Snowflake;
|
|||||||
*/
|
*/
|
||||||
public function execute(mixed $class, mixed $method = null): bool
|
public function execute(mixed $class, mixed $method = null): bool
|
||||||
{
|
{
|
||||||
$injectValue = $this->parseInjectValue();
|
|
||||||
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();
|
||||||
if ($method->isPrivate() || $method->isProtected()) {
|
if ($method->isPrivate() || $method->isProtected()) {
|
||||||
$method = 'set' . ucfirst(Str::convertUnderline($method->getName()));
|
$this->setter($class, $method, $injectValue);
|
||||||
if (!method_exists($class, $method)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
$class->$method($injectValue);
|
|
||||||
} else {
|
} else {
|
||||||
$class->{$method->getName()} = $injectValue;
|
$class->{$method->getName()} = $injectValue;
|
||||||
}
|
}
|
||||||
@@ -55,6 +52,21 @@ use Snowflake\Snowflake;
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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 $class
|
||||||
* @param $method
|
* @param $method
|
||||||
@@ -62,12 +74,12 @@ use Snowflake\Snowflake;
|
|||||||
*/
|
*/
|
||||||
private function getProperty($class, $method): ReflectionProperty|bool
|
private function getProperty($class, $method): ReflectionProperty|bool
|
||||||
{
|
{
|
||||||
if ($method instanceof ReflectionProperty) {
|
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()->getClassProperty($class, $method);
|
$method = Snowflake::getDi()->getClassProperty($class, $method);
|
||||||
if (!$method) {
|
if (!$method || $method->isStatic()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return $method;
|
return $method;
|
||||||
@@ -80,6 +92,9 @@ use Snowflake\Snowflake;
|
|||||||
*/
|
*/
|
||||||
private function parseInjectValue(): mixed
|
private function parseInjectValue(): mixed
|
||||||
{
|
{
|
||||||
|
if ($this->withContext) {
|
||||||
|
return Context::getContext($this->className);
|
||||||
|
}
|
||||||
if (class_exists($this->className)) {
|
if (class_exists($this->className)) {
|
||||||
return Snowflake::getDi()->get($this->className, $this->args);
|
return Snowflake::getDi()->get($this->className, $this->args);
|
||||||
} else if (Snowflake::app()->has($this->className)) {
|
} else if (Snowflake::app()->has($this->className)) {
|
||||||
|
|||||||
+2
-16
@@ -38,10 +38,6 @@ class Loader extends BaseObject
|
|||||||
|
|
||||||
private static array $_methods = [];
|
private static array $_methods = [];
|
||||||
|
|
||||||
|
|
||||||
private array $_annotationMaps = [];
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
@@ -339,14 +335,12 @@ class Loader extends BaseObject
|
|||||||
if (empty($classes)) {
|
if (empty($classes)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$annotation = Snowflake::getAnnotation();
|
|
||||||
|
|
||||||
foreach ($classes as $className) {
|
foreach ($classes as $className) {
|
||||||
if (!isset(static::$_methods[$className])) {
|
if (!isset(static::$_methods[$className])) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
foreach (static::$_methods[$className] as $name => $attribute) {
|
foreach (static::$_methods[$className] as $name => $attribute) {
|
||||||
$this->methods($attribute, $annotation, $className, $name);
|
$this->methods($attribute, $className, $name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -358,20 +352,12 @@ class Loader extends BaseObject
|
|||||||
* @param $className
|
* @param $className
|
||||||
* @param $name
|
* @param $name
|
||||||
*/
|
*/
|
||||||
private function methods($attribute, $annotation, $className, $name)
|
private function methods($attribute, $className, $name)
|
||||||
{
|
{
|
||||||
$handler = static::$_classes[$className]['handler'];
|
$handler = static::$_classes[$className]['handler'];
|
||||||
foreach ($attribute as $value) {
|
foreach ($attribute as $value) {
|
||||||
if ($value instanceof Relation) {
|
|
||||||
$annotation->addRelate($className, $value->name, $name);
|
|
||||||
} else if ($value instanceof Get) {
|
|
||||||
$annotation->addGets($className, $value->name, $name);
|
|
||||||
} else if ($value instanceof Set) {
|
|
||||||
$annotation->addSets($className, $value->name, $name);
|
|
||||||
} else {
|
|
||||||
$value->execute($handler, $name);
|
$value->execute($handler, $name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+14
-64
@@ -4,11 +4,14 @@ declare(strict_types=1);
|
|||||||
namespace HttpServer;
|
namespace HttpServer;
|
||||||
|
|
||||||
|
|
||||||
|
use Annotation\Inject;
|
||||||
use Exception;
|
use Exception;
|
||||||
use HttpServer\Abstracts\HttpService;
|
use HttpServer\Abstracts\HttpService;
|
||||||
use HttpServer\Http\HttpHeaders;
|
use HttpServer\Http\HttpHeaders;
|
||||||
use HttpServer\Http\HttpParams;
|
use HttpServer\Http\HttpParams;
|
||||||
use HttpServer\Http\Request;
|
use HttpServer\Http\Request;
|
||||||
|
use HttpServer\Http\Response;
|
||||||
|
use Snowflake\Abstracts\Input;
|
||||||
use Snowflake\Abstracts\TraitApplication;
|
use Snowflake\Abstracts\TraitApplication;
|
||||||
use Snowflake\Snowflake;
|
use Snowflake\Snowflake;
|
||||||
|
|
||||||
@@ -17,82 +20,29 @@ use Snowflake\Snowflake;
|
|||||||
* @package Snowflake\Snowflake\Web
|
* @package Snowflake\Snowflake\Web
|
||||||
* @property-read HttpParams $input
|
* @property-read HttpParams $input
|
||||||
* @property-read HttpHeaders $headers
|
* @property-read HttpHeaders $headers
|
||||||
* @property-read Request $request
|
|
||||||
*/
|
*/
|
||||||
class Controller extends HttpService
|
class Controller
|
||||||
{
|
{
|
||||||
|
|
||||||
use TraitApplication;
|
use TraitApplication;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Controller constructor.
|
* inject request
|
||||||
* @param array $config
|
*
|
||||||
* @throws Exception
|
* @var \HttpServer\Http\Request
|
||||||
*/
|
*/
|
||||||
public function __construct($config = [])
|
#[Inject(className: 'request', withContext: true)]
|
||||||
{
|
protected Request $request;
|
||||||
parent::__construct($config);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws Exception
|
* inject response
|
||||||
|
*
|
||||||
|
* @var \HttpServer\Http\Response
|
||||||
*/
|
*/
|
||||||
public function init()
|
#[Inject('response')]
|
||||||
{
|
protected Response $response;
|
||||||
$annotation = Snowflake::getAnnotation();
|
|
||||||
$annotation->injectProperty($this);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return ?HttpParams
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function getInput(): ?HttpParams
|
|
||||||
{
|
|
||||||
return \request()->params;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return ?HttpHeaders
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function getHeaders(): ?HttpHeaders
|
|
||||||
{
|
|
||||||
return \request()->headers;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return Request|null
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function getRequest(): ?Request
|
|
||||||
{
|
|
||||||
return \request();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $name
|
|
||||||
* @return mixed
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function __get($name): mixed
|
|
||||||
{
|
|
||||||
// TODO: Change the autogenerated stub
|
|
||||||
if (property_exists($this, $name)) {
|
|
||||||
return $this->$name;
|
|
||||||
}
|
|
||||||
|
|
||||||
$method = 'get' . ucfirst($name);
|
|
||||||
if (method_exists($this, $method)) {
|
|
||||||
return $this->{$method}();
|
|
||||||
}
|
|
||||||
|
|
||||||
return Snowflake::app()->get($name);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -452,7 +452,7 @@ class Request extends HttpService
|
|||||||
* @param \Swoole\Http\Request $request
|
* @param \Swoole\Http\Request $request
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public static function create(\Swoole\Http\Request $request, \Swoole\Http\Response $response): Request
|
public static function create(\Swoole\Http\Request $request): Request
|
||||||
{
|
{
|
||||||
/** @var Request $sRequest */
|
/** @var Request $sRequest */
|
||||||
$sRequest = Context::setContext('request', new Request());
|
$sRequest = Context::setContext('request', new Request());
|
||||||
@@ -469,7 +469,9 @@ class Request extends HttpService
|
|||||||
|
|
||||||
$sRequest->parseUri();
|
$sRequest->parseUri();
|
||||||
|
|
||||||
HResponse::create($response);
|
Context::setContext('input', $sRequest->params);
|
||||||
|
Context::setContext('header', $sRequest->headers);
|
||||||
|
|
||||||
return $sRequest;
|
return $sRequest;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -32,8 +32,6 @@ class HTTPServerListener extends Abstracts\Server
|
|||||||
|
|
||||||
private Router $router;
|
private Router $router;
|
||||||
|
|
||||||
private ApplicationStore $store;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* HTTPServerListener constructor.
|
* HTTPServerListener constructor.
|
||||||
@@ -42,7 +40,6 @@ class HTTPServerListener extends Abstracts\Server
|
|||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->router = Snowflake::getApp('router');
|
$this->router = Snowflake::getApp('router');
|
||||||
$this->store = ApplicationStore::getStore();
|
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,16 +102,19 @@ class HTTPServerListener extends Abstracts\Server
|
|||||||
*/
|
*/
|
||||||
public function onRequest(Request $request, Response $response)
|
public function onRequest(Request $request, Response $response)
|
||||||
{
|
{
|
||||||
|
[$sRequest, $sResponse] = [HRequest::create($request), HResponse::create($response)];
|
||||||
try {
|
try {
|
||||||
if (HRequest::create($request, $response)->is('favicon.ico')) {
|
if ($sRequest->is('favicon.ico')) {
|
||||||
|
$this->router->status404();
|
||||||
|
} else if (!($node = $this->router->find_path($sRequest))) {
|
||||||
$this->router->status404();
|
$this->router->status404();
|
||||||
} else {
|
} else {
|
||||||
$this->router->dispatch();
|
$sResponse->send($node->dispatch(), 200);
|
||||||
}
|
}
|
||||||
} catch (Error | Throwable $exception) {
|
} catch (Error | Throwable $exception) {
|
||||||
$response->setHeader('Content-Type', 'text/html; charset=utf-8');
|
$sResponse->addHeader('Content-Type', 'text/html; charset=utf-8');
|
||||||
$response->status($exception->getCode() == 0 ? 500 : $exception->getCode());
|
$sResponse->send($exception->getMessage(),
|
||||||
$response->end($exception->getMessage());
|
$exception->getCode() == 0 ? 500 : $exception->getCode());
|
||||||
} finally {
|
} finally {
|
||||||
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
|
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ namespace Snowflake\Abstracts;
|
|||||||
use Annotation\Annotation as SAnnotation;
|
use Annotation\Annotation as SAnnotation;
|
||||||
use Exception;
|
use Exception;
|
||||||
use HttpServer\Client\Http2;
|
use HttpServer\Client\Http2;
|
||||||
|
use HttpServer\Http\HttpHeaders;
|
||||||
|
use HttpServer\Http\HttpParams;
|
||||||
use HttpServer\Http\Request;
|
use HttpServer\Http\Request;
|
||||||
use HttpServer\Http\Response;
|
use HttpServer\Http\Response;
|
||||||
use HttpServer\HttpFilter;
|
use HttpServer\HttpFilter;
|
||||||
@@ -457,6 +459,8 @@ abstract class BaseApplication extends Service
|
|||||||
'event' => ['class' => Event::class],
|
'event' => ['class' => Event::class],
|
||||||
'redis' => ['class' => Redis::class],
|
'redis' => ['class' => Redis::class],
|
||||||
'aop' => ['class' => Aop::class],
|
'aop' => ['class' => Aop::class],
|
||||||
|
'input' => ['class' => HttpParams::class],
|
||||||
|
'header' => ['class' => HttpHeaders::class],
|
||||||
'jwt' => ['class' => Jwt::class],
|
'jwt' => ['class' => Jwt::class],
|
||||||
'async' => ['class' => Async::class],
|
'async' => ['class' => Async::class],
|
||||||
'kafka-container' => ['class' => TaskContainer::class],
|
'kafka-container' => ['class' => TaskContainer::class],
|
||||||
|
|||||||
Reference in New Issue
Block a user