This commit is contained in:
2020-12-17 14:09:14 +08:00
parent 672a719dbd
commit 36c1d0502a
151 changed files with 1937 additions and 2848 deletions
-157
View File
@@ -1,157 +0,0 @@
<?php
declare(strict_types=1);
namespace Snowflake\Abstracts;
use Exception;
use ReflectionClass;
use ReflectionException;
use ReflectionMethod;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
/**
* Class BaseAnnotation
* @package Snowflake\Snowflake\Annotation\Base
*/
abstract class BaseAnnotation extends Component
{
/**
* @param ReflectionClass $reflect
* @param string $method
* @param array $annotations
* @return array
* @throws ReflectionException
* @throws Exception
*/
public function instance(ReflectionClass $reflect, $method = '', $annotations = [])
{
$classMethods = $reflect->getMethods(ReflectionMethod::IS_PUBLIC);
if (!$reflect->isInstantiable()) {
throw new Exception('Class ' . $reflect->getName() . ' cannot be instantiated.');
}
$array = [];
$object = $reflect->newInstance();
if (!empty($method)) {
$array = $this->resolveDocComment($reflect->getMethod($method), $object, $annotations, $array);
} else {
foreach ($classMethods as $classMethod) {
$array = $this->resolveDocComment($classMethod, $object, $annotations, $array);
}
}
return $array;
}
/**
* @param ReflectionMethod $function
* @param $object
* @param $annotations
* @param $array
* @return array
* @throws
*/
protected function resolveDocComment(ReflectionMethod $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) {
if (!$comment) {
continue;
}
preg_match('/@(' . $annotation . ')\((.*?)\)/', $comment, $events);
if (!isset($events[1])) {
continue;
}
if (!isset($array[$annotation])) {
$array[$annotation] = [];
}
$array[$annotation] = [$annotation, $events];
}
return $array;
}
/**
* @param $rule
* @param $content
* @param $rules
* @return bool
* @throws ReflectionException
* @throws NotFindClassException
* @throws Exception
*/
public function check($rule, $content, $rules)
{
if (empty($rule)) {
return true;
}
$explode = explode('|', $rule);
foreach ($explode as $value) {
$reflect = array_merge($rules[$value], [
'value' => $content
]);
$validator = Snowflake::createObject($reflect);
if (!$validator->check()) {
throw new Exception($validator->getMessage());
}
}
return false;
}
abstract public function runWith($path);
}
-15
View File
@@ -18,9 +18,7 @@ use HttpServer\Route\Router;
use HttpServer\Server;
use JetBrains\PhpStorm\Pure;
use Kafka\Producer;
use Snowflake\Annotation\Annotation;
use Annotation\Annotation as SAnnotation;
use Snowflake\Cache\Memcached;
use Snowflake\Cache\Redis;
use Snowflake\Di\Service;
use Snowflake\Error\ErrorHandler;
@@ -38,7 +36,6 @@ use Database\DatabasesProviders;
/**
* Class BaseApplication
* @package Snowflake\Snowflake\Base
* @property Annotation $annotation
* @property Event $event
* @property Router $router
* @property SPool $pool
@@ -46,7 +43,6 @@ use Database\DatabasesProviders;
* @property Server $server
* @property DatabasesProviders $db
* @property Connection $connections
* @property Memcached $memcached
* @property Logger $logger
* @property Jwt $jwt
* @property SAnnotation $attributes
@@ -283,16 +279,6 @@ abstract class BaseApplication extends Service
}
/**
* @return Annotation
* @throws ComponentException
*/
public function getAnnotation(): Annotation
{
return $this->get('annotation');
}
/**
* @return Connection
* @throws ComponentException
@@ -389,7 +375,6 @@ abstract class BaseApplication extends Service
$this->setComponents([
'error' => ['class' => ErrorHandler::class],
'event' => ['class' => Event::class],
'annotation' => ['class' => Annotation::class],
'connections' => ['class' => Connection::class],
'redis_connections' => ['class' => SRedis::class],
'pool' => ['class' => SPool::class],
+4 -4
View File
@@ -16,12 +16,12 @@ class BaseGoto extends Component
{
/**
* @param $message
* @param string $message
* @param int $statusCode
* @return mixed|void
* @throws Exception
* @return mixed
* @throws ExitException
*/
public function end(string $message, $statusCode = 200)
public function end(string $message, $statusCode = 200): mixed
{
throw new ExitException(JSON::to(12350, $message), $statusCode);
}
+9 -11
View File
@@ -10,6 +10,7 @@ declare(strict_types=1);
namespace Snowflake\Abstracts;
use Exception;
use JetBrains\PhpStorm\Pure;
use Snowflake\Error\Logger;
use Snowflake\Snowflake;
@@ -44,7 +45,7 @@ class BaseObject implements Configure
/**
* @return string
*/
public static function className()
#[Pure] public static function className(): string
{
return get_called_class();
}
@@ -72,7 +73,7 @@ class BaseObject implements Configure
* @return mixed
* @throws Exception
*/
public function __get($name)
public function __get($name): mixed
{
$method = 'get' . ucfirst($name);
if (method_exists($this, $method)) {
@@ -89,7 +90,7 @@ class BaseObject implements Configure
* @return bool
* @throws Exception
*/
public function addError($message, $model = 'app')
public function addError($message, $model = 'app'): bool
{
if ($message instanceof \Throwable) {
$this->error($message->getMessage(), $message->getFile(), $message->getLine());
@@ -109,9 +110,8 @@ class BaseObject implements Configure
* @param mixed $message
* @param string $method
* @param string $file
* @throws
*/
public function debug($message, string $method = __METHOD__, string $file = __FILE__)
public function debug(mixed $message, string $method = __METHOD__, string $file = __FILE__)
{
if (!is_string($message)) {
$message = print_r($message, true);
@@ -125,9 +125,8 @@ class BaseObject implements Configure
* @param mixed $message
* @param string $method
* @param string $file
* @throws
*/
public function info($message, string $method = __METHOD__, string $file = __FILE__)
public function info(mixed $message, string $method = __METHOD__, string $file = __FILE__)
{
if (!is_string($message)) {
$message = print_r($message, true);
@@ -140,9 +139,8 @@ class BaseObject implements Configure
* @param mixed $message
* @param string $method
* @param string $file
* @throws
*/
public function success($message, string $method = __METHOD__, string $file = __FILE__)
public function success(mixed $message, string $method = __METHOD__, string $file = __FILE__)
{
if (!is_string($message)) {
$message = print_r($message, true);
@@ -157,7 +155,7 @@ class BaseObject implements Configure
* @param string $method
* @param string $file
*/
public function warning($message, string $method = __METHOD__, string $file = __FILE__)
public function warning(mixed $message, string $method = __METHOD__, string $file = __FILE__)
{
if (!is_string($message)) {
$message = print_r($message, true);
@@ -172,7 +170,7 @@ class BaseObject implements Configure
* @param null $method
* @param null $file
*/
public function error($message, $method = null, $file = null)
public function error(mixed $message, $method = null, $file = null)
{
if (!empty($file)) {
echo "\033[41;37m[ERROR][" . date('Y-m-d H:i:s') . ']: ' . $file . "\033[0m";
+11 -7
View File
@@ -6,10 +6,12 @@
* Time: 14:28
*/
declare(strict_types=1);
namespace Snowflake\Abstracts;
use Exception;
use JetBrains\PhpStorm\Pure;
use Snowflake\Snowflake;
/**
@@ -48,7 +50,7 @@ class Component extends BaseObject
* @param null $callback
* @return bool
*/
public function hasEvent($name, $callback = null)
#[Pure] public function hasEvent($name, $callback = null): bool
{
if (!isset($this->_events[$name])) {
return false;
@@ -94,19 +96,21 @@ class Component extends BaseObject
/**
* @param $name
* @param null $handler
* @return mixed
* @return void
*/
public function off($name, $handler = NULL)
public function off($name, $handler = NULL): void
{
$aEvents = Snowflake::app()->event;
if (!isset($this->_events[$name])) {
return $aEvents->of($name, $handler);
$aEvents->of($name, $handler);
return;
}
if (empty($handler)) {
unset($this->_events[$name]);
return $aEvents->of($name, $handler);
$aEvents->of($name, $handler);
return;
}
foreach ($this->_events[$name] as $key => $val) {
@@ -117,7 +121,7 @@ class Component extends BaseObject
break;
}
return $aEvents->of($name, $handler);
$aEvents->of($name, $handler);
}
/**
@@ -150,7 +154,7 @@ class Component extends BaseObject
* @return mixed
* @throws Exception
*/
public function __get($name)
public function __get($name): mixed
{
if (property_exists($this, $name)) {
return $this->$name ?? null;
+7 -6
View File
@@ -9,6 +9,7 @@ declare(strict_types=1);
namespace Snowflake\Abstracts;
use Exception;
use JetBrains\PhpStorm\Pure;
use Snowflake\Exception\ConfigException;
use Snowflake\Abstracts\Component;
use Snowflake\Snowflake;
@@ -29,7 +30,7 @@ class Config extends Component
/**
* @return mixed
*/
public function getData()
public function getData(): mixed
{
return $this->data;
}
@@ -40,7 +41,7 @@ class Config extends Component
* @param $value
* @return mixed
*/
public function setData($key, $value)
public function setData($key, $value): mixed
{
return $this->data[$key] = $value;
}
@@ -52,10 +53,10 @@ class Config extends Component
* @return mixed
* @throws
*/
public static function get($key, $try = FALSE, $default = null)
public static function get($key, $try = FALSE, $default = null): mixed
{
$instance = Snowflake::app()->config->getData();
if (strpos($key, '.') === false) {
if (!str_contains($key, '.')) {
return isset($instance[$key]) ? $instance[$key] : $default;
}
foreach (explode('.', $key) as $value) {
@@ -82,7 +83,7 @@ class Config extends Component
* @return mixed
* @throws Exception
*/
public static function set($key, $value)
public static function set($key, $value): mixed
{
$config = Snowflake::app()->config;
return $config->setData($key, $value);
@@ -93,7 +94,7 @@ class Config extends Component
* @param bool $must_not_null
* @return bool
*/
public static function has($key, $must_not_null = false)
#[Pure] public static function has($key, $must_not_null = false): bool
{
$config = Snowflake::app()->config;
if (!isset($config->data[$key])) {
+6 -6
View File
@@ -29,7 +29,7 @@ class Input
/**
* @return string
*/
public function getCommandName()
public function getCommandName(): string
{
return $this->_command;
}
@@ -40,7 +40,7 @@ class Input
* @param null $default
* @return mixed|null
*/
public function get($key, $default = null)
public function get($key, $default = null): mixed
{
return $this->_argv[$key] ?? $default;
}
@@ -50,7 +50,7 @@ class Input
* @param $value
* @return $this
*/
public function set($key, $value)
public function set($key, $value): static
{
$this->_argv[$key] = $value;
return $this;
@@ -60,7 +60,7 @@ class Input
/**
* @return false|string
*/
public function toJson()
public function toJson(): bool|string
{
return json_encode($this->_argv, JSON_UNESCAPED_UNICODE);
}
@@ -71,7 +71,7 @@ class Input
* @return array
* @throws Exception
*/
public function resolve($parameters)
public function resolve($parameters): array
{
$arrays = [];
$parameters = array_slice($parameters, 1);
@@ -90,7 +90,7 @@ class Input
/**
* @return string
*/
public function getCommand()
public function getCommand(): string
{
return $this->_command;
}
+1 -1
View File
@@ -19,7 +19,7 @@ abstract class Listener extends Component implements IListener
* @return string
* @throws \Exception
*/
public function getName()
public function getName(): string
{
if (empty($this->trigger)) {
throw new \Exception('Listener name con\'t empty.');
+16 -15
View File
@@ -5,6 +5,7 @@ namespace Snowflake\Abstracts;
use Exception;
use JetBrains\PhpStorm\Pure;
use Swoole\Coroutine;
use Swoole\Coroutine\Channel;
@@ -37,10 +38,10 @@ abstract class Pool extends Component
/**
* @param $name
* @return mixed
* @return array
* @throws Exception
*/
protected function get($name)
protected function get($name): array
{
[$timeout, $connection] = $this->_items[$name]->pop();
if (!$this->checkCanUse($name, $timeout, $connection)) {
@@ -56,12 +57,12 @@ abstract class Pool extends Component
* @param false $isMaster
* @return string
*/
public function name($cds, $isMaster = false)
#[Pure] public function name($cds, $isMaster = false): string
{
if ($isMaster === true) {
return hash('sha256',$cds . 'master');
return hash('sha256', $cds . 'master');
} else {
return hash('sha256',$cds . 'slave');
return hash('sha256', $cds . 'slave');
}
}
@@ -74,7 +75,7 @@ abstract class Pool extends Component
* 检查连接可靠性
* @throws Exception
*/
public function checkCanUse($name, $time, $client)
public function checkCanUse(string $name, int $time, mixed $client): mixed
{
throw new Exception('Undefined system processing function.');
}
@@ -83,7 +84,7 @@ abstract class Pool extends Component
* @param $name
* @throws Exception
*/
public function desc($name)
public function desc(string $name)
{
throw new Exception('Undefined system processing function.');
}
@@ -93,16 +94,16 @@ abstract class Pool extends Component
* @param $isMaster
* @throws Exception
*/
public function getConnection(array $config, $isMaster)
public function getConnection(array $config, bool $isMaster)
{
throw new Exception('Undefined system processing function.');
}
/**
* @param $name
* @return mixed
* @return bool
*/
public function hasItem($name)
public function hasItem(string $name): bool
{
return $this->size($name) > 0;
}
@@ -112,7 +113,7 @@ abstract class Pool extends Component
* @param $name
* @return mixed
*/
public function size($name)
public function size(string $name): mixed
{
if (!isset($this->_items[$name])) {
return 0;
@@ -125,7 +126,7 @@ abstract class Pool extends Component
* @param $name
* @param $client
*/
public function push($name, $client)
public function push(string $name, mixed $client)
{
$this->_items[$name]->push([time(), $client]);
unset($client);
@@ -133,9 +134,9 @@ abstract class Pool extends Component
/**
* @param $name
* @param string $name
*/
public function clean($name)
public function clean(string $name)
{
if (!isset($this->_items[$name])) {
return;
@@ -150,7 +151,7 @@ abstract class Pool extends Component
/**
* @return Channel[]
*/
protected function getChannels()
protected function getChannels(): array
{
return $this->_items;
}