diff --git a/http-server/Events/WebSocket.php b/http-server/Events/WebSocket.php index 3baa375a..c82da76d 100644 --- a/http-server/Events/WebSocket.php +++ b/http-server/Events/WebSocket.php @@ -11,6 +11,7 @@ namespace HttpServer\Events; use Exception; use HttpServer\ServerManager; use ReflectionException; +use Snowflake\Application; use Snowflake\Error\Logger; use Snowflake\Event; use Snowflake\Exception\NotFindClassException; @@ -20,6 +21,7 @@ use Swoole\Http\Response as SResponse; use Swoole\Process\Pool; use Swoole\WebSocket\Frame; use Swoole\WebSocket\Server; +use HttpServer\Route\Annotation\Websocket as AWebsocket; /** * Class ServerWebSocket @@ -32,6 +34,7 @@ class WebSocket extends Server public $callback = []; + /** @var Application */ public $application; @@ -63,8 +66,14 @@ class WebSocket extends Server public function set(array $settings, $pool = null, $events = [], $config = []) { parent::set($settings); - Snowflake::get()->set(WebSocket::class, $this); - Snowflake::get()->set(Pool::class, $pool); + + $application = Snowflake::get(); + $application->set(WebSocket::class, $this); + $application->set(Pool::class, $pool); + + $annotation = $application->annotation; + $annotation->register('websocket', AWebsocket::class); + ServerManager::set($this, $settings, $this->application, $events, $config); } @@ -87,11 +96,12 @@ class WebSocket extends Server } $json = json_decode($frame->data, true); - $manager = Snowflake::get()->annotation; - $manager->runWith($this->getName($json), [$frame->fd, $server]); + /** @var AWebsocket $manager */ + $manager = Snowflake::get()->annotation->get('websocket'); + $path = $manager->getName(AWebsocket::MESSAGE, $json['route']); + $manager->runWith($path, [$frame->fd, $server]); } catch (Exception $exception) { -// $this->error($exception->getMessage(), __METHOD__, __FILE__); -// $this->addError($exception->getMessage()); + $this->application->addError($exception->getMessage(), 'websocket'); } finally { $event = Snowflake::get()->event; $event->trigger(Event::EVENT_AFTER_REQUEST); @@ -99,15 +109,6 @@ class WebSocket extends Server } } - /** - * @param $json - * @return string - */ - private function getName($json) - { - return 'WEBSOCKET:MESSAGE:' . $json['route']; - } - /** * @param SRequest $request * @param SResponse $response @@ -177,7 +178,7 @@ class WebSocket extends Server $event->trigger(Event::SERVER_CLOSE, [$fd]); } } catch (\Throwable $exception) { -// $this->addError($exception->getMessage()); + $this->application->addError($exception->getMessage()); } finally { $event->trigger(Event::RELEASE_ALL); Logger::insert(); diff --git a/http-server/Route/Annotation/Annotation.php b/http-server/Route/Annotation/Annotation.php index 12399477..124ef007 100644 --- a/http-server/Route/Annotation/Annotation.php +++ b/http-server/Route/Annotation/Annotation.php @@ -3,6 +3,7 @@ namespace HttpServer\Route\Annotation; +use ReflectionClass; use ReflectionException; use Snowflake\Abstracts\BaseAnnotation; use Snowflake\Snowflake; @@ -30,6 +31,47 @@ class Annotation extends \Snowflake\Annotation\Annotation protected $_annotations = []; + + + /** + * @param ReflectionClass $reflect + * @param $method + * @param $annotations + * @return mixed|null + * @throws ReflectionException + */ + public function read($reflect, $method, $annotations) + { + $method = $reflect->getMethod($method); + + $_annotations = $this->getDocCommentAnnotation($annotations, $method->getDocComment()); + + $array = []; + foreach ($_annotations as $keyName => $annotation) { + if (!in_array($keyName, $annotations)) { + continue; + } + $array[$keyName] = $this->pop($this->getName(...$annotation)); + } + return $array; + } + + + /** + * @param $controller + * @param $methodName + * @param $events + * @return array|void + * @throws + */ + public function createHandler($controller, $methodName, $events) + { + return Snowflake::createObject($events[2]); + } + + + + /** * @param $events * @return bool diff --git a/system/Annotation/Definition/Websocket.php b/http-server/Route/Annotation/Websocket.php similarity index 81% rename from system/Annotation/Definition/Websocket.php rename to http-server/Route/Annotation/Websocket.php index 940ba203..c0f1bcd3 100644 --- a/system/Annotation/Definition/Websocket.php +++ b/http-server/Route/Annotation/Websocket.php @@ -1,9 +1,8 @@ annotation->http; - if (!empty($methods = $annotation->getAnnotation(Annotation::class))) { - $this->_interceptors = $annotation->instance($reflect, $action, $methods); + $annotation = Snowflake::get()->annotation->get('http'); + if (!empty($annotations = $annotation->getAnnotation(Annotation::class))) { + $this->_interceptors = $annotation->read($reflect, $action, $annotations); } return [$reflect->newInstance(), $action]; } catch (Exception $exception) { diff --git a/http-server/Route/Router.php b/http-server/Route/Router.php index 07470da4..0a8c302f 100644 --- a/http-server/Route/Router.php +++ b/http-server/Route/Router.php @@ -511,9 +511,12 @@ class Router extends Application implements RouterInterface $prefix = APP_PATH . 'app/Http/'; /** @var Annotation $annotation */ - $annotation = Snowflake::get()->annotation->getHttp(); - $annotation->registration_notes($prefix . 'Interceptor', 'App\Http\Interceptor',Annotation::class); - $annotation->registration_notes($prefix . 'Limits', 'App\Http\Limits',Annotation::class); + $annotation = Snowflake::get()->annotation; + $annotation->register('http',Annotation::class); + + $annotation = $annotation->get('http'); + $annotation->registration_notes($prefix . 'Interceptor', 'App\Http\Interceptor'); + $annotation->registration_notes($prefix . 'Limits', 'App\Http\Limits'); include_once "$file"; } diff --git a/system/Abstracts/BaseAnnotation.php b/system/Abstracts/BaseAnnotation.php index 1abb23bf..fb39b1fb 100644 --- a/system/Abstracts/BaseAnnotation.php +++ b/system/Abstracts/BaseAnnotation.php @@ -19,34 +19,6 @@ abstract class BaseAnnotation extends Component { - /** - * @param ReflectionClass $reflect - * @return array - */ - protected function getPrivates(ReflectionClass $reflect) - { - $arrays = []; - $properties = $reflect->getProperties(ReflectionMethod::IS_PRIVATE); - foreach ($properties as $property) { - $arrays[] = $property->getName(); - } - return $arrays; - } - - - /** - * @param string $class - * @return string[] - * @throws ReflectionException - */ - public function getAnnotation(string $class) - { - $reflect = Snowflake::getDi()->getReflect($class); - - return $this->getPrivates($reflect); - } - - /** * @param ReflectionClass $reflect * @param string $method @@ -86,23 +58,64 @@ abstract class BaseAnnotation extends Component protected function resolveDocComment($function, $object, $annotations, $array) { $comment = $function->getDocComment(); + $array = $this->getDocCommentAnnotation($annotations, $comment); + foreach ($array as $name => $annotation) { + foreach ($annotation as $index => $events) { + if (!isset($events[1])) { + continue; + } + if (!($_key = $this->getName($name, $events))) { + continue; + } + if (isset($item[2])) { + $handler = Snowflake::createObject($events[2]); + } else { + $handler = [$object, $events[1]]; + } + if (!isset($array[$annotation])) { + $array[$annotation] = []; + } + $array[$name][] = [$_key, $handler]; + } + + } + return $array; + } + + + /** + * @param $object + * @param $events + * @throws NotFindClassException + * @throws ReflectionException + */ + protected function getOrCreate($object, $events) + { + if (isset($item[2])) { + $handler = Snowflake::createObject($events[2]); + } else { + $handler = [$object, $events[1]]; + } + } + + + /** + * @param $annotations + * @param $comment + * @return array + */ + protected function getDocCommentAnnotation($annotations, $comment) + { + $array = []; foreach ($annotations as $annotation) { preg_match('/@(' . $annotation . ')\((.*?)\)/', $comment, $events); if (!isset($events[1])) { continue; } - if (!($_key = $this->getName($function, $events))) { - continue; - } - if (isset($events[2])) { - $handler = Snowflake::createObject($events[2]); - } else { - $handler = [$object, $events[1]]; - } if (!isset($array[$annotation])) { $array[$annotation] = []; } - $array[$annotation][] = [$_key, $handler]; + $array[$annotation] = [$annotation, $events]; } return $array; } diff --git a/system/Annotation/Annotation.php b/system/Annotation/Annotation.php index 13a4a042..8ea36982 100644 --- a/system/Annotation/Annotation.php +++ b/system/Annotation/Annotation.php @@ -6,9 +6,8 @@ namespace Snowflake\Annotation; use Exception; use ReflectionClass; use ReflectionException; +use ReflectionMethod; use Snowflake\Abstracts\BaseAnnotation; -use Snowflake\Annotation\Definition\Http; -use Snowflake\Annotation\Definition\Websocket; use Snowflake\Exception\NotFindClassException; use Snowflake\Snowflake; use validator\RequiredValidator; @@ -17,15 +16,9 @@ use validator\RequiredValidator as NotEmptyValidator; /** * Class Annotation * @package Snowflake\Snowflake\Annotation - * @property Websocket $websocket - * @property Http $http */ class Annotation extends BaseAnnotation { - - protected $_Scan_directory = []; - protected $params = []; - public $namespace = ''; public $prefix = ''; @@ -33,72 +26,34 @@ class Annotation extends BaseAnnotation public $path = ''; - private $rules = [ - 'required' => [ - 'class' => RequiredValidator::class - ], - 'not empty' => [ - 'class' => NotEmptyValidator::class - ] - ]; + protected $_Scan_directory = []; + + protected $_alias = []; + + protected $params = []; - private $_classMap = [ - 'websocket' => Websocket::class, - 'http' => Http::class - ]; - + private $_classMap = []; /** * @param $name * @param $class + * @throws */ public function register($name, $class) { - $this->_classMap[$name] = $class; + $this->_classMap[$name] = Snowflake::createObject($class); } /** * @param $path * @param $namespace - * @param $class * @throws ReflectionException */ - public function registration_notes($path, $namespace, $class) + public function registration_notes($path, $namespace) { - $this->scanning(rtrim($path, '/'), $namespace, $class); - } - - - /** - * @return string|Http - * @throws - */ - public function getHttp() - { - if (is_object($this->_classMap['http'])) { - return $this->_classMap['http']; - } - - return $this->_classMap['http'] = Snowflake::createObject($this->_classMap['http']); - } - - - /** - * @return string|Websocket - * @throws - */ - public function getWebsocket() - { - if (is_object($this->_classMap['websocket'])) { - return $this->_classMap['websocket']; - } - - return $this->_classMap['websocket'] = Snowflake::createObject($this->_classMap['websocket']); - - - return make($this->_classMap['websocket'], $this->_classMap['websocket']); + $this->scanning(rtrim($path, '/'), $namespace, get_called_class()); } /** @@ -107,8 +62,9 @@ class Annotation extends BaseAnnotation * @throws ReflectionException * @throws Exception * @Message(updatePosition) + * 注入注解 */ - public function resolve(ReflectionClass $reflect, string $className) + private function resolve(ReflectionClass $reflect, string $className) { $controller = $reflect->newInstance(); @@ -139,6 +95,20 @@ class Annotation extends BaseAnnotation } + /** + * @param $name + * @return mixed + * @throws Exception + */ + public function get($name) + { + if (!isset($this->_Scan_directory[$name])) { + throw new Exception('Undefined analytic function.'); + } + return $this->_Scan_directory[$name]; + } + + /** * @param $events * @throws Exception @@ -196,16 +166,17 @@ class Annotation extends BaseAnnotation /** * @param $path + * @param array $params * @return bool|mixed */ - public function runWith($path) + public function runWith($path, $params = []) { if (!$this->has($path)) { return null; } $callback = $this->_Scan_directory[$path]; if (!isset($this->params[$path])) { - return $callback(); + return $callback(...$params); } return $callback(...$this->params[$path]); } @@ -225,6 +196,19 @@ class Annotation extends BaseAnnotation } + /** + * @param $name + * @return array|null[] + */ + public function pop($name) + { + if (!isset($this->_Scan_directory[$name])) { + return [$this->_Scan_directory[$name], $this->params[$name] ?? []]; + } + return [null, null]; + } + + /** * @param $path * @return bool|mixed @@ -253,5 +237,32 @@ class Annotation extends BaseAnnotation return $this->_classMap[$name]; } + /** + * @param ReflectionClass $reflect + * @return array + */ + protected function getPrivates(ReflectionClass $reflect) + { + $arrays = []; + $properties = $reflect->getProperties(ReflectionMethod::IS_PRIVATE); + foreach ($properties as $property) { + $arrays[] = $property->getName(); + } + return $arrays; + } + + + /** + * @param string $class + * @return string[] + * @throws ReflectionException + */ + public function getAnnotation(string $class) + { + $reflect = Snowflake::getDi()->getReflect($class); + + return $this->getPrivates($reflect); + } + } diff --git a/system/Annotation/Definition/Http.php b/system/Annotation/Definition/Http.php deleted file mode 100644 index 992a1463..00000000 --- a/system/Annotation/Definition/Http.php +++ /dev/null @@ -1,69 +0,0 @@ -