From 8c181b9cbe9980778f0aa7e88f8ebf1506f28122 Mon Sep 17 00:00:00 2001 From: Demin Yin Date: Fri, 16 Jul 2021 11:20:17 -0700 Subject: [PATCH] updates for Swoole 4.7.0 Signed-off-by: Demin Yin --- output/swoole/aliases.php | 1 - output/swoole/constants.php | 31 ++++++---- output/swoole/functions.php | 3 +- output/swoole/namespace/Coroutine.php | 16 +++++- output/swoole/namespace/Coroutine/System.php | 2 +- output/swoole/namespace/Process/Pool.php | 14 +++++ output/swoole/namespace/Server/Port.php | 2 + output/swoole/namespace/Table.php | 32 +---------- output/swoole/namespace/Table/Row.php | 45 --------------- .../swoole_library/src/core/ArrayObject.php | 13 +++++ .../src/core/ConnectionPool.php | 4 +- output/swoole_library/src/core/Constant.php | 57 ++++++++++++------- .../src/core/Coroutine/functions.php | 2 +- .../swoole_library/src/core/Curl/Handler.php | 41 ++++++++++++- .../src/core/Database/PDOStatementProxy.php | 9 +-- .../swoole_library/src/core/Http/Status.php | 2 +- .../swoole_library/src/core/StringObject.php | 23 ++++---- 17 files changed, 162 insertions(+), 135 deletions(-) delete mode 100644 output/swoole/namespace/Table/Row.php diff --git a/output/swoole/aliases.php b/output/swoole/aliases.php index 13c2397e..c83e9b46 100644 --- a/output/swoole/aliases.php +++ b/output/swoole/aliases.php @@ -42,7 +42,6 @@ class_alias(Swoole\Server::class, swoole_server::class); class_alias(Swoole\Server\Port::class, swoole_server_port::class); class_alias(Swoole\Server\Task::class, swoole_server_task::class); class_alias(Swoole\Table::class, swoole_table::class); -class_alias(Swoole\Table\Row::class, swoole_table_row::class); class_alias(Swoole\Timer::class, swoole_timer::class); class_alias(Swoole\Timer\Iterator::class, swoole_timer_iterator::class); class_alias(Swoole\Websocket\Closeframe::class, swoole_websocket_closeframe::class); diff --git a/output/swoole/constants.php b/output/swoole/constants.php index 2c9a0de6..ec58b999 100644 --- a/output/swoole/constants.php +++ b/output/swoole/constants.php @@ -1,14 +1,15 @@ array[$key]); } + /** + * @param mixed $key + * @param mixed $default + * @return ArrayObject|StringObject + */ + public function getOr($key, $default = null) + { + if (!$this->exists($key)) { + return $default; + } + return static::detectType($this->array[$key]); + } + /** * @return mixed */ diff --git a/output/swoole_library/src/core/ConnectionPool.php b/output/swoole_library/src/core/ConnectionPool.php index c37be02b..3773294e 100644 --- a/output/swoole_library/src/core/ConnectionPool.php +++ b/output/swoole_library/src/core/ConnectionPool.php @@ -49,7 +49,7 @@ public function fill(): void } } - public function get() + public function get(float $timeout = -1) { if ($this->pool === null) { throw new RuntimeException('Pool has been closed'); @@ -57,7 +57,7 @@ public function get() if ($this->pool->isEmpty() && $this->num < $this->size) { $this->make(); } - return $this->pool->pop(); + return $this->pool->pop($timeout); } public function put($connection): void diff --git a/output/swoole_library/src/core/Constant.php b/output/swoole_library/src/core/Constant.php index ea8c0d77..a8bda4ff 100644 --- a/output/swoole_library/src/core/Constant.php +++ b/output/swoole_library/src/core/Constant.php @@ -13,43 +13,56 @@ class Constant { - public const EVENT_RECEIVE = 'receive'; - - public const EVENT_CONNECT = 'connect'; + /* {{{ EVENT */ + public const EVENT_START = 'start'; - public const EVENT_CLOSE = 'close'; + public const EVENT_SHUTDOWN = 'shutdown'; - public const EVENT_PACKET = 'packet'; + public const EVENT_WORKER_START = 'workerStart'; - public const EVENT_REQUEST = 'request'; + public const EVENT_WORKER_STOP = 'workerStop'; - public const EVENT_MESSAGE = 'message'; + public const EVENT_BEFORE_RELOAD = 'beforeReload'; - public const EVENT_OPEN = 'open'; - - public const EVENT_HANDSHAKE = 'handshake'; + public const EVENT_AFTER_RELOAD = 'afterReload'; public const EVENT_TASK = 'task'; public const EVENT_FINISH = 'finish'; - public const EVENT_START = 'start'; - - public const EVENT_SHUTDOWN = 'shutdown'; - - public const EVENT_WORKER_START = 'workerStart'; - public const EVENT_WORKER_EXIT = 'workerExit'; public const EVENT_WORKER_ERROR = 'workerError'; - public const EVENT_WORKER_STOP = 'workerStop'; + public const EVENT_MANAGER_START = 'managerStart'; + + public const EVENT_MANAGER_STOP = 'managerStop'; public const EVENT_PIPE_MESSAGE = 'pipeMessage'; - public const EVENT_MANAGER_START = 'managerStart'; + public const EVENT_CONNECT = 'connect'; - public const EVENT_MANAGER_STOP = 'managerStop'; + public const EVENT_RECEIVE = 'receive'; + + public const EVENT_CLOSE = 'close'; + + public const EVENT_PACKET = 'packet'; + + public const EVENT_BUFFER_FULL = 'bufferFull'; + + public const EVENT_BUFFER_EMPTY = 'bufferEmpty'; + + public const EVENT_REQUEST = 'request'; + + public const EVENT_HANDSHAKE = 'handshake'; + + public const EVENT_OPEN = 'open'; + + public const EVENT_MESSAGE = 'message'; + + public const EVENT_DISCONNECT = 'disconnect'; + + /* }}} EVENT */ public const EVENT_ERROR = 'error'; @@ -224,6 +237,10 @@ class Constant public const OPTION_WEBSOCKET_MASK = 'websocket_mask'; + public const OPTION_HTTP_COMPRESSION = 'http_compression'; + + public const OPTION_BODY_DECOMPRESSION = 'body_decompression'; + public const OPTION_WEBSOCKET_COMPRESSION = 'websocket_compression'; public const OPTION_HTTP_PARSE_COOKIE = 'http_parse_cookie'; @@ -232,8 +249,6 @@ class Constant public const OPTION_HTTP_PARSE_FILES = 'http_parse_files'; - public const OPTION_HTTP_COMPRESSION = 'http_compression'; - public const OPTION_HTTP_COMPRESSION_LEVEL = 'http_compression_level'; public const OPTION_HTTP_GZIP_LEVEL = 'http_gzip_level'; diff --git a/output/swoole_library/src/core/Coroutine/functions.php b/output/swoole_library/src/core/Coroutine/functions.php index 70cdfdfd..4fc47696 100644 --- a/output/swoole_library/src/core/Coroutine/functions.php +++ b/output/swoole_library/src/core/Coroutine/functions.php @@ -26,7 +26,7 @@ function run(callable $fn, ...$args) function go(callable $fn, ...$args) { - Coroutine::create($fn, ...$args); + return Coroutine::create($fn, ...$args); } function defer(callable $fn) diff --git a/output/swoole_library/src/core/Curl/Handler.php b/output/swoole_library/src/core/Curl/Handler.php index 7e1e1642..92168641 100644 --- a/output/swoole_library/src/core/Curl/Handler.php +++ b/output/swoole_library/src/core/Curl/Handler.php @@ -59,6 +59,7 @@ final class Handler 'protocol' => 0, 'ssl_verifyresult' => 0, 'scheme' => '', + 'private' => '', ]; private $withHeaderOut = false; @@ -129,6 +130,8 @@ final class Handler private $cookieJar = ''; + private $resolve = []; + public function __construct(string $url = '') { if ($url) { @@ -215,7 +218,15 @@ private function create(?array $urlInfo = null): void if ($urlInfo === null) { $urlInfo = $this->urlInfo; } - $this->client = new Client($urlInfo['host'], $urlInfo['port'], $urlInfo['scheme'] === 'https'); + $host = $urlInfo['host']; + $port = $urlInfo['port']; + if (isset($this->resolve[$host])) { + if (!$this->hasHeader('Host')) { + $this->setHeader('Host', $host); + } + $this->urlInfo['host'] = $host = $this->resolve[$host][$port] ?? null ?: $host; + } + $this->client = new Client($host, $port, $urlInfo['scheme'] === 'https'); } private function getUrl(): string @@ -303,7 +314,7 @@ private function setPort(int $port): void private function setError($code, $msg = ''): void { $this->errCode = $code; - $this->errMsg = $msg ? $msg : curl_strerror($code); + $this->errMsg = $msg ?: curl_strerror($code); } private function hasHeader(string $headerName): bool @@ -412,6 +423,25 @@ private function setOption(int $opt, $value): bool $this->nobody = boolval($value); $this->method = 'HEAD'; break; + case CURLOPT_RESOLVE: + foreach ((array) $value as $resolve) { + $flag = substr($resolve, 0, 1); + if ($flag === '+' || $flag === '-') { + // TODO: [+]HOST:PORT:ADDRESS + $resolve = substr($resolve, 1); + } + $tmpResolve = explode(':', $resolve, 3); + $host = $tmpResolve[0] ?? ''; + $port = $tmpResolve[1] ?? 0; + $ip = $tmpResolve[2] ?? ''; + if ($flag === '-') { + unset($this->resolve[$host][$port]); + } else { + // TODO: HOST:PORT:ADDRESS[,ADDRESS]... + $this->resolve[$host][$port] = explode(',', $ip)[0]; + } + } + break; case CURLOPT_IPRESOLVE: if ($value !== CURL_IPRESOLVE_WHATEVER and $value !== CURL_IPRESOLVE_V4) { throw new Swoole\Curl\Exception( @@ -422,6 +452,9 @@ private function setOption(int $opt, $value): bool case CURLOPT_TCP_NODELAY: $this->clientOptions[Constant::OPTION_OPEN_TCP_NODELAY] = boolval($value); break; + case CURLOPT_PRIVATE: + $this->info['private'] = $value; + break; /* * Ignore options */ @@ -799,6 +832,10 @@ private function execute() $this->info['redirect_time'] = microtime(true) - $redirectBeginTime; } + if (filter_var($this->urlInfo['host'], FILTER_VALIDATE_IP)) { + $this->info['primary_ip'] = $this->urlInfo['host']; + } + $headerContent = ''; if ($client->headers) { $cb = $this->headerFunction; diff --git a/output/swoole_library/src/core/Database/PDOStatementProxy.php b/output/swoole_library/src/core/Database/PDOStatementProxy.php index 0b2972b4..532cf65e 100644 --- a/output/swoole_library/src/core/Database/PDOStatementProxy.php +++ b/output/swoole_library/src/core/Database/PDOStatementProxy.php @@ -123,13 +123,10 @@ public function setAttribute(int $attribute, $value): bool return $this->__object->setAttribute($attribute, $value); } - public function setFetchMode(int $mode, $classNameObject = null, array $ctorarfg = []): bool + public function setFetchMode(int $mode, ...$args): bool { - $this->setFetchModeContext = [$mode, $classNameObject, $ctorarfg]; - if (!isset($classNameObject)) { - return $this->__object->setFetchMode($mode); - } - return $this->__object->setFetchMode($mode, $classNameObject, $ctorarfg); + $this->setFetchModeContext = func_get_args(); + return $this->__object->setFetchMode(...$this->setFetchModeContext); } public function bindParam($parameter, &$variable, $data_type = PDO::PARAM_STR, $length = null, $driver_options = null): bool diff --git a/output/swoole_library/src/core/Http/Status.php b/output/swoole_library/src/core/Http/Status.php index 6ba568e1..3c5dc52d 100644 --- a/output/swoole_library/src/core/Http/Status.php +++ b/output/swoole_library/src/core/Http/Status.php @@ -176,7 +176,7 @@ abstract class Status self::UNSUPPORTED_MEDIA_TYPE => 'Unsupported Media Type', self::REQUESTED_RANGE_NOT_SATISFIABLE => 'Requested range not satisfiable', self::EXPECTATION_FAILED => 'Expectation Failed', - self::MISDIRECTED_REQUEST => 'Unprocessable Entity', + self::MISDIRECTED_REQUEST => 'Misdirected Request', self::UNPROCESSABLE_ENTITY => 'Unprocessable Entity', self::LOCKED => 'Locked', self::FAILED_DEPENDENCY => 'Failed Dependency', diff --git a/output/swoole_library/src/core/StringObject.php b/output/swoole_library/src/core/StringObject.php index 11f31174..c7b03ab1 100644 --- a/output/swoole_library/src/core/StringObject.php +++ b/output/swoole_library/src/core/StringObject.php @@ -124,29 +124,28 @@ public function substr(int $offset, ?int $length = null) return new static(substr($this->string, ...func_get_args())); } - public function repeat(int $n): StringObject + /** + * @return static + */ + public function repeat(int $times): self { - return new static(str_repeat($this->string, $n)); + return new static(str_repeat($this->string, $times)); } /** - * @param $str + * @param mixed $str + * @return static */ - public function append($str): StringObject + public function append($str): self { - if (is_string($str)) { - $this->string .= $str; - } else { - $this->string .= strval($str); - } - return $this; + return new static($this->string .= $str); } /** * @param null|int $count * @return static */ - public function replace(string $search, string $replace, &$count = null) + public function replace(string $search, string $replace, &$count = null): self { return new static(str_replace($search, $replace, $this->string, $count)); } @@ -193,7 +192,7 @@ public function char(int $index): string /** * @return static */ - public function chunkSplit(int $chunkLength = 76, string $chunkEnd = '') + public function chunkSplit(int $chunkLength = 76, string $chunkEnd = ''): self { return new static(chunk_split($this->string, ...func_get_args())); }