This commit is contained in:
as2252258@163.com
2021-07-27 00:35:50 +08:00
parent 170de0b822
commit b89554b6c6
6 changed files with 1285 additions and 1328 deletions
+24 -9
View File
@@ -5,6 +5,7 @@ namespace Annotation;
use Exception;
use HttpServer\Http\Context;
use ReflectionException;
use ReflectionProperty;
use Snowflake\Core\Str;
@@ -23,7 +24,7 @@ use Snowflake\Snowflake;
* @param string $className
* @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
{
$injectValue = $this->parseInjectValue();
if (!($method = $this->getProperty($class, $method))) {
return false;
}
/** @var ReflectionProperty $class */
$injectValue = $this->parseInjectValue();
if ($method->isPrivate() || $method->isProtected()) {
$method = 'set' . ucfirst(Str::convertUnderline($method->getName()));
if (!method_exists($class, $method)) {
return false;
}
$class->$method($injectValue);
$this->setter($class, $method, $injectValue);
} else {
$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 $method
@@ -62,12 +74,12 @@ use Snowflake\Snowflake;
*/
private function getProperty($class, $method): ReflectionProperty|bool
{
if ($method instanceof ReflectionProperty) {
if ($method instanceof ReflectionProperty && !$method->isStatic()) {
return $method;
}
if (is_object($class)) $class = $class::class;
$method = Snowflake::getDi()->getClassProperty($class, $method);
if (!$method) {
if (!$method || $method->isStatic()) {
return false;
}
return $method;
@@ -80,6 +92,9 @@ use Snowflake\Snowflake;
*/
private function parseInjectValue(): mixed
{
if ($this->withContext) {
return Context::getContext($this->className);
}
if (class_exists($this->className)) {
return Snowflake::getDi()->get($this->className, $this->args);
} else if (Snowflake::app()->has($this->className)) {
+2 -16
View File
@@ -38,10 +38,6 @@ class Loader extends BaseObject
private static array $_methods = [];
private array $_annotationMaps = [];
/**
* @return array
*/
@@ -339,14 +335,12 @@ class Loader extends BaseObject
if (empty($classes)) {
return;
}
$annotation = Snowflake::getAnnotation();
foreach ($classes as $className) {
if (!isset(static::$_methods[$className])) {
continue;
}
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 $name
*/
private function methods($attribute, $annotation, $className, $name)
private function methods($attribute, $className, $name)
{
$handler = static::$_classes[$className]['handler'];
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);
}
}
}
}
+14 -64
View File
@@ -4,11 +4,14 @@ declare(strict_types=1);
namespace HttpServer;
use Annotation\Inject;
use Exception;
use HttpServer\Abstracts\HttpService;
use HttpServer\Http\HttpHeaders;
use HttpServer\Http\HttpParams;
use HttpServer\Http\Request;
use HttpServer\Http\Response;
use Snowflake\Abstracts\Input;
use Snowflake\Abstracts\TraitApplication;
use Snowflake\Snowflake;
@@ -17,82 +20,29 @@ use Snowflake\Snowflake;
* @package Snowflake\Snowflake\Web
* @property-read HttpParams $input
* @property-read HttpHeaders $headers
* @property-read Request $request
*/
class Controller extends HttpService
class Controller
{
use TraitApplication;
/**
* Controller constructor.
* @param array $config
* @throws Exception
* inject request
*
* @var \HttpServer\Http\Request
*/
public function __construct($config = [])
{
parent::__construct($config);
}
#[Inject(className: 'request', withContext: true)]
protected Request $request;
/**
* @throws Exception
* inject response
*
* @var \HttpServer\Http\Response
*/
public function init()
{
$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);
}
#[Inject('response')]
protected Response $response;
}
+4 -2
View File
@@ -452,7 +452,7 @@ class Request extends HttpService
* @param \Swoole\Http\Request $request
* @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 */
$sRequest = Context::setContext('request', new Request());
@@ -469,7 +469,9 @@ class Request extends HttpService
$sRequest->parseUri();
HResponse::create($response);
Context::setContext('input', $sRequest->params);
Context::setContext('header', $sRequest->headers);
return $sRequest;
}
+8 -8
View File
@@ -32,8 +32,6 @@ class HTTPServerListener extends Abstracts\Server
private Router $router;
private ApplicationStore $store;
/**
* HTTPServerListener constructor.
@@ -42,7 +40,6 @@ class HTTPServerListener extends Abstracts\Server
public function __construct()
{
$this->router = Snowflake::getApp('router');
$this->store = ApplicationStore::getStore();
parent::__construct();
}
@@ -105,16 +102,19 @@ class HTTPServerListener extends Abstracts\Server
*/
public function onRequest(Request $request, Response $response)
{
[$sRequest, $sResponse] = [HRequest::create($request), HResponse::create($response)];
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();
} else {
$this->router->dispatch();
$sResponse->send($node->dispatch(), 200);
}
} catch (Error | Throwable $exception) {
$response->setHeader('Content-Type', 'text/html; charset=utf-8');
$response->status($exception->getCode() == 0 ? 500 : $exception->getCode());
$response->end($exception->getMessage());
$sResponse->addHeader('Content-Type', 'text/html; charset=utf-8');
$sResponse->send($exception->getMessage(),
$exception->getCode() == 0 ? 500 : $exception->getCode());
} finally {
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
}
+4
View File
@@ -13,6 +13,8 @@ namespace Snowflake\Abstracts;
use Annotation\Annotation as SAnnotation;
use Exception;
use HttpServer\Client\Http2;
use HttpServer\Http\HttpHeaders;
use HttpServer\Http\HttpParams;
use HttpServer\Http\Request;
use HttpServer\Http\Response;
use HttpServer\HttpFilter;
@@ -457,6 +459,8 @@ abstract class BaseApplication extends Service
'event' => ['class' => Event::class],
'redis' => ['class' => Redis::class],
'aop' => ['class' => Aop::class],
'input' => ['class' => HttpParams::class],
'header' => ['class' => HttpHeaders::class],
'jwt' => ['class' => Jwt::class],
'async' => ['class' => Async::class],
'kafka-container' => ['class' => TaskContainer::class],