From 82257092d620ea7c6153a766f52ff656fc217fd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Fri, 30 Apr 2021 15:21:31 +0800 Subject: [PATCH 01/32] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HttpServer/Http/HttpParams.php | 10 ++-------- HttpServer/Http/Request.php | 25 ++++++++++--------------- 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/HttpServer/Http/HttpParams.php b/HttpServer/Http/HttpParams.php index 2cc70746..a83792fb 100644 --- a/HttpServer/Http/HttpParams.php +++ b/HttpServer/Http/HttpParams.php @@ -13,9 +13,7 @@ use Exception; use HttpServer\Exception\RequestException; use JetBrains\PhpStorm\Pure; use ReflectionException; -use Snowflake\Core\Help; use Snowflake\Core\Json; -use Snowflake\Core\Str; use Snowflake\Exception\NotFindClassException; use Snowflake\Snowflake; @@ -26,7 +24,7 @@ use Snowflake\Snowflake; class HttpParams { - private ?array $body = []; + private string|array $body = []; /** @var array */ private array $gets = []; @@ -47,11 +45,7 @@ class HttpParams $this->gets = $get ?? []; $this->files = $files ?? []; $this->socket = $socket ?? []; - if (!is_array($body)) { - $this->body = Help::toArray($body); - } else { - $this->body = $body ?? []; - } + $this->body = $body ?? ''; } /** diff --git a/HttpServer/Http/Request.php b/HttpServer/Http/Request.php index 219a1611..401421c9 100644 --- a/HttpServer/Http/Request.php +++ b/HttpServer/Http/Request.php @@ -8,12 +8,11 @@ use Exception; use HttpServer\Abstracts\HttpService; use HttpServer\IInterface\AuthIdentity; use JetBrains\PhpStorm\Pure; -use ReflectionException; use Snowflake\Abstracts\Config; use Snowflake\Core\ArrayAccess; +use Snowflake\Core\Help; use Snowflake\Core\Json; use Snowflake\Exception\ConfigException; -use Snowflake\Exception\NotFindClassException; use Snowflake\Snowflake; use function router; @@ -452,20 +451,21 @@ class Request extends HttpService /** - * @param $request + * @param \Swoole\Http\Request $request * @return mixed */ - public static function create($request): Request + public static function create(\Swoole\Http\Request $request): Request { /** @var Request $sRequest */ $sRequest = Context::setContext('request', new Request()); $sRequest->fd = $request->fd; $sRequest->startTime = microtime(true); - $sRequest->params = new HttpParams($request->rawContent(), $request->get, $request->files); + $sRequest->params = new HttpParams(Help::toArray($request->rawContent()), $request->get, $request->files); if (!empty($request->post)) { $sRequest->params->setPosts($request->post ?? []); } + $sRequest->headers = new HttpHeaders(ArrayAccess::merge($request->server, $request->header)); $sRequest->uri = $sRequest->headers->get('request_uri'); @@ -535,20 +535,15 @@ class Request extends HttpService return $this; } - $data = $this->params->getBody(); - if (is_string($data) && is_null($data = Json::decode($data))) { + [$cmd, $repeat, $body] = explode("\n", $this->params->getBody()); + if (is_null($body) || is_null($cmd) || !empty($repeat)) { throw new Exception('Protocol format error.'); } - if (!isset($data['cmd']) || empty(!isset($data['cmd']))) { - throw new Exception('Unknown system cmd.'); + if (is_string($body) && is_null($data = Json::decode($body))) { + throw new Exception('Protocol format error.'); } - - if (str_starts_with($data['cmd'], '/')) { - $data['cmd'] = ltrim($data['cmd'], '/'); - } - - $this->headers->setRequestUri('rpc/p' . $rpc . '/' . $data['cmd']); + $this->headers->setRequestUri('rpc/p' . $rpc . '/' . ltrim($cmd, '/')); $this->headers->setRequestMethod(Request::HTTP_CMD); return $this; From 734b782c0e6556072ac03305387041b01dc9406c Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sat, 1 May 2021 18:26:36 +0800 Subject: [PATCH 02/32] modify --- System/Jwt/Jwt.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/System/Jwt/Jwt.php b/System/Jwt/Jwt.php index 435940ea..f61918e1 100644 --- a/System/Jwt/Jwt.php +++ b/System/Jwt/Jwt.php @@ -461,6 +461,9 @@ mlAZUEjsoaT9vjvjGTxl3uCm0TX5KTgtSJIt2kA1tYVjQef+/iZTHxY= */ public function expireRefresh($token = null, $source = null) { + if (!isset($this->data['token'])) { + return; + } if (!empty($token)) { $this->data['token'] = $token; } From 9cbbfbe86feabb97eff609556928e911e18874f0 Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sat, 1 May 2021 18:28:10 +0800 Subject: [PATCH 03/32] modify --- System/Jwt/Jwt.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/System/Jwt/Jwt.php b/System/Jwt/Jwt.php index f61918e1..5b3589eb 100644 --- a/System/Jwt/Jwt.php +++ b/System/Jwt/Jwt.php @@ -461,15 +461,15 @@ mlAZUEjsoaT9vjvjGTxl3uCm0TX5KTgtSJIt2kA1tYVjQef+/iZTHxY= */ public function expireRefresh($token = null, $source = null) { - if (!isset($this->data['token'])) { - return; - } if (!empty($token)) { $this->data['token'] = $token; } if (!empty($source)) { $this->data['source'] = $source; } + if (!isset($this->data['token'])) { + return; + } $key = $this->authKey($this->getSource(), $this->data['token']); $this->getRedis()->expire($key, $this->timeout); } From e0a96b8e5a538f906b71797e7a889db9ab60e686 Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sun, 2 May 2021 00:13:52 +0800 Subject: [PATCH 04/32] modify --- System/Channel.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/System/Channel.php b/System/Channel.php index 8e3c764f..e8cc7e4e 100644 --- a/System/Channel.php +++ b/System/Channel.php @@ -28,6 +28,7 @@ class Channel extends Component */ public function push(mixed $value, string $name = ''): void { + return; $channel = $this->channelInit($name); $channel->enqueue($value); } @@ -73,6 +74,7 @@ class Channel extends Component */ public function pop(string $name, Closure $closure, int|float $timeout = null): mixed { + return call_user_func($closure); if (($channel = $this->channelInit($name)) == false) { return $this->addError('Channel is full.'); } From cd4aad83de6f7795e999247cb23cb24d468e164d Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sun, 2 May 2021 00:14:08 +0800 Subject: [PATCH 05/32] modify --- System/Channel.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/System/Channel.php b/System/Channel.php index e8cc7e4e..8e3c764f 100644 --- a/System/Channel.php +++ b/System/Channel.php @@ -28,7 +28,6 @@ class Channel extends Component */ public function push(mixed $value, string $name = ''): void { - return; $channel = $this->channelInit($name); $channel->enqueue($value); } @@ -74,7 +73,6 @@ class Channel extends Component */ public function pop(string $name, Closure $closure, int|float $timeout = null): mixed { - return call_user_func($closure); if (($channel = $this->channelInit($name)) == false) { return $this->addError('Channel is full.'); } From 52f637ea25b71befe6625403ba3f77624d546216 Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sun, 2 May 2021 04:12:31 +0800 Subject: [PATCH 06/32] modify --- Annotation/Loader.php | 15 ++++++++------- HttpServer/Events/OnWorkerStart.php | 21 +++++++++++++++++---- HttpServer/Server.php | 6 +++++- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/Annotation/Loader.php b/Annotation/Loader.php index 6224f36e..1d696581 100644 --- a/Annotation/Loader.php +++ b/Annotation/Loader.php @@ -179,7 +179,7 @@ class Loader extends BaseObject } $this->appendFileToDirectory($path->getRealPath(), $replace->getName()); - $_array = ['handler' => $replace->newInstance(), 'target' => [], 'methods' => [], 'property' => []]; + $_array = ['handler' => $replace->getName(), 'target' => [], 'methods' => [], 'property' => []]; foreach ($replace->getAttributes() as $attribute) { if ($attribute->getName() == Attribute::class) { continue; @@ -401,21 +401,22 @@ class Loader extends BaseObject if ($annotations === null) { continue; } + + $target = new $annotations['handler'](); foreach ($annotations['target'] ?? [] as $value) { - $value->execute([$annotations['handler']]); + $value->execute([$target]); } - $_className = $annotations['handler']::class; foreach ($annotations['methods'] as $name => $attribute) { foreach ($attribute as $value) { if ($value instanceof Relation) { - $annotation->addRelate($_className, $value->name, $name); + $annotation->addRelate($target::class, $value->name, $name); } else if ($value instanceof Get) { - $annotation->addGets($_className, $value->name, $name); + $annotation->addGets($target::class, $value->name, $name); } else if ($value instanceof Set) { - $annotation->addSets($_className, $value->name, $name); + $annotation->addSets($target::class, $value->name, $name); } else { - $value->execute([$annotations['handler'], $name]); + $value->execute([$target, $name]); } } } diff --git a/HttpServer/Events/OnWorkerStart.php b/HttpServer/Events/OnWorkerStart.php index 8c47ad1a..7e992c3e 100644 --- a/HttpServer/Events/OnWorkerStart.php +++ b/HttpServer/Events/OnWorkerStart.php @@ -9,6 +9,7 @@ use HttpServer\Abstracts\Callback; use Snowflake\Event; use Snowflake\Exception\ConfigException; use Snowflake\Snowflake; +use Swoole\Coroutine; use Swoole\Coroutine\System; use Swoole\Server; @@ -32,10 +33,7 @@ class OnWorkerStart extends Callback putenv('state=start'); putenv('worker=' . $worker_id); - $content = System::readFile(storage('runtime.php')); - - $annotation = Snowflake::app()->getAnnotation(); - $annotation->setLoader(unserialize($content)); + $annotation = $this->settings(); if ($worker_id < $server->setting['worker_num']) { $this->onWorker($server, $annotation); } else { @@ -44,6 +42,21 @@ class OnWorkerStart extends Callback } + /** + * @return \Annotation\Annotation + * @throws \Exception + */ + private function settings(): Annotation + { + $content = System::readFile(storage('runtime.php')); + + $annotation = Snowflake::app()->getAnnotation(); + $annotation->setLoader(unserialize($content)); + + return $annotation; + } + + /** * @param Server $server * @param int $worker_id diff --git a/HttpServer/Server.php b/HttpServer/Server.php index 0a19bb7d..db90cb5a 100644 --- a/HttpServer/Server.php +++ b/HttpServer/Server.php @@ -115,7 +115,11 @@ class Server extends HttpService Runtime::enableCoroutine(true, SWOOLE_HOOK_ALL ^ SWOOLE_HOOK_BLOCKING_FUNCTION); - Coroutine::set(['enable_deadlock_check' => false]); + $settings['enable_deadlock_check'] = false; + $settings['exit_condition'] = function () { + return Coroutine::stats()['coroutine_num'] === 0; + }; + Coroutine::set($settings); return $this->execute($baseServer); } From 291ad9870df5ff3a2811f6f238bab43dbb7375db Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sun, 2 May 2021 04:18:06 +0800 Subject: [PATCH 07/32] modify --- Annotation/Loader.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/Annotation/Loader.php b/Annotation/Loader.php index 1d696581..6224f36e 100644 --- a/Annotation/Loader.php +++ b/Annotation/Loader.php @@ -179,7 +179,7 @@ class Loader extends BaseObject } $this->appendFileToDirectory($path->getRealPath(), $replace->getName()); - $_array = ['handler' => $replace->getName(), 'target' => [], 'methods' => [], 'property' => []]; + $_array = ['handler' => $replace->newInstance(), 'target' => [], 'methods' => [], 'property' => []]; foreach ($replace->getAttributes() as $attribute) { if ($attribute->getName() == Attribute::class) { continue; @@ -401,22 +401,21 @@ class Loader extends BaseObject if ($annotations === null) { continue; } - - $target = new $annotations['handler'](); foreach ($annotations['target'] ?? [] as $value) { - $value->execute([$target]); + $value->execute([$annotations['handler']]); } + $_className = $annotations['handler']::class; foreach ($annotations['methods'] as $name => $attribute) { foreach ($attribute as $value) { if ($value instanceof Relation) { - $annotation->addRelate($target::class, $value->name, $name); + $annotation->addRelate($_className, $value->name, $name); } else if ($value instanceof Get) { - $annotation->addGets($target::class, $value->name, $name); + $annotation->addGets($_className, $value->name, $name); } else if ($value instanceof Set) { - $annotation->addSets($target::class, $value->name, $name); + $annotation->addSets($_className, $value->name, $name); } else { - $value->execute([$target, $name]); + $value->execute([$annotations['handler'], $name]); } } } From 72965758e98e50e36dcf4d00e08ac526b5a760b6 Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sun, 2 May 2021 04:18:32 +0800 Subject: [PATCH 08/32] modify --- Annotation/Loader.php | 1 + 1 file changed, 1 insertion(+) diff --git a/Annotation/Loader.php b/Annotation/Loader.php index 6224f36e..247ede8b 100644 --- a/Annotation/Loader.php +++ b/Annotation/Loader.php @@ -419,6 +419,7 @@ class Loader extends BaseObject } } } + unset($this->_classes[$className]); } } From f831fa24103e95d13b05d106e1ffa408265a65b7 Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sun, 2 May 2021 04:27:19 +0800 Subject: [PATCH 09/32] modify --- Annotation/Loader.php | 41 +++++++++++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/Annotation/Loader.php b/Annotation/Loader.php index 247ede8b..33ea34ac 100644 --- a/Annotation/Loader.php +++ b/Annotation/Loader.php @@ -179,7 +179,7 @@ class Loader extends BaseObject } $this->appendFileToDirectory($path->getRealPath(), $replace->getName()); - $_array = ['handler' => $replace->newInstance(), 'target' => [], 'methods' => [], 'property' => []]; + $_array = ['handler' => $replace->getName(), 'target' => [], 'methods' => [], 'property' => []]; foreach ($replace->getAttributes() as $attribute) { if ($attribute->getName() == Attribute::class) { continue; @@ -395,27 +395,31 @@ class Loader extends BaseObject if (empty($classes)) { return; } - $annotation = Snowflake::app()->getAnnotation(); + $annotation = Snowflake::getAnnotation(); + + foreach ($classes as $className) { $annotations = $this->_classes[$className] ?? null; if ($annotations === null) { continue; } - foreach ($annotations['target'] ?? [] as $value) { - $value->execute([$annotations['handler']]); + if (($reflect = $this->getRelect($annotations)) === null) { + continue; + } + $reflect = $reflect->newInstance(); + foreach ($annotations['target'] ?? [] as $value) { + $value->execute([$reflect]); } - - $_className = $annotations['handler']::class; foreach ($annotations['methods'] as $name => $attribute) { foreach ($attribute as $value) { if ($value instanceof Relation) { - $annotation->addRelate($_className, $value->name, $name); + $annotation->addRelate($reflect->getName(), $value->name, $name); } else if ($value instanceof Get) { - $annotation->addGets($_className, $value->name, $name); + $annotation->addGets($reflect->getName(), $value->name, $name); } else if ($value instanceof Set) { - $annotation->addSets($_className, $value->name, $name); + $annotation->addSets($reflect->getName(), $value->name, $name); } else { - $value->execute([$annotations['handler'], $name]); + $value->execute([$reflect, $name]); } } } @@ -423,4 +427,21 @@ class Loader extends BaseObject } } + + /** + * @param $annotations + * @return \ReflectionClass|null + * @throws \ReflectionException + * @throws \Snowflake\Exception\NotFindClassException + */ + private function getRelect($annotations) + { + $reflect = Snowflake::getDi()->getReflect($annotations['handler']); + if ($reflect === null) { + return null; + } + return $reflect; + } + + } From 78980aebe22792655066788ce1409668fe9dd2cf Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sun, 2 May 2021 04:28:05 +0800 Subject: [PATCH 10/32] modify --- Annotation/Loader.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Annotation/Loader.php b/Annotation/Loader.php index 33ea34ac..a3bf004f 100644 --- a/Annotation/Loader.php +++ b/Annotation/Loader.php @@ -406,9 +406,9 @@ class Loader extends BaseObject if (($reflect = $this->getRelect($annotations)) === null) { continue; } - $reflect = $reflect->newInstance(); + $class = $reflect->newInstance(); foreach ($annotations['target'] ?? [] as $value) { - $value->execute([$reflect]); + $value->execute([$class]); } foreach ($annotations['methods'] as $name => $attribute) { foreach ($attribute as $value) { @@ -419,7 +419,7 @@ class Loader extends BaseObject } else if ($value instanceof Set) { $annotation->addSets($reflect->getName(), $value->name, $name); } else { - $value->execute([$reflect, $name]); + $value->execute([$class, $name]); } } } From 3ef6bf95489c08a4259d2161235e9bd97ff8abac Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sun, 2 May 2021 04:31:53 +0800 Subject: [PATCH 11/32] modify --- Annotation/Loader.php | 93 +------------------------------------------ 1 file changed, 1 insertion(+), 92 deletions(-) diff --git a/Annotation/Loader.php b/Annotation/Loader.php index a3bf004f..c20d8c71 100644 --- a/Annotation/Loader.php +++ b/Annotation/Loader.php @@ -279,98 +279,6 @@ class Loader extends BaseObject $array = '/' . trim(implode('/', $array), '/'); $this->_directory[$array][] = $className; - -// $directory = $this->splitDirectory($filePath); -// array_pop($directory); -// -// $tree = null; -// foreach ($directory as $value) { -// $tree = $this->getTree($tree, $value); -// } -// -// if ($tree instanceof FileTree) { -// $tree->addFile($className, $filePath); -// } - } - - - /** - * @param string $filePath - * @param string|null $outPath - * @return $this - * @throws Exception - */ - private function each(string $filePath, ?string $outPath): static - { - $tree = null; - $directory = $this->splitDirectory($filePath); - - $_tmp = ''; - if (!empty($outPath)) { - $outPath = rtrim($outPath, '/'); - } - - foreach ($directory as $key => $value) { - $_tmp .= DIRECTORY_SEPARATOR . $value; - if (!empty($outPath) && str_contains($_tmp, $outPath)) { - break; - } - $tree = $this->getTree($tree, $value); - } - if ($tree instanceof FileTree) { - $this->eachNode($tree->getChildes(), $outPath); - $this->execute($tree->getFiles()); - } - return $this; - } - - - /** - * @param string $filePath - * @return false|string[] - */ - private function splitDirectory(string $filePath): array|bool - { - $DIRECTORY = explode(DIRECTORY_SEPARATOR, $filePath); - return array_filter($DIRECTORY, function ($value) { - return !empty($value); - }); - } - - - /** - * @param $tree - * @param $value - * @return FileTree - */ - private function getTree($tree, $value): FileTree - { - if ($tree === null) { - $tree = $this->files->getChild($value); - } else { - $tree = $tree->getChild($value); - } - return $tree; - } - - - /** - * @param FileTree[] $nodes - * @param string|null $outPath - * @throws Exception - */ - private function eachNode(array $nodes, ?string $outPath = '') - { - foreach ($nodes as $node) { - $this->execute($node->getFiles()); - if (!empty($outPath) && str_contains($node->getDirPath(), $outPath)) { - continue; - } - $childes = $node->getChildes(); - if (!empty($childes)) { - $this->eachNode($childes, $outPath); - } - } } @@ -406,6 +314,7 @@ class Loader extends BaseObject if (($reflect = $this->getRelect($annotations)) === null) { continue; } + var_export($reflect); $class = $reflect->newInstance(); foreach ($annotations['target'] ?? [] as $value) { $value->execute([$class]); From 96cbb09b288e6017d1c99ec353584593425838f9 Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sun, 2 May 2021 04:32:18 +0800 Subject: [PATCH 12/32] modify --- Annotation/Loader.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Annotation/Loader.php b/Annotation/Loader.php index c20d8c71..04ad5b44 100644 --- a/Annotation/Loader.php +++ b/Annotation/Loader.php @@ -311,10 +311,10 @@ class Loader extends BaseObject if ($annotations === null) { continue; } + var_export($annotations); if (($reflect = $this->getRelect($annotations)) === null) { continue; } - var_export($reflect); $class = $reflect->newInstance(); foreach ($annotations['target'] ?? [] as $value) { $value->execute([$class]); From ed570b3e3db1c42373d16845917cf492b7464ccf Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sun, 2 May 2021 04:32:57 +0800 Subject: [PATCH 13/32] modify --- Annotation/Loader.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Annotation/Loader.php b/Annotation/Loader.php index 04ad5b44..4a813c4d 100644 --- a/Annotation/Loader.php +++ b/Annotation/Loader.php @@ -235,6 +235,7 @@ class Loader extends BaseObject $path = '/' . trim($path, '/'); foreach ($this->_directory as $key => $_path) { $key = '/' . trim($key, '/'); + var_export($key); if (!str_starts_with($key, $path)) { continue; } @@ -304,8 +305,6 @@ class Loader extends BaseObject return; } $annotation = Snowflake::getAnnotation(); - - foreach ($classes as $className) { $annotations = $this->_classes[$className] ?? null; if ($annotations === null) { From fa0c4bd5dd0738a03f50492083cc11467528a1e9 Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sun, 2 May 2021 04:34:12 +0800 Subject: [PATCH 14/32] modify --- Annotation/Loader.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Annotation/Loader.php b/Annotation/Loader.php index 4a813c4d..a8790ddf 100644 --- a/Annotation/Loader.php +++ b/Annotation/Loader.php @@ -235,13 +235,13 @@ class Loader extends BaseObject $path = '/' . trim($path, '/'); foreach ($this->_directory as $key => $_path) { $key = '/' . trim($key, '/'); - var_export($key); if (!str_starts_with($key, $path)) { continue; } if (in_array($key, $outPath)) { continue; } + var_export($_path); $this->execute($_path); } } catch (Throwable $exception) { From de1c0be1a491773705e73c77a8eaedc26c364efa Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sun, 2 May 2021 04:35:16 +0800 Subject: [PATCH 15/32] modify --- Annotation/Loader.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Annotation/Loader.php b/Annotation/Loader.php index a8790ddf..7d4ad84e 100644 --- a/Annotation/Loader.php +++ b/Annotation/Loader.php @@ -241,7 +241,6 @@ class Loader extends BaseObject if (in_array($key, $outPath)) { continue; } - var_export($_path); $this->execute($_path); } } catch (Throwable $exception) { @@ -307,10 +306,10 @@ class Loader extends BaseObject $annotation = Snowflake::getAnnotation(); foreach ($classes as $className) { $annotations = $this->_classes[$className] ?? null; + var_export($annotations); if ($annotations === null) { continue; } - var_export($annotations); if (($reflect = $this->getRelect($annotations)) === null) { continue; } @@ -331,7 +330,6 @@ class Loader extends BaseObject } } } - unset($this->_classes[$className]); } } From a906ac187c2916f0f29673e66e7fb907e6db7ac5 Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sun, 2 May 2021 04:35:56 +0800 Subject: [PATCH 16/32] modify --- Annotation/Loader.php | 1 - 1 file changed, 1 deletion(-) diff --git a/Annotation/Loader.php b/Annotation/Loader.php index 7d4ad84e..b708845d 100644 --- a/Annotation/Loader.php +++ b/Annotation/Loader.php @@ -306,7 +306,6 @@ class Loader extends BaseObject $annotation = Snowflake::getAnnotation(); foreach ($classes as $className) { $annotations = $this->_classes[$className] ?? null; - var_export($annotations); if ($annotations === null) { continue; } From c44fb76b598dba16c3b15cbbc236f2b9b46f1c96 Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sun, 2 May 2021 04:38:26 +0800 Subject: [PATCH 17/32] modify --- Annotation/Loader.php | 29 +++++------------------------ 1 file changed, 5 insertions(+), 24 deletions(-) diff --git a/Annotation/Loader.php b/Annotation/Loader.php index b708845d..097dae56 100644 --- a/Annotation/Loader.php +++ b/Annotation/Loader.php @@ -179,7 +179,7 @@ class Loader extends BaseObject } $this->appendFileToDirectory($path->getRealPath(), $replace->getName()); - $_array = ['handler' => $replace->getName(), 'target' => [], 'methods' => [], 'property' => []]; + $_array = ['handler' => $replace->newInstance(), 'target' => [], 'methods' => [], 'property' => []]; foreach ($replace->getAttributes() as $attribute) { if ($attribute->getName() == Attribute::class) { continue; @@ -309,21 +309,18 @@ class Loader extends BaseObject if ($annotations === null) { continue; } - if (($reflect = $this->getRelect($annotations)) === null) { - continue; - } - $class = $reflect->newInstance(); + $class = clone $annotations['handler']; foreach ($annotations['target'] ?? [] as $value) { $value->execute([$class]); } foreach ($annotations['methods'] as $name => $attribute) { foreach ($attribute as $value) { if ($value instanceof Relation) { - $annotation->addRelate($reflect->getName(), $value->name, $name); + $annotation->addRelate($class::class, $value->name, $name); } else if ($value instanceof Get) { - $annotation->addGets($reflect->getName(), $value->name, $name); + $annotation->addGets($class::class, $value->name, $name); } else if ($value instanceof Set) { - $annotation->addSets($reflect->getName(), $value->name, $name); + $annotation->addSets($class::class, $value->name, $name); } else { $value->execute([$class, $name]); } @@ -333,20 +330,4 @@ class Loader extends BaseObject } - /** - * @param $annotations - * @return \ReflectionClass|null - * @throws \ReflectionException - * @throws \Snowflake\Exception\NotFindClassException - */ - private function getRelect($annotations) - { - $reflect = Snowflake::getDi()->getReflect($annotations['handler']); - if ($reflect === null) { - return null; - } - return $reflect; - } - - } From 77399fa68ff1cca6caccf012ff89151553382c65 Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sun, 2 May 2021 04:46:21 +0800 Subject: [PATCH 18/32] modify --- Annotation/Loader.php | 5 +---- System/Channel.php | 14 ++++---------- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/Annotation/Loader.php b/Annotation/Loader.php index 097dae56..1049785d 100644 --- a/Annotation/Loader.php +++ b/Annotation/Loader.php @@ -235,10 +235,7 @@ class Loader extends BaseObject $path = '/' . trim($path, '/'); foreach ($this->_directory as $key => $_path) { $key = '/' . trim($key, '/'); - if (!str_starts_with($key, $path)) { - continue; - } - if (in_array($key, $outPath)) { + if (!str_starts_with($key, $path) || in_array($key, $outPath)) { continue; } $this->execute($_path); diff --git a/System/Channel.php b/System/Channel.php index 8e3c764f..b79ccfa2 100644 --- a/System/Channel.php +++ b/System/Channel.php @@ -71,21 +71,15 @@ class Channel extends Component * @return mixed * @throws Exception */ - public function pop(string $name, Closure $closure, int|float $timeout = null): mixed + public function pop(string $name, Closure $closure): mixed { if (($channel = $this->channelInit($name)) == false) { return $this->addError('Channel is full.'); } - if (!$channel->isEmpty()) { - return $channel->shift(); + if ($channel->isEmpty()) { + return call_user_func($closure); } - if ($timeout !== null) { - $data = $channel->dequeue(); - } - if (empty($data)) { - $data = call_user_func($closure); - } - return $data; + return $channel->dequeue(); } From 3d8befe6c096f3d57d4e12f27e14d929ff3cb36c Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sun, 2 May 2021 04:50:18 +0800 Subject: [PATCH 19/32] modify --- Database/ActiveRecord.php | 718 +++++++++++++++++++------------------- 1 file changed, 359 insertions(+), 359 deletions(-) diff --git a/Database/ActiveRecord.php b/Database/ActiveRecord.php index c1d79098..bd1b493a 100644 --- a/Database/ActiveRecord.php +++ b/Database/ActiveRecord.php @@ -32,418 +32,418 @@ defined('FIND_OR_CREATE_MESSAGE') or define('FIND_OR_CREATE_MESSAGE', 'Create a class ActiveRecord extends BaseActiveRecord { - const DECR = 'decr'; - const INCR = 'incr'; + const DECR = 'decr'; + const INCR = 'incr'; - /** - * @return array - */ - public function rules(): array - { - return []; - } + /** + * @return array + */ + public function rules(): array + { + return []; + } - /** - * @param string $column - * @param int $value - * @return ActiveRecord|false - * @throws Exception - */ - public function increment(string $column, int $value): bool|ActiveRecord - { - if (!$this->mathematics([$column => $value], '+')) { - return false; - } - $this->{$column} += $value; - return $this->refresh(); - } + /** + * @param string $column + * @param int $value + * @return ActiveRecord|false + * @throws Exception + */ + public function increment(string $column, int $value): bool|ActiveRecord + { + if (!$this->mathematics([$column => $value], '+')) { + return false; + } + $this->{$column} += $value; + return $this->refresh(); + } - /** - * @param string $column - * @param int $value - * @return ActiveRecord|false - * @throws Exception - */ - public function decrement(string $column, int $value): bool|ActiveRecord - { - if (!$this->mathematics([$column => $value], '-')) { - return false; - } - $this->{$column} -= $value; - return $this->refresh(); - } + /** + * @param string $column + * @param int $value + * @return ActiveRecord|false + * @throws Exception + */ + public function decrement(string $column, int $value): bool|ActiveRecord + { + if (!$this->mathematics([$column => $value], '-')) { + return false; + } + $this->{$column} -= $value; + return $this->refresh(); + } - /** - * @param array $columns - * @return ActiveRecord|false - * @throws Exception - */ - public function increments(array $columns): bool|static - { - if (!$this->mathematics($columns, '+')) { - return false; - } - foreach ($columns as $key => $attribute) { - $this->$key += $attribute; - } - return $this; - } + /** + * @param array $columns + * @return ActiveRecord|false + * @throws Exception + */ + public function increments(array $columns): bool|static + { + if (!$this->mathematics($columns, '+')) { + return false; + } + foreach ($columns as $key => $attribute) { + $this->$key += $attribute; + } + return $this; + } - /** - * @param array $columns - * @return ActiveRecord|false - * @throws Exception - */ - public function decrements(array $columns): bool|static - { - if (!$this->mathematics($columns, '-')) { - return false; - } - foreach ($columns as $key => $attribute) { - $this->$key -= $attribute; - } - return $this; - } + /** + * @param array $columns + * @return ActiveRecord|false + * @throws Exception + */ + public function decrements(array $columns): bool|static + { + if (!$this->mathematics($columns, '-')) { + return false; + } + foreach ($columns as $key => $attribute) { + $this->$key -= $attribute; + } + return $this; + } - /** - * @param array $condition - * @param array $attributes - * @return bool|ActiveRecord - * @throws ReflectionException - * @throws NotFindClassException - * @throws Exception - */ - public static function findOrCreate(array $condition, array $attributes = []): bool|static - { - $logger = Snowflake::app()->getLogger(); + /** + * @param array $condition + * @param array $attributes + * @return bool|ActiveRecord + * @throws ReflectionException + * @throws NotFindClassException + * @throws Exception + */ + public static function findOrCreate(array $condition, array $attributes = []): bool|static + { + $logger = Snowflake::app()->getLogger(); - /** @var static $select */ - $select = static::find()->where($condition)->first(); - if (!empty($select)) { - return $select; - } - if (empty($attributes)) { - return $logger->addError(FIND_OR_CREATE_MESSAGE, 'mysql'); - } - $select = self::getModelClass(); - $select->attributes = $attributes; - if (!$select->save()) { - return $logger->addError($select->getLastError(), 'mysql'); - } - return $select; - } + /** @var static $select */ + $select = static::find()->where($condition)->first(); + if (!empty($select)) { + return $select; + } + if (empty($attributes)) { + return $logger->addError(FIND_OR_CREATE_MESSAGE, 'mysql'); + } + $select = self::getModelClass(); + $select->attributes = $attributes; + if (!$select->save()) { + return $logger->addError($select->getLastError(), 'mysql'); + } + return $select; + } - /** - * @param array $condition - * @param array $attributes - * @return bool|static - * @throws Exception - */ - public static function createOrUpdate(array $condition, array $attributes = []): bool|static - { - $logger = Snowflake::app()->getLogger(); - if (empty($attributes)) { - return $logger->addError(FIND_OR_CREATE_MESSAGE, 'mysql'); - } - /** @var static $select */ - $select = static::find()->where($condition)->first(); - if (empty($select)) { - $select = self::getModelClass(); - } - $select->attributes = $attributes; - if (!$select->save()) { - return $logger->addError($select->getLastError(), 'mysql'); - } - return $select; - } + /** + * @param array $condition + * @param array $attributes + * @return bool|static + * @throws Exception + */ + public static function createOrUpdate(array $condition, array $attributes = []): bool|static + { + $logger = Snowflake::app()->getLogger(); + if (empty($attributes)) { + return $logger->addError(FIND_OR_CREATE_MESSAGE, 'mysql'); + } + /** @var static $select */ + $select = static::find()->where($condition)->first(); + if (empty($select)) { + $select = self::getModelClass(); + } + $select->attributes = $attributes; + if (!$select->save()) { + return $logger->addError($select->getLastError(), 'mysql'); + } + return $select; + } - /** - * @return static - * @throws Exception - */ - private static function getModelClass(): static - { - /** @var Channel $channel */ - $channel = Snowflake::app()->get('channel'); - return $channel->pop(static::class, function () { - return new static(); - }); - } + /** + * @return static + * @throws Exception + */ + private static function getModelClass(): static + { + /** @var Channel $channel */ + $channel = Snowflake::app()->get('channel'); + return $channel->pop(static::class, function () { + return new static(); + }); + } - /** - * @param $action - * @param $columns - * @param array $condition - * @return array|bool|int|string|null - * @throws Exception - */ - private function mathematics($columns, $action, $condition = []): int|bool|array|string|null - { - if (empty($condition)) { - $condition = [$this->getPrimary() => $this->getPrimaryValue()]; - } + /** + * @param $action + * @param $columns + * @param array $condition + * @return array|bool|int|string|null + * @throws Exception + */ + private function mathematics($columns, $action, $condition = []): int|bool|array|string|null + { + if (empty($condition)) { + $condition = [$this->getPrimary() => $this->getPrimaryValue()]; + } - $activeQuery = static::find()->where($condition); - $create = SqlBuilder::builder($activeQuery)->mathematics($columns, $action); - if (is_bool($create)) { - return false; - } - return static::getDb()->createCommand($create[0], $create[1])->exec(); - } + $activeQuery = static::find()->where($condition); + $create = SqlBuilder::builder($activeQuery)->mathematics($columns, $action); + if (is_bool($create)) { + return false; + } + return static::getDb()->createCommand($create[0], $create[1])->exec(); + } - /** - * @param array $fields - * @return ActiveRecord|bool - * @throws Exception - */ - public function update(array $fields): static|bool - { - return $this->save($fields); - } + /** + * @param array $fields + * @return ActiveRecord|bool + * @throws Exception + */ + public function update(array $fields): static|bool + { + return $this->save($fields); + } - /** - * @param array $data - * @return bool - * @throws Exception - */ - public static function inserts(array $data): bool - { - /** @var static $class */ - $class = Snowflake::createObject(['class' => static::class]); - if (empty($data)) { - return $class->addError('Insert data empty.', 'mysql'); - } - return $class::find()->batchInsert($data); - } + /** + * @param array $data + * @return bool + * @throws Exception + */ + public static function inserts(array $data): bool + { + /** @var static $class */ + $class = Snowflake::createObject(['class' => static::class]); + if (empty($data)) { + return $class->addError('Insert data empty.', 'mysql'); + } + return $class::find()->batchInsert($data); + } - /** - * @return bool - * @throws Exception - */ - public function delete(): bool - { - $conditions = $this->_oldAttributes; - if (empty($conditions)) { - return $this->addError("Delete condition do not empty.", 'mysql'); - } - $primary = $this->getPrimary(); + /** + * @return bool + * @throws Exception + */ + public function delete(): bool + { + $conditions = $this->_oldAttributes; + if (empty($conditions)) { + return $this->addError("Delete condition do not empty.", 'mysql'); + } + $primary = $this->getPrimary(); - if (!empty($primary)) { - $conditions = [$primary => $this->getAttribute($primary)]; - } - return static::deleteByCondition($conditions); - } + if (!empty($primary)) { + $conditions = [$primary => $this->getAttribute($primary)]; + } + return static::deleteByCondition($conditions); + } - /** - * @param $condition - * @param array $attributes - * - * @return bool - * @throws Exception - */ - public static function updateAll(mixed $condition, $attributes = []): bool - { - $condition = static::find()->where($condition); - return $condition->batchUpdate($attributes); - } + /** + * @param $condition + * @param array $attributes + * + * @return bool + * @throws Exception + */ + public static function updateAll(mixed $condition, $attributes = []): bool + { + $condition = static::find()->where($condition); + return $condition->batchUpdate($attributes); + } - /** - * @param $condition - * @param array $attributes - * - * @return array|Collection - * @throws Exception - */ - public static function findAll($condition, $attributes = []): array|Collection - { - $query = static::find()->where($condition); - if (!empty($attributes)) { - $query->bindParams($attributes); - } - return $query->all(); - } + /** + * @param $condition + * @param array $attributes + * + * @return array|Collection + * @throws Exception + */ + public static function findAll($condition, $attributes = []): array|Collection + { + $query = static::find()->where($condition); + if (!empty($attributes)) { + $query->bindParams($attributes); + } + return $query->all(); + } - /** - * @param $method - * @return mixed - * @throws Exception - */ - private function resolveObject($method): mixed - { - $resolve = $this->{$this->getRelate($method)}(); - if ($resolve instanceof HasBase) { - $resolve = $resolve->get(); - } - if ($resolve instanceof Collection) { - return $resolve->toArray(); - } else if ($resolve instanceof ActiveRecord) { - return $resolve->toArray(); - } else if (is_object($resolve)) { - return get_object_vars($resolve); - } else { - return $resolve; - } - } + /** + * @param $method + * @return mixed + * @throws Exception + */ + private function resolveObject($method): mixed + { + $resolve = $this->{$this->getRelate($method)}(); + if ($resolve instanceof HasBase) { + $resolve = $resolve->get(); + } + if ($resolve instanceof Collection) { + return $resolve->toArray(); + } else if ($resolve instanceof ActiveRecord) { + return $resolve->toArray(); + } else if (is_object($resolve)) { + return get_object_vars($resolve); + } else { + return $resolve; + } + } - /** - * @return array - * @throws Exception - */ - public function toArray(): array - { - $data = $this->_attributes; + /** + * @return array + * @throws Exception + */ + public function toArray(): array + { + $data = $this->_attributes; - $lists = Snowflake::getAnnotation()->getGets(static::class); - foreach ($lists as $key => $item) { - $data[$key] = $this->{$item}($data[$key] ?? null); - } - $data = array_merge($data, $this->runRelate()); + $lists = Snowflake::getAnnotation()->getGets(static::class); + foreach ($lists as $key => $item) { + $data[$key] = $this->{$item}($data[$key] ?? null); + } + $data = array_merge($data, $this->runRelate()); - $class = Snowflake::app()->getChannel(); - $class->push($this, static::class); + $class = Snowflake::app()->getChannel(); + $class->push($this, static::class); - return $data; - } + return $data; + } - /** - * @return array - * @throws Exception - */ - private function runRelate(): array - { - $relates = []; - if (empty($with = $this->getWith())) { - return $relates; - } - foreach ($with as $val) { - $relates[$val] = $this->resolveObject($val); - } - return $relates; - } + /** + * @return array + * @throws Exception + */ + private function runRelate(): array + { + $relates = []; + if (empty($with = $this->getWith())) { + return $relates; + } + foreach ($with as $val) { + $relates[$val] = $this->resolveObject($val); + } + return $relates; + } - /** - * @param string $modelName - * @param $foreignKey - * @param $localKey - * @return HasOne|ActiveQuery - * @throws Exception - */ - public function hasOne(string $modelName, $foreignKey, $localKey): HasOne|ActiveQuery - { - if (!$this->has($localKey)) { - throw new Exception("Need join table primary key."); - } + /** + * @param string $modelName + * @param $foreignKey + * @param $localKey + * @return HasOne|ActiveQuery + * @throws Exception + */ + public function hasOne(string $modelName, $foreignKey, $localKey): HasOne|ActiveQuery + { + if (!$this->has($localKey)) { + throw new Exception("Need join table primary key."); + } - $value = $this->getAttribute($localKey); + $value = $this->getAttribute($localKey); - $relation = $this->getRelation(); + $relation = $this->getRelation(); - return new HasOne($modelName, $foreignKey, $value, $relation); - } + return new HasOne($modelName, $foreignKey, $value, $relation); + } - /** - * @param $modelName - * @param $foreignKey - * @param $localKey - * @return ActiveQuery - * @throws Exception - */ - public function hasCount($modelName, $foreignKey, $localKey): mixed - { - if (!$this->has($localKey)) { - throw new Exception("Need join table primary key."); - } + /** + * @param $modelName + * @param $foreignKey + * @param $localKey + * @return ActiveQuery + * @throws Exception + */ + public function hasCount($modelName, $foreignKey, $localKey): mixed + { + if (!$this->has($localKey)) { + throw new Exception("Need join table primary key."); + } - $value = $this->getAttribute($localKey); + $value = $this->getAttribute($localKey); - $relation = $this->getRelation(); + $relation = $this->getRelation(); - return new HasCount($modelName, $foreignKey, $value, $relation); - } + return new HasCount($modelName, $foreignKey, $value, $relation); + } - /** - * @param $modelName - * @param $foreignKey - * @param $localKey - * @return ActiveQuery - * @throws Exception - */ - public function hasMany($modelName, $foreignKey, $localKey): mixed - { - if (!$this->has($localKey)) { - throw new Exception("Need join table primary key."); - } + /** + * @param $modelName + * @param $foreignKey + * @param $localKey + * @return ActiveQuery + * @throws Exception + */ + public function hasMany($modelName, $foreignKey, $localKey): mixed + { + if (!$this->has($localKey)) { + throw new Exception("Need join table primary key."); + } - $value = $this->getAttribute($localKey); + $value = $this->getAttribute($localKey); - $relation = $this->getRelation(); + $relation = $this->getRelation(); - return new HasMany($modelName, $foreignKey, $value, $relation); - } + return new HasMany($modelName, $foreignKey, $value, $relation); + } - /** - * @param $modelName - * @param $foreignKey - * @param $localKey - * @return ActiveQuery - * @throws Exception - */ - public function hasIn($modelName, $foreignKey, $localKey): mixed - { - if (!$this->has($localKey)) { - throw new Exception("Need join table primary key."); - } + /** + * @param $modelName + * @param $foreignKey + * @param $localKey + * @return ActiveQuery + * @throws Exception + */ + public function hasIn($modelName, $foreignKey, $localKey): mixed + { + if (!isset($this->attributes[$localKey])) { + throw new Exception("Need join table primary key."); + } - $value = $this->getAttribute($localKey); + $value = $this->getAttribute($localKey); - $relation = $this->getRelation(); + $relation = $this->getRelation(); - return new HasMany($modelName, $foreignKey, $value, $relation); - } + return new HasMany($modelName, $foreignKey, $value, $relation); + } - /** - * @return bool - * @throws Exception - */ - public function afterDelete(): bool - { - if (!$this->hasPrimary()) { - return TRUE; - } - $value = $this->getPrimaryValue(); - if (empty($value)) { - return TRUE; - } - return TRUE; - } + /** + * @return bool + * @throws Exception + */ + public function afterDelete(): bool + { + if (!$this->hasPrimary()) { + return TRUE; + } + $value = $this->getPrimaryValue(); + if (empty($value)) { + return TRUE; + } + return TRUE; + } - /** - * @return bool - * @throws Exception - */ - public function beforeDelete(): bool - { - if (!$this->hasPrimary()) { - return TRUE; - } - $value = $this->getPrimaryValue(); - if (empty($value)) { - return TRUE; - } - return TRUE; - } + /** + * @return bool + * @throws Exception + */ + public function beforeDelete(): bool + { + if (!$this->hasPrimary()) { + return TRUE; + } + $value = $this->getPrimaryValue(); + if (empty($value)) { + return TRUE; + } + return TRUE; + } } From d376042361b6d1cdf0d159010b913fea18a5931e Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sun, 2 May 2021 04:50:45 +0800 Subject: [PATCH 20/32] modify --- Database/ActiveRecord.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Database/ActiveRecord.php b/Database/ActiveRecord.php index bd1b493a..b581b8fb 100644 --- a/Database/ActiveRecord.php +++ b/Database/ActiveRecord.php @@ -342,7 +342,7 @@ class ActiveRecord extends BaseActiveRecord */ public function hasOne(string $modelName, $foreignKey, $localKey): HasOne|ActiveQuery { - if (!$this->has($localKey)) { + if (!isset($this->attributes[$localKey])) { throw new Exception("Need join table primary key."); } @@ -363,7 +363,7 @@ class ActiveRecord extends BaseActiveRecord */ public function hasCount($modelName, $foreignKey, $localKey): mixed { - if (!$this->has($localKey)) { + if (!isset($this->attributes[$localKey])) { throw new Exception("Need join table primary key."); } @@ -384,7 +384,7 @@ class ActiveRecord extends BaseActiveRecord */ public function hasMany($modelName, $foreignKey, $localKey): mixed { - if (!$this->has($localKey)) { + if (!isset($this->attributes[$localKey])) { throw new Exception("Need join table primary key."); } From 1a11178875b42363afb9513095a0e12afa96b1d2 Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sun, 2 May 2021 04:52:29 +0800 Subject: [PATCH 21/32] modify --- Database/ActiveRecord.php | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/Database/ActiveRecord.php b/Database/ActiveRecord.php index b581b8fb..43237297 100644 --- a/Database/ActiveRecord.php +++ b/Database/ActiveRecord.php @@ -342,12 +342,10 @@ class ActiveRecord extends BaseActiveRecord */ public function hasOne(string $modelName, $foreignKey, $localKey): HasOne|ActiveQuery { - if (!isset($this->attributes[$localKey])) { + if (($value = $this->getAttribute($localKey)) === null) { throw new Exception("Need join table primary key."); } - $value = $this->getAttribute($localKey); - $relation = $this->getRelation(); return new HasOne($modelName, $foreignKey, $value, $relation); @@ -363,12 +361,10 @@ class ActiveRecord extends BaseActiveRecord */ public function hasCount($modelName, $foreignKey, $localKey): mixed { - if (!isset($this->attributes[$localKey])) { + if (($value = $this->getAttribute($localKey)) === null) { throw new Exception("Need join table primary key."); } - $value = $this->getAttribute($localKey); - $relation = $this->getRelation(); return new HasCount($modelName, $foreignKey, $value, $relation); @@ -384,12 +380,10 @@ class ActiveRecord extends BaseActiveRecord */ public function hasMany($modelName, $foreignKey, $localKey): mixed { - if (!isset($this->attributes[$localKey])) { + if (($value = $this->getAttribute($localKey)) === null) { throw new Exception("Need join table primary key."); } - $value = $this->getAttribute($localKey); - $relation = $this->getRelation(); return new HasMany($modelName, $foreignKey, $value, $relation); @@ -404,12 +398,10 @@ class ActiveRecord extends BaseActiveRecord */ public function hasIn($modelName, $foreignKey, $localKey): mixed { - if (!isset($this->attributes[$localKey])) { + if (($value = $this->getAttribute($localKey)) === null) { throw new Exception("Need join table primary key."); } - $value = $this->getAttribute($localKey); - $relation = $this->getRelation(); return new HasMany($modelName, $foreignKey, $value, $relation); From e6e2449fe6a3423d7980c36ff213a752f1f7fd42 Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sun, 2 May 2021 04:55:27 +0800 Subject: [PATCH 22/32] modify --- Database/Base/BaseActiveRecord.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Database/Base/BaseActiveRecord.php b/Database/Base/BaseActiveRecord.php index 4715f4fe..90786328 100644 --- a/Database/Base/BaseActiveRecord.php +++ b/Database/Base/BaseActiveRecord.php @@ -968,7 +968,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess */ public function offsetExists(mixed $offset): bool { - return $this->has($offset); + return isset($this->_attributes[$offset]); } /** @@ -997,7 +997,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess */ public function offsetUnset(mixed $offset) { - if (!$this->has($offset)) { + if (!isset($this->_attributes[$offset])) { return; } unset($this->_attributes[$offset]); From 7473193da68c0c7bb635415019d2d4ee98af9bac Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sun, 2 May 2021 04:56:05 +0800 Subject: [PATCH 23/32] modify --- Database/Base/BaseActiveRecord.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Database/Base/BaseActiveRecord.php b/Database/Base/BaseActiveRecord.php index 90786328..de233768 100644 --- a/Database/Base/BaseActiveRecord.php +++ b/Database/Base/BaseActiveRecord.php @@ -968,7 +968,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess */ public function offsetExists(mixed $offset): bool { - return isset($this->_attributes[$offset]); + return isset($this->_attributes[$offset]) || isset($this->_oldAttributes[$offset]); } /** @@ -997,7 +997,8 @@ abstract class BaseActiveRecord extends Component implements IOrm, ArrayAccess */ public function offsetUnset(mixed $offset) { - if (!isset($this->_attributes[$offset])) { + if (!isset($this->_attributes[$offset]) + && !isset($this->_oldAttributes[$offset])) { return; } unset($this->_attributes[$offset]); From ca6ff010ed354aa2c1ddbba4ebb4e4b675e41749 Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sun, 2 May 2021 05:00:06 +0800 Subject: [PATCH 24/32] modify --- HttpServer/Http/Context.php | 401 +++++++++++++++++------------------- 1 file changed, 189 insertions(+), 212 deletions(-) diff --git a/HttpServer/Http/Context.php b/HttpServer/Http/Context.php index 32b2014d..86b7dd6b 100644 --- a/HttpServer/Http/Context.php +++ b/HttpServer/Http/Context.php @@ -13,239 +13,216 @@ use Swoole\Coroutine; class Context extends BaseContext { - protected static array $_contents = []; + protected static array $_contents = []; + /** + * @param $id + * @param $context + * @param null $key + * @return mixed + */ + public static function setContext($id, $context, $key = null): mixed + { + return self::setCoroutine($id, $context, $key); + } - /** - * @param $id - * @param $context - * @param null $key - * @return mixed - */ - public static function setContext($id, $context, $key = null): mixed - { - if (static::inCoroutine()) { - return self::setCoroutine($id, $context, $key); - } else { - return self::setStatic($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 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 $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 + { + if (!isset(Coroutine::getContext()[$id][$key])) { + return false; + } + return Coroutine::getContext()[$id][$key] += $value; + } - /** - * @param $id - * @param null $key - * @param int $value - * @return bool|int - */ - public static function increment($id, $key = null, $value = 1): bool|int - { - if (!static::inCoroutine()) { - return false; - } - if (!isset(Coroutine::getContext()[$id][$key])) { - 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 + { + if (!static::hasContext($id)) { + return false; + } + if (!isset(Coroutine::getContext()[$id][$key])) { + 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 - { - if (!static::inCoroutine() || !static::hasContext($id)) { - return false; - } - if (!isset(Coroutine::getContext()[$id][$key])) { - return false; - } - return Coroutine::getContext()[$id][$key] -= $value; - } - - /** - * @param $id - * @param null $key - * @return mixed - */ - public static function getContext($id, $key = null): mixed - { - if (!static::hasContext($id)) { - return null; - } - if (static::inCoroutine()) { - return static::loadByContext($id, $key); - } else { - return static::loadByStatic($id, $key); - } - } + /** + * @param $id + * @param null $key + * @return mixed + */ + public static function getContext($id, $key = null): mixed + { + return static::loadByContext($id, $key); + } - /** - * @param $id - * @param null $key - * @return mixed - */ - private static function loadByContext($id, $key = null): mixed - { - $data = Coroutine::getContext()[$id] ?? null; - if ($data === null) { - return null; - } - if ($key !== null) { - return $data[$key] ?? null; - } - return $data; - } + /** + * @param $id + * @param null $key + * @return mixed + */ + private static function loadByContext($id, $key = null): mixed + { + $data = Coroutine::getContext()[$id] ?? null; + if ($data === null) { + return null; + } + if ($key !== null) { + return $data[$key] ?? null; + } + return $data; + } - /** - * @param $id - * @param null $key - * @return mixed - */ - private static function loadByStatic($id, $key = null): mixed - { - $data = static::$_contents[$id] ?? null; - if ($data === null) { - return null; - } - if ($key !== null) { - return $data[$key] ?? null; - } - return $data; - } + /** + * @param $id + * @param null $key + * @return mixed + */ + private static function loadByStatic($id, $key = null): mixed + { + $data = static::$_contents[$id] ?? null; + if ($data === null) { + return null; + } + if ($key !== null) { + return $data[$key] ?? null; + } + return $data; + } - /** - * @return mixed - */ - public static function getAllContext(): mixed - { - if (static::inCoroutine()) { - return Coroutine::getContext() ?? []; - } else { - return static::$_contents ?? []; - } - } + /** + * @return mixed + */ + public static function getAllContext(): mixed + { + if (static::inCoroutine()) { + return Coroutine::getContext() ?? []; + } else { + return static::$_contents ?? []; + } + } - /** - * @param string $id - * @param string|null $key - */ - public static function remove(string $id, string $key = null) - { - if (!static::hasContext($id, $key)) { - return; - } - if (static::inCoroutine()) { - unset(static::$_contents[$id]); - return; - } - if (!empty($key)) { - unset(Coroutine::getContext()[$id][$key]); - } else { - unset(Coroutine::getContext()[$id]); - } - } + /** + * @param string $id + * @param string|null $key + */ + public static function remove(string $id, string $key = null) + { + if (!static::hasContext($id, $key)) { + return; + } + if (!empty($key)) { + unset(Coroutine::getContext()[$id][$key]); + } else { + unset(Coroutine::getContext()[$id]); + } + } - /** - * @param $id - * @param null $key - * @return bool - */ - public static function hasContext($id, $key = null): bool - { - if (!static::inCoroutine()) { - return static::searchByStatic($id, $key); - } else { - return static::searchByCoroutine($id, $key); - } - } + /** + * @param $id + * @param null $key + * @return bool + */ + public static function hasContext($id, $key = null): bool + { + return static::searchByCoroutine($id, $key); + } - /** - * @param $id - * @param null $key - * @return bool - */ - private static function searchByStatic($id, $key = null): bool - { - if (!isset(static::$_contents[$id])) { - return false; - } - if (!empty($key) && !isset(static::$_contents[$id][$key])) { - return false; - } - return true; - } + /** + * @param $id + * @param null $key + * @return bool + */ + private static function searchByStatic($id, $key = null): bool + { + if (!isset(static::$_contents[$id])) { + return false; + } + if (!empty($key) && !isset(static::$_contents[$id][$key])) { + return false; + } + return true; + } - /** - * @param $id - * @param null $key - * @return bool - */ - private static function searchByCoroutine($id, $key = null): bool - { - if (!isset(Coroutine::getContext()[$id])) { - return false; - } - if ($key !== null) { - return isset((Coroutine::getContext()[$id] ?? [])[$key]); - } - return true; - } + /** + * @param $id + * @param null $key + * @return bool + */ + private static function searchByCoroutine($id, $key = null): bool + { + if (!isset(Coroutine::getContext()[$id])) { + return false; + } + if ($key !== null) { + return isset((Coroutine::getContext()[$id] ?? [])[$key]); + } + return true; + } - /** - * @return bool - */ - public static function inCoroutine(): bool - { - return Coroutine::getCid() > 0; - } + /** + * @return bool + */ + public static function inCoroutine(): bool + { + return Coroutine::getCid() > 0; + } } From 1699a72add596cd09c237b9f53eb3a0adbed2f19 Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sun, 2 May 2021 05:08:02 +0800 Subject: [PATCH 25/32] modify --- HttpServer/Http/Context.php | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/HttpServer/Http/Context.php b/HttpServer/Http/Context.php index 86b7dd6b..5df350a1 100644 --- a/HttpServer/Http/Context.php +++ b/HttpServer/Http/Context.php @@ -24,6 +24,9 @@ class Context extends BaseContext */ public static function setContext($id, $context, $key = null): mixed { + if (Coroutine::getCid() > 0) { + return static::setStatic($id, $context, $key); + } return self::setCoroutine($id, $context, $key); } @@ -103,6 +106,9 @@ class Context extends BaseContext */ public static function getContext($id, $key = null): mixed { + if (Coroutine::getCid() > 0) { + return static::loadByStatic($id, $key); + } return static::loadByContext($id, $key); } @@ -164,10 +170,18 @@ class Context extends BaseContext if (!static::hasContext($id, $key)) { return; } - if (!empty($key)) { - unset(Coroutine::getContext()[$id][$key]); + if (Coroutine::getCid() > 0) { + if (!empty($key)) { + unset(static::$_contents[$id][$key]); + } else { + unset(static::$_contents[$id]); + } } else { - unset(Coroutine::getContext()[$id]); + if (!empty($key)) { + unset(Coroutine::getContext()[$id][$key]); + } else { + unset(Coroutine::getContext()[$id]); + } } } @@ -178,6 +192,9 @@ class Context extends BaseContext */ public static function hasContext($id, $key = null): bool { + if (Coroutine::getCid() > 0) { + return static::searchByStatic($id, $key); + } return static::searchByCoroutine($id, $key); } From ead674613a6073ba55c30e2f8dc6e2d995ab5775 Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sun, 2 May 2021 05:10:23 +0800 Subject: [PATCH 26/32] modify --- HttpServer/Http/Context.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/HttpServer/Http/Context.php b/HttpServer/Http/Context.php index 5df350a1..c9773685 100644 --- a/HttpServer/Http/Context.php +++ b/HttpServer/Http/Context.php @@ -24,7 +24,7 @@ class Context extends BaseContext */ public static function setContext($id, $context, $key = null): mixed { - if (Coroutine::getCid() > 0) { + if (!static::inCoroutine()) { return static::setStatic($id, $context, $key); } return self::setCoroutine($id, $context, $key); @@ -106,7 +106,7 @@ class Context extends BaseContext */ public static function getContext($id, $key = null): mixed { - if (Coroutine::getCid() > 0) { + if (!static::inCoroutine()) { return static::loadByStatic($id, $key); } return static::loadByContext($id, $key); @@ -170,7 +170,7 @@ class Context extends BaseContext if (!static::hasContext($id, $key)) { return; } - if (Coroutine::getCid() > 0) { + if (!static::inCoroutine()) { if (!empty($key)) { unset(static::$_contents[$id][$key]); } else { @@ -192,7 +192,7 @@ class Context extends BaseContext */ public static function hasContext($id, $key = null): bool { - if (Coroutine::getCid() > 0) { + if (!static::inCoroutine()) { return static::searchByStatic($id, $key); } return static::searchByCoroutine($id, $key); @@ -238,7 +238,7 @@ class Context extends BaseContext */ public static function inCoroutine(): bool { - return Coroutine::getCid() > 0; + return Coroutine::getCid() === -1; } } From c32ae6ffd989e6509087453e92649524896a99ee Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sun, 2 May 2021 05:11:49 +0800 Subject: [PATCH 27/32] modify --- HttpServer/Http/Context.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/HttpServer/Http/Context.php b/HttpServer/Http/Context.php index c9773685..839162d5 100644 --- a/HttpServer/Http/Context.php +++ b/HttpServer/Http/Context.php @@ -238,7 +238,7 @@ class Context extends BaseContext */ public static function inCoroutine(): bool { - return Coroutine::getCid() === -1; + return Coroutine::getCid() !== -1; } } From c67156ce2d3443b3c634e53ae7daecc2f5682dc5 Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sun, 2 May 2021 05:22:20 +0800 Subject: [PATCH 28/32] modify --- System/Channel.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/System/Channel.php b/System/Channel.php index b79ccfa2..5014a906 100644 --- a/System/Channel.php +++ b/System/Channel.php @@ -29,6 +29,9 @@ class Channel extends Component public function push(mixed $value, string $name = ''): void { $channel = $this->channelInit($name); + if ($channel->count() >= 100) { + return; + } $channel->enqueue($value); } From 6fffb8988a4e249b512e562de8c32df73567919e Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sun, 2 May 2021 05:29:32 +0800 Subject: [PATCH 29/32] modify --- System/Channel.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/System/Channel.php b/System/Channel.php index 5014a906..bda439ab 100644 --- a/System/Channel.php +++ b/System/Channel.php @@ -76,9 +76,7 @@ class Channel extends Component */ public function pop(string $name, Closure $closure): mixed { - if (($channel = $this->channelInit($name)) == false) { - return $this->addError('Channel is full.'); - } + $channel = $this->channelInit($name); if ($channel->isEmpty()) { return call_user_func($closure); } From 993528c834ed913b8dbc6b5b69e5b1aa58d36787 Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sun, 2 May 2021 05:32:57 +0800 Subject: [PATCH 30/32] modify --- System/Pool/Connection.php | 393 +++++++++++++++++++------------------ 1 file changed, 197 insertions(+), 196 deletions(-) diff --git a/System/Pool/Connection.php b/System/Pool/Connection.php index 4103c7e7..ac207990 100644 --- a/System/Pool/Connection.php +++ b/System/Pool/Connection.php @@ -19,225 +19,226 @@ class Connection extends Pool { - public int $timeout = 1900; + public int $timeout = 1900; - /** - * @param $timeout - */ - public function setTimeout($timeout) - { - $this->timeout = $timeout; - } + /** + * @param $timeout + */ + public function setTimeout($timeout) + { + $this->timeout = $timeout; + } - /** - * @param $value - */ - public function setLength($value) - { - $this->max = $value; - } + /** + * @param $value + */ + public function setLength($value) + { + $this->max = $value; + } - /** - * @param $cds - * @return bool - * - * db is in transaction - */ - public function inTransaction($cds): bool - { - return Context::getContext('begin_' . $this->name('mysql', $cds, true)) == 0; - } + /** + * @param $cds + * @return bool + * + * db is in transaction + */ + public function inTransaction($cds): bool + { + return Context::getContext('begin_' . $this->name('mysql', $cds, true)) == 0; + } - /** - * @param $coroutineName - */ - public function beginTransaction($coroutineName) - { - $coroutineName = $this->name('mysql', $coroutineName, true); - if (!Context::hasContext('begin_' . $coroutineName)) { - Context::setContext('begin_' . $coroutineName, 0); - } - Context::increment('begin_' . $coroutineName); - if (!Context::getContext('begin_' . $coroutineName) !== 0) { - return; - } - $connection = Context::getContext($coroutineName); - if ($connection instanceof PDO && !$connection->inTransaction()) { - $connection->beginTransaction(); - } - } + /** + * @param $coroutineName + */ + public function beginTransaction($coroutineName) + { + $coroutineName = $this->name('mysql', $coroutineName, true); + if (!Context::hasContext('begin_' . $coroutineName)) { + Context::setContext('begin_' . $coroutineName, 0); + } + Context::increment('begin_' . $coroutineName); + if (!Context::getContext('begin_' . $coroutineName) !== 0) { + return; + } + $connection = Context::getContext($coroutineName); + if ($connection instanceof PDO && !$connection->inTransaction()) { + $connection->beginTransaction(); + } + } - /** - * @param $coroutineName - */ - public function commit($coroutineName) - { - $coroutineName = $this->name('mysql', $coroutineName, true); - if (!Context::hasContext('begin_' . $coroutineName)) { - return; - } - if (Context::decrement('begin_' . $coroutineName) > 0) { - return; - } - $connection = Context::getContext($coroutineName); - if (!($connection instanceof PDO)) { - return; - } - Context::setContext('begin_' . $coroutineName, 0); - if ($connection->inTransaction()) { - $connection->commit(); - } - } + /** + * @param $coroutineName + */ + public function commit($coroutineName) + { + $coroutineName = $this->name('mysql', $coroutineName, true); + if (!Context::hasContext('begin_' . $coroutineName)) { + return; + } + if (Context::decrement('begin_' . $coroutineName) > 0) { + return; + } + $connection = Context::getContext($coroutineName); + if (!($connection instanceof PDO)) { + return; + } + Context::setContext('begin_' . $coroutineName, 0); + if ($connection->inTransaction()) { + $connection->commit(); + } + } - /** - * @param $name - * @param false $isMaster - * @return array - */ - private function getIndex($name, $isMaster = false): array - { - return [Coroutine::getCid(), $this->name('mysql', $name, $isMaster)]; - } + /** + * @param $name + * @param false $isMaster + * @return array + */ + private function getIndex($name, $isMaster = false): array + { + return [Coroutine::getCid(), $this->name('mysql', $name, $isMaster)]; + } - /** - * @param $coroutineName - */ - public function rollback($coroutineName) - { - $coroutineName = $this->name('mysql', $coroutineName, true); - if (!Context::hasContext('begin_' . $coroutineName)) { - return; - } - if (Context::decrement('begin_' . $coroutineName) > 0) { - return; - } - if (($connection = Context::getContext($coroutineName)) instanceof PDO) { - if ($connection->inTransaction()) { - $connection->rollBack(); - } - } - Context::setContext('begin_' . $coroutineName, 0); - } + /** + * @param $coroutineName + */ + public function rollback($coroutineName) + { + $coroutineName = $this->name('mysql', $coroutineName, true); + if (!Context::hasContext('begin_' . $coroutineName)) { + return; + } + if (Context::decrement('begin_' . $coroutineName) > 0) { + return; + } + if (($connection = Context::getContext($coroutineName)) instanceof PDO) { + if ($connection->inTransaction()) { + $connection->rollBack(); + } + } + Context::setContext('begin_' . $coroutineName, 0); + } - /** - * @param mixed $config - * @param bool $isMaster - * @return mixed - * @throws Exception - */ - public function get(mixed $config, $isMaster = false): mixed - { - $coroutineName = $this->name('mysql', $config['cds'], $isMaster); - if (($pdo = Context::getContext($coroutineName)) instanceof PDO) { - return $pdo; - } - $connections = $this->getFromChannel($coroutineName, $config); - if ($number = Context::getContext('begin_' . $coroutineName, Coroutine::getCid())) { - $number > 0 && $connections->beginTransaction(); - } - return Context::setContext($coroutineName, $connections); - } + /** + * @param mixed $config + * @param bool $isMaster + * @return mixed + * @throws Exception + */ + public function get(mixed $config, $isMaster = false): mixed + { + $coroutineName = $this->name('mysql', $config['cds'], $isMaster); + if (($pdo = Context::getContext($coroutineName)) instanceof PDO) { + return $pdo; + } + $connections = $this->getFromChannel($coroutineName, $config); + if ($number = Context::getContext('begin_' . $coroutineName, Coroutine::getCid())) { + $number > 0 && $connections->beginTransaction(); + } + return Context::setContext($coroutineName, $connections); + } - /** - * @param string $name - * @param mixed $config - * @return PDO - * @throws Exception - */ - public function createClient(string $name, mixed $config): PDO - { - $link = new PDO($config['cds'], $config['username'], $config['password'], [ - PDO::ATTR_EMULATE_PREPARES => false, - PDO::ATTR_CASE => PDO::CASE_NATURAL, - PDO::ATTR_TIMEOUT => $this->timeout, - PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . ($config['charset'] ?? 'utf8mb4') - ]); - $link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); - $link->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false); - $link->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_EMPTY_STRING); - return $link; - } + /** + * @param string $name + * @param mixed $config + * @return PDO + * @throws Exception + */ + public function createClient(string $name, mixed $config): PDO + { + $link = new PDO($config['cds'], $config['username'], $config['password'], [ + PDO::ATTR_EMULATE_PREPARES => false, + PDO::ATTR_CASE => PDO::CASE_NATURAL, + PDO::ATTR_TIMEOUT => $this->timeout, + PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true, + PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . ($config['charset'] ?? 'utf8mb4') + ]); + $link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); + $link->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false); + $link->setAttribute(PDO::ATTR_ORACLE_NULLS, PDO::NULL_EMPTY_STRING); + return $link; + } - /** - * @param $coroutineName - * @param $isMaster - * @throws Exception - */ - public function release($coroutineName, $isMaster) - { - $coroutineName = $this->name('mysql', $coroutineName, $isMaster); + /** + * @param $coroutineName + * @param $isMaster + * @throws Exception + */ + public function release($coroutineName, $isMaster) + { + $coroutineName = $this->name('mysql', $coroutineName, $isMaster); - /** @var PDO $client */ - if (!($client = Context::getContext($coroutineName)) instanceof PDO) { - return; - } - if ($client->inTransaction()) { - $client->commit(); - } - $this->push($coroutineName, $client); - $this->lastTime = time(); - } + /** @var PDO $client */ + if (!($client = Context::getContext($coroutineName)) instanceof PDO) { + return; + } + if ($client->inTransaction()) { + $client->commit(); + } + $this->push($coroutineName, $client); + $this->lastTime = time(); + } - /** - * @param $coroutineName - * @return bool - */ - private function hasClient($coroutineName): bool - { - return Context::hasContext($coroutineName); - } + /** + * @param $coroutineName + * @return bool + */ + private function hasClient($coroutineName): bool + { + return Context::hasContext($coroutineName); + } - /** - * batch release - * @throws Exception - */ - public function connection_clear() - { - $this->flush(0); - } + /** + * batch release + * @throws Exception + */ + public function connection_clear() + { + $this->flush(0); + } - /** - * @param string $name - * @param mixed $client - * @return bool - * @throws Exception - */ - public function checkCanUse(string $name, mixed $client): bool - { - try { - if (empty($client) || !($client instanceof PDO)) { - $result = false; - } else { - $result = true; - } - } catch (Error | Throwable $exception) { - $result = $this->addError($exception, 'mysql'); - } finally { - if (!$result) { - $this->decrement($name); - } - return $result; - } - } + /** + * @param string $name + * @param mixed $client + * @return bool + * @throws Exception + */ + public function checkCanUse(string $name, mixed $client): bool + { + try { + if (empty($client) || !($client instanceof PDO)) { + $result = false; + } else { + $result = true; + } + } catch (Error | Throwable $exception) { + $result = $this->addError($exception, 'mysql'); + } finally { + if (!$result) { + $this->decrement($name); + } + return $result; + } + } - /** - * @param $coroutineName - * @param bool $isMaster - * @throws Exception - */ - public function disconnect($coroutineName, $isMaster = false) - { - $coroutineName = $this->name($coroutineName, $isMaster); - $this->clean($coroutineName); - } + /** + * @param $coroutineName + * @param bool $isMaster + * @throws Exception + */ + public function disconnect($coroutineName, $isMaster = false) + { + $coroutineName = $this->name($coroutineName, $isMaster); + $this->clean($coroutineName); + } } From 9e1fc9ab1e006aa203b28b3b9687288951488406 Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sun, 2 May 2021 05:40:59 +0800 Subject: [PATCH 31/32] modify --- Database/Command.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/Database/Command.php b/Database/Command.php index f400a11f..2eaef4ee 100644 --- a/Database/Command.php +++ b/Database/Command.php @@ -121,15 +121,11 @@ class Command extends Component private function execute($type, $isInsert = null, $hasAutoIncrement = null): int|bool|array|string|null { try { - $time = microtime(true); if ($type === static::EXECUTE) { $result = $this->insert_or_change($isInsert, $hasAutoIncrement); } else { $result = $this->search($type); } - if (microtime(true) - $time >= 0.03) { - $this->warning('execute sql Worker.' . env('worker') . '.' . Coroutine::getCid() . '`' . $this->sql . '` use time ' . (microtime(true) - $time)); - } return $result; } catch (\Throwable $exception) { return $this->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql'); @@ -144,11 +140,7 @@ class Command extends Component */ private function search($type): mixed { - $time = microtime(true); $connect = $this->db->getConnect($this->sql); - if (microtime(true) - $time > 0.02) { - $this->error('get connect time:' . $this->sql . '; ' . (microtime(true) - $time)); - } if (!($query = $connect?->query($this->sql))) { return $this->addError($connect->errorInfo()[2] ?? '数据库异常, 请稍后再试.'); } From 02b8c7ea3e637c40f57255f3acf403b470d77f21 Mon Sep 17 00:00:00 2001 From: "as2252258@163.com" Date: Sun, 2 May 2021 13:51:12 +0800 Subject: [PATCH 32/32] modify --- System/Abstracts/Pool.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/System/Abstracts/Pool.php b/System/Abstracts/Pool.php index 249d218a..e5d2caa1 100644 --- a/System/Abstracts/Pool.php +++ b/System/Abstracts/Pool.php @@ -174,11 +174,7 @@ abstract class Pool extends Component if ($this->_items[$name]->isEmpty()) { $this->createByCallback($name, $callback); } - $time = microtime(true); $connection = $this->_items[$name]->pop(0.002); - if (($end = microtime(true) - $time) >= 0.01) { - $this->error('waite channel connect use time.' . $end); - } if (!$this->checkCanUse($name, $connection)) { return $this->createClient($name, $callback); } else {