This commit is contained in:
2021-07-12 18:51:41 +08:00
parent 16b300b139
commit 49f358fa93
6 changed files with 610 additions and 635 deletions
+6 -10
View File
@@ -15,7 +15,6 @@ use Database\Traits\HasBase;
use Exception;
use ReflectionException;
use Snowflake\Channel;
use Snowflake\Event;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
@@ -33,9 +32,6 @@ defined('FIND_OR_CREATE_MESSAGE') or define('FIND_OR_CREATE_MESSAGE', 'Create a
class ActiveRecord extends BaseActiveRecord
{
const DECR = 'decr';
const INCR = 'incr';
/**
* @return array
@@ -53,7 +49,7 @@ class ActiveRecord extends BaseActiveRecord
*/
public function increment(string $column, int $value): bool|ActiveRecord
{
if (!$this->mathematics([$column => $value], '+')) {
if (!$this->mathematics([$column => $value], '+', null)) {
return false;
}
$this->{$column} += $value;
@@ -69,7 +65,7 @@ class ActiveRecord extends BaseActiveRecord
*/
public function decrement(string $column, int $value): bool|ActiveRecord
{
if (!$this->mathematics([$column => $value], '-')) {
if (!$this->mathematics([$column => $value], '-', null)) {
return false;
}
$this->{$column} -= $value;
@@ -84,7 +80,7 @@ class ActiveRecord extends BaseActiveRecord
*/
public function increments(array $columns): bool|static
{
if (!$this->mathematics($columns, '+')) {
if (!$this->mathematics($columns, '+', null)) {
return false;
}
foreach ($columns as $key => $attribute) {
@@ -101,7 +97,7 @@ class ActiveRecord extends BaseActiveRecord
*/
public function decrements(array $columns): bool|static
{
if (!$this->mathematics($columns, '-')) {
if (!$this->mathematics($columns, '-', null)) {
return false;
}
foreach ($columns as $key => $attribute) {
@@ -181,11 +177,11 @@ class ActiveRecord extends BaseActiveRecord
/**
* @param $action
* @param $columns
* @param array $condition
* @param null|array $condition
* @return array|bool|int|string|null
* @throws Exception
*/
private function mathematics($columns, $action, $condition = []): int|bool|array|string|null
private function mathematics($columns, $action, ?array $condition): int|bool|array|string|null
{
if (empty($condition)) {
$condition = [$this->getPrimary() => $this->getPrimaryValue()];
+21 -77
View File
@@ -19,113 +19,69 @@ class Context extends BaseContext
/**
* @param $id
* @param $context
* @param null $key
* @return mixed
*/
public static function setContext($id, $context, $key = null): mixed
public static function setContext($id, $context): mixed
{
if (Coroutine::getCid() === -1) {
return static::setStatic($id, $context, $key);
}
return self::setCoroutine($id, $context, $key);
}
/**
* @param $id
* @param $context
* @param null $key
* @return array
*/
private static function setStatic($id, $context, $key = null): mixed
{
if (empty($key)) {
return static::$_contents[$id] = $context;
}
if (!is_array(static::$_contents[$id])) {
static::$_contents[$id] = [$key => $context];
} else {
static::$_contents[$id][$key] = $context;
}
return $context;
}
/**
* @param $id
* @param $context
* @param null $key
* @return mixed
*/
private static function setCoroutine($id, $context, $key = null): mixed
{
if (empty($key)) {
return Coroutine::getContext()[$id] = $context;
}
if (!is_array(Coroutine::getContext()[$id])) {
Coroutine::getContext()[$id] = [$key => $context];
} else {
Coroutine::getContext()[$id][$key] = $context;
}
return $context;
}
/**
* @param $id
* @param null $key
* @param int $value
* @return bool|int
*/
public static function increment($id, $key = null, $value = 1): bool|int
public static function increment($id, int $value = 1): bool|int
{
if (!isset(Coroutine::getContext()[$id][$key])) {
if (!isset(Coroutine::getContext()[$id])) {
return Coroutine::getContext()[$id] += $value;
}
return false;
}
return Coroutine::getContext()[$id][$key] += $value;
}
/**
* @param $id
* @param null $key
* @param int $value
* @return bool|int
*/
public static function decrement($id, $key = null, $value = 1): bool|int
public static function decrement($id, int $value = 1): bool|int
{
if (!static::hasContext($id)) {
return false;
}
if (!isset(Coroutine::getContext()[$id][$key])) {
if (isset(Coroutine::getContext()[$id])) {
return Coroutine::getContext()[$id] -= $value;
}
return false;
}
return Coroutine::getContext()[$id][$key] -= $value;
}
/**
* @param $id
* @param null $key
* @param null $default
* @return mixed
*/
public static function getContext($id, $key = null): mixed
public static function getContext($id, $default = null): mixed
{
if (Coroutine::getCid() === -1) {
return static::loadByStatic($id, $key);
return static::loadByStatic($id, $default);
}
return static::loadByContext($id, $key);
return static::loadByContext($id, $default);
}
/**
* @param $id
* @param null $key
* @param null $default
* @return mixed
*/
private static function loadByContext($id, $key = null): mixed
private static function loadByContext($id, $default = null): mixed
{
$data = Coroutine::getContext()[$id] ?? null;
if ($data === null) {
return null;
}
if ($key !== null) {
return $data[$key] ?? null;
return $default;
}
return $data;
}
@@ -133,17 +89,14 @@ class Context extends BaseContext
/**
* @param $id
* @param null $key
* @param null $default
* @return mixed
*/
private static function loadByStatic($id, $key = null): mixed
private static function loadByStatic($id, $default = null): mixed
{
$data = static::$_contents[$id] ?? null;
if ($data === null) {
return null;
}
if ($key !== null) {
return $data[$key] ?? null;
return $default;
}
return $data;
}
@@ -163,27 +116,18 @@ class Context extends BaseContext
/**
* @param string $id
* @param string|null $key
*/
public static function remove(string $id, string $key = null)
public static function remove(string $id)
{
if (!static::hasContext($id, $key)) {
if (!static::hasContext($id)) {
return;
}
if (Coroutine::getCid() === -1) {
if (!empty($key)) {
unset(static::$_contents[$id][$key]);
} else {
unset(static::$_contents[$id]);
}
} else {
if (!empty($key)) {
unset(Coroutine::getContext()[$id][$key]);
} else {
unset(Coroutine::getContext()[$id]);
}
}
}
/**
* @param $id
+31 -9
View File
@@ -17,7 +17,10 @@ use HttpServer\Controller;
use HttpServer\Http\Context;
use HttpServer\Http\Request;
use JetBrains\PhpStorm\Pure;
use ReflectionException;
use Snowflake\Aop;
use Snowflake\Core\Json;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
use Throwable;
@@ -109,30 +112,49 @@ class Node extends HttpService
}
if (!empty($this->handler)) {
$this->callback = Reduce::reduce($this->createDispatch(), $this->annotation());
$this->setAop();
}
return $this;
}
private array $_aop;
/**
* @return Closure
* @throws Exception
*/
public function createDispatch(): Closure
{
$handler = $this->handler;
return static function () use ($handler) {
$dispatchParam = Context::getContext('dispatch-param');
if (empty($dispatchParam)) {
$dispatchParam = [\request()];
return static function () {
$dispatchParam = Context::getContext('dispatch-param', [\request()]);
if (empty($this->_aop) || $this->handler instanceof Closure) {
return call_user_func($this->handler, ...$dispatchParam);
}
if ($handler instanceof Closure) {
return call_user_func($handler, ...$dispatchParam);
}
return \aop($handler, $dispatchParam);
return call_user_func($this->_aop[0], $this->_aop[1], $dispatchParam);
};
}
/**
* @throws ReflectionException
* @throws NotFindClassException
*/
private function setAop()
{
/** @var Aop $aop */
$aop = Snowflake::app()->get('aop');
if (!$aop->hasAop($this->handler)) {
return;
}
$reflect = $aop->getAop($this->handler);
$method = $reflect->getMethod('invoke');
$this->_aop = [[$method, 'invokeArgs'], $reflect->newInstance($this->handler)];
}
private bool $_hasBeforeAction = false;
+32 -20
View File
@@ -5,6 +5,8 @@ namespace Snowflake;
use Exception;
use Reflection;
use ReflectionClass;
use ReflectionException;
use Snowflake\Abstracts\Component;
use Snowflake\Exception\NotFindClassException;
@@ -41,6 +43,16 @@ class Aop extends Component
}
/**
* @param $handler
* @return bool
*/
public function hasAop($handler): bool
{
return isset(static::$_aop[$handler[0]::class . '::' . $handler[1]]);
}
/**
* @param $handler
* @param $params
@@ -51,28 +63,8 @@ class Aop extends Component
*/
final public function dispatch($handler, $params): mixed
{
if ($handler instanceof \Closure) {
return call_user_func($handler, ...$params);
}
$aopName = $handler[0]::class . '::' . $handler[1];
if (!isset(static::$_aop[$aopName])) {
return $this->notFound($handler, $params);
}
return $this->invoke($handler, $params, $aopName);
}
/**
* @param $handler
* @param $params
* @param $aopName
* @return mixed
* @throws ReflectionException
* @throws NotFindClassException
* @throws Exception
*/
private function invoke($handler, $params, $aopName): mixed
{
$reflect = Snowflake::getDi()->getReflect(current(static::$_aop[$aopName]));
if (!$reflect->isInstantiable() || !$reflect->hasMethod('invoke')) {
throw new Exception(ASPECT_ERROR . IAspect::class);
@@ -83,6 +75,26 @@ class Aop extends Component
}
/**
* @param array $handler
* @return ReflectionClass
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/
public function getAop(array $handler): ReflectionClass
{
$aopName = $handler[0]::class . '::' . $handler[1];
$reflect = Snowflake::getDi()->getReflect(current(static::$_aop[$aopName]));
if (!$reflect->isInstantiable() || !$reflect->hasMethod('invoke')) {
throw new Exception(ASPECT_ERROR . IAspect::class);
}
return $reflect;
}
/**
* @param $handler
* @param $params
+3 -2
View File
@@ -32,7 +32,8 @@ class Connection extends Component
*/
public function inTransaction($cds): bool
{
return Context::getContext('begin_' . $this->getPool()->name('Mysql:' . $cds, true)) == 0;
$name = $this->getPool()->name('Mysql:' . $cds, true);
return Context::getContext('begin_' . $name) == 0;
}
/**
@@ -122,7 +123,7 @@ class Connection extends Component
$connections = $this->createClient($coroutineName, $config);
}
}
if ($number = Context::getContext('begin_' . $coroutineName, Coroutine::getCid())) {
if ($number = Context::getContext('begin_' . $coroutineName)) {
$number > 0 && $connections->beginTransaction();
}
return Context::setContext($coroutineName, $connections);
+1 -1
View File
@@ -67,7 +67,7 @@ class Snowflake
* @return mixed
* @throws Exception
*/
public static function set($alias, $array = []): mixed
public static function set($alias, array $array = []): mixed
{
return static::app()->set($alias, $array);
}