改名
This commit is contained in:
@@ -15,7 +15,6 @@ use Database\Traits\HasBase;
|
|||||||
use Exception;
|
use Exception;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
use Snowflake\Channel;
|
use Snowflake\Channel;
|
||||||
use Snowflake\Event;
|
|
||||||
use Snowflake\Exception\NotFindClassException;
|
use Snowflake\Exception\NotFindClassException;
|
||||||
use Snowflake\Snowflake;
|
use Snowflake\Snowflake;
|
||||||
|
|
||||||
@@ -33,9 +32,6 @@ defined('FIND_OR_CREATE_MESSAGE') or define('FIND_OR_CREATE_MESSAGE', 'Create a
|
|||||||
class ActiveRecord extends BaseActiveRecord
|
class ActiveRecord extends BaseActiveRecord
|
||||||
{
|
{
|
||||||
|
|
||||||
const DECR = 'decr';
|
|
||||||
const INCR = 'incr';
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
@@ -53,7 +49,7 @@ class ActiveRecord extends BaseActiveRecord
|
|||||||
*/
|
*/
|
||||||
public function increment(string $column, int $value): bool|ActiveRecord
|
public function increment(string $column, int $value): bool|ActiveRecord
|
||||||
{
|
{
|
||||||
if (!$this->mathematics([$column => $value], '+')) {
|
if (!$this->mathematics([$column => $value], '+', null)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$this->{$column} += $value;
|
$this->{$column} += $value;
|
||||||
@@ -69,7 +65,7 @@ class ActiveRecord extends BaseActiveRecord
|
|||||||
*/
|
*/
|
||||||
public function decrement(string $column, int $value): bool|ActiveRecord
|
public function decrement(string $column, int $value): bool|ActiveRecord
|
||||||
{
|
{
|
||||||
if (!$this->mathematics([$column => $value], '-')) {
|
if (!$this->mathematics([$column => $value], '-', null)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$this->{$column} -= $value;
|
$this->{$column} -= $value;
|
||||||
@@ -84,7 +80,7 @@ class ActiveRecord extends BaseActiveRecord
|
|||||||
*/
|
*/
|
||||||
public function increments(array $columns): bool|static
|
public function increments(array $columns): bool|static
|
||||||
{
|
{
|
||||||
if (!$this->mathematics($columns, '+')) {
|
if (!$this->mathematics($columns, '+', null)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
foreach ($columns as $key => $attribute) {
|
foreach ($columns as $key => $attribute) {
|
||||||
@@ -101,7 +97,7 @@ class ActiveRecord extends BaseActiveRecord
|
|||||||
*/
|
*/
|
||||||
public function decrements(array $columns): bool|static
|
public function decrements(array $columns): bool|static
|
||||||
{
|
{
|
||||||
if (!$this->mathematics($columns, '-')) {
|
if (!$this->mathematics($columns, '-', null)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
foreach ($columns as $key => $attribute) {
|
foreach ($columns as $key => $attribute) {
|
||||||
@@ -181,11 +177,11 @@ class ActiveRecord extends BaseActiveRecord
|
|||||||
/**
|
/**
|
||||||
* @param $action
|
* @param $action
|
||||||
* @param $columns
|
* @param $columns
|
||||||
* @param array $condition
|
* @param null|array $condition
|
||||||
* @return array|bool|int|string|null
|
* @return array|bool|int|string|null
|
||||||
* @throws Exception
|
* @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)) {
|
if (empty($condition)) {
|
||||||
$condition = [$this->getPrimary() => $this->getPrimaryValue()];
|
$condition = [$this->getPrimary() => $this->getPrimaryValue()];
|
||||||
|
|||||||
+21
-77
@@ -19,113 +19,69 @@ class Context extends BaseContext
|
|||||||
/**
|
/**
|
||||||
* @param $id
|
* @param $id
|
||||||
* @param $context
|
* @param $context
|
||||||
* @param null $key
|
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public static function setContext($id, $context, $key = null): mixed
|
public static function setContext($id, $context): mixed
|
||||||
{
|
{
|
||||||
if (Coroutine::getCid() === -1) {
|
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;
|
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;
|
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 $id
|
||||||
* @param null $key
|
|
||||||
* @param int $value
|
* @param int $value
|
||||||
* @return bool|int
|
* @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 false;
|
||||||
}
|
}
|
||||||
return Coroutine::getContext()[$id][$key] += $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $id
|
* @param $id
|
||||||
* @param null $key
|
|
||||||
* @param int $value
|
* @param int $value
|
||||||
* @return bool|int
|
* @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)) {
|
if (!static::hasContext($id)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!isset(Coroutine::getContext()[$id][$key])) {
|
if (isset(Coroutine::getContext()[$id])) {
|
||||||
|
return Coroutine::getContext()[$id] -= $value;
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return Coroutine::getContext()[$id][$key] -= $value;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $id
|
* @param $id
|
||||||
* @param null $key
|
* @param null $default
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public static function getContext($id, $key = null): mixed
|
public static function getContext($id, $default = null): mixed
|
||||||
{
|
{
|
||||||
if (Coroutine::getCid() === -1) {
|
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 $id
|
||||||
* @param null $key
|
* @param null $default
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
private static function loadByContext($id, $key = null): mixed
|
private static function loadByContext($id, $default = null): mixed
|
||||||
{
|
{
|
||||||
$data = Coroutine::getContext()[$id] ?? null;
|
$data = Coroutine::getContext()[$id] ?? null;
|
||||||
if ($data === null) {
|
if ($data === null) {
|
||||||
return null;
|
return $default;
|
||||||
}
|
|
||||||
if ($key !== null) {
|
|
||||||
return $data[$key] ?? null;
|
|
||||||
}
|
}
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
@@ -133,17 +89,14 @@ class Context extends BaseContext
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $id
|
* @param $id
|
||||||
* @param null $key
|
* @param null $default
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
private static function loadByStatic($id, $key = null): mixed
|
private static function loadByStatic($id, $default = null): mixed
|
||||||
{
|
{
|
||||||
$data = static::$_contents[$id] ?? null;
|
$data = static::$_contents[$id] ?? null;
|
||||||
if ($data === null) {
|
if ($data === null) {
|
||||||
return null;
|
return $default;
|
||||||
}
|
|
||||||
if ($key !== null) {
|
|
||||||
return $data[$key] ?? null;
|
|
||||||
}
|
}
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
@@ -163,27 +116,18 @@ class Context extends BaseContext
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $id
|
* @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;
|
return;
|
||||||
}
|
}
|
||||||
if (Coroutine::getCid() === -1) {
|
if (Coroutine::getCid() === -1) {
|
||||||
if (!empty($key)) {
|
|
||||||
unset(static::$_contents[$id][$key]);
|
|
||||||
} else {
|
|
||||||
unset(static::$_contents[$id]);
|
unset(static::$_contents[$id]);
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (!empty($key)) {
|
|
||||||
unset(Coroutine::getContext()[$id][$key]);
|
|
||||||
} else {
|
} else {
|
||||||
unset(Coroutine::getContext()[$id]);
|
unset(Coroutine::getContext()[$id]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $id
|
* @param $id
|
||||||
|
|||||||
@@ -17,7 +17,10 @@ use HttpServer\Controller;
|
|||||||
use HttpServer\Http\Context;
|
use HttpServer\Http\Context;
|
||||||
use HttpServer\Http\Request;
|
use HttpServer\Http\Request;
|
||||||
use JetBrains\PhpStorm\Pure;
|
use JetBrains\PhpStorm\Pure;
|
||||||
|
use ReflectionException;
|
||||||
|
use Snowflake\Aop;
|
||||||
use Snowflake\Core\Json;
|
use Snowflake\Core\Json;
|
||||||
|
use Snowflake\Exception\NotFindClassException;
|
||||||
use Snowflake\Snowflake;
|
use Snowflake\Snowflake;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
|
|
||||||
@@ -109,30 +112,49 @@ class Node extends HttpService
|
|||||||
}
|
}
|
||||||
if (!empty($this->handler)) {
|
if (!empty($this->handler)) {
|
||||||
$this->callback = Reduce::reduce($this->createDispatch(), $this->annotation());
|
$this->callback = Reduce::reduce($this->createDispatch(), $this->annotation());
|
||||||
|
$this->setAop();
|
||||||
}
|
}
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private array $_aop;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Closure
|
* @return Closure
|
||||||
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function createDispatch(): Closure
|
public function createDispatch(): Closure
|
||||||
{
|
{
|
||||||
$handler = $this->handler;
|
return static function () {
|
||||||
return static function () use ($handler) {
|
$dispatchParam = Context::getContext('dispatch-param', [\request()]);
|
||||||
$dispatchParam = Context::getContext('dispatch-param');
|
if (empty($this->_aop) || $this->handler instanceof Closure) {
|
||||||
if (empty($dispatchParam)) {
|
return call_user_func($this->handler, ...$dispatchParam);
|
||||||
$dispatchParam = [\request()];
|
|
||||||
}
|
}
|
||||||
if ($handler instanceof Closure) {
|
return call_user_func($this->_aop[0], $this->_aop[1], $dispatchParam);
|
||||||
return call_user_func($handler, ...$dispatchParam);
|
|
||||||
}
|
|
||||||
return \aop($handler, $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;
|
private bool $_hasBeforeAction = false;
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+32
-20
@@ -5,6 +5,8 @@ namespace Snowflake;
|
|||||||
|
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use Reflection;
|
||||||
|
use ReflectionClass;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
use Snowflake\Abstracts\Component;
|
use Snowflake\Abstracts\Component;
|
||||||
use Snowflake\Exception\NotFindClassException;
|
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 $handler
|
||||||
* @param $params
|
* @param $params
|
||||||
@@ -51,28 +63,8 @@ class Aop extends Component
|
|||||||
*/
|
*/
|
||||||
final public function dispatch($handler, $params): mixed
|
final public function dispatch($handler, $params): mixed
|
||||||
{
|
{
|
||||||
if ($handler instanceof \Closure) {
|
|
||||||
return call_user_func($handler, ...$params);
|
|
||||||
}
|
|
||||||
$aopName = $handler[0]::class . '::' . $handler[1];
|
$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]));
|
$reflect = Snowflake::getDi()->getReflect(current(static::$_aop[$aopName]));
|
||||||
if (!$reflect->isInstantiable() || !$reflect->hasMethod('invoke')) {
|
if (!$reflect->isInstantiable() || !$reflect->hasMethod('invoke')) {
|
||||||
throw new Exception(ASPECT_ERROR . IAspect::class);
|
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 $handler
|
||||||
* @param $params
|
* @param $params
|
||||||
|
|||||||
@@ -32,7 +32,8 @@ class Connection extends Component
|
|||||||
*/
|
*/
|
||||||
public function inTransaction($cds): bool
|
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);
|
$connections = $this->createClient($coroutineName, $config);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ($number = Context::getContext('begin_' . $coroutineName, Coroutine::getCid())) {
|
if ($number = Context::getContext('begin_' . $coroutineName)) {
|
||||||
$number > 0 && $connections->beginTransaction();
|
$number > 0 && $connections->beginTransaction();
|
||||||
}
|
}
|
||||||
return Context::setContext($coroutineName, $connections);
|
return Context::setContext($coroutineName, $connections);
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ class Snowflake
|
|||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public static function set($alias, $array = []): mixed
|
public static function set($alias, array $array = []): mixed
|
||||||
{
|
{
|
||||||
return static::app()->set($alias, $array);
|
return static::app()->set($alias, $array);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user