diff --git a/CHANGELOG-3.1.md b/CHANGELOG-3.1.md index 0616e53b2..17f01a7a1 100644 --- a/CHANGELOG-3.1.md +++ b/CHANGELOG-3.1.md @@ -62,6 +62,7 @@ - [x] Remove deprecated codes. - [#5813](https://github.com/hyperf/hyperf/pull/5813) Removed support for swoole 4.x - [#5859](https://github.com/hyperf/hyperf/pull/5859) Removed string cache from `Hyperf\Stringable\Str` +- [#6040](https://github.com/hyperf/hyperf/pull/6040) Removed some deprecated methods from `Hyperf\Di\Annotation\AbstractAnnotation`. ## Changed diff --git a/docs/en/coroutine.md b/docs/en/coroutine.md index 44f246c2e..039f6527a 100644 --- a/docs/en/coroutine.md +++ b/docs/en/coroutine.md @@ -2,7 +2,7 @@ ## Concept -Hyperf is built on the coroutine of `Swoole 4`, which is one of the big factors that Hyperf can provide for high performance. +Hyperf is built on the coroutine of `Swoole 5`, which is one of the big factors that Hyperf can provide for high performance. ### Running Mode of PHP-FPM diff --git a/docs/zh-cn/annotation.md b/docs/zh-cn/annotation.md index 1c1052b2d..5e4d6b7e2 100644 --- a/docs/zh-cn/annotation.md +++ b/docs/zh-cn/annotation.md @@ -159,6 +159,8 @@ return [ 如: `class_map/Hyperf/Utils/Coroutine.php` +[Coroutine.php](https://github.com/hyperf/biz-skeleton/blob/master/app/Kernel/Context/Coroutine.php) + ```php container = $container; $this->logger = $container->get(StdoutLoggerInterface::class); - if ($container->has(FormatterInterface::class)) { - $this->formatter = $container->get(FormatterInterface::class); - } } /** @@ -202,23 +200,26 @@ class Coroutine */ public function create(callable $callable): int { - $id = Utils\Coroutine::id(); - $result = SwooleCoroutine::create(function () use ($callable, $id) { + $id = Co::id(); + $coroutine = Co::create(function () use ($callable, $id) { try { - // 按需复制,禁止复制 Socket,不然会导致 Socket 跨协程调用从而报错。 + // Shouldn't copy all contexts to avoid socket already been bound to another coroutine. Context::copy($id, [ + AppendRequestIdProcessor::REQUEST_ID, ServerRequestInterface::class, ]); - call($callable); + $callable(); } catch (Throwable $throwable) { - if ($this->formatter) { - $this->logger->warning($this->formatter->format($throwable)); - } else { - $this->logger->warning((string) $throwable); - } + $this->logger->warning((string) $throwable); } }); - return is_int($result) ? $result : -1; + + try { + return $coroutine->getId(); + } catch (Throwable $throwable) { + $this->logger->warning((string) $throwable); + return -1; + } } } @@ -226,6 +227,8 @@ class Coroutine 然后,我们实现一个跟 `Hyperf\Coroutine\Coroutine` 一模一样的对象。其中 `create()` 方法替换成我们上述实现的方法。 +[Coroutine.php](https://github.com/hyperf/biz-skeleton/blob/master/app/Kernel/ClassMap/Coroutine.php) + `class_map/Hyperf/Coroutine/Coroutine.php` ```php @@ -236,57 +239,56 @@ declare(strict_types=1); * This file is part of Hyperf. * * @link https://www.hyperf.io - * @document https://doc.hyperf.io + * @document https://hyperf.wiki * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ + namespace Hyperf\Coroutine; -use App\Kernel\Context\Coroutine as Co; -use Swoole\Coroutine as SwooleCoroutine; -use Hyperf\Context\ApplicationContext; +use App\Kernel\Context\Coroutine as Go; +use Hyperf\Contract\StdoutLoggerInterface; +use Hyperf\Engine\Coroutine as Co; +use Hyperf\Engine\Exception\CoroutineDestroyedException; +use Hyperf\Engine\Exception\RunningInNonCoroutineException; +use Throwable; -/** - * @method static void defer(callable $callable) - */ class Coroutine { - public static function __callStatic($name, $arguments) - { - if (! method_exists(SwooleCoroutine::class, $name)) { - throw new \BadMethodCallException(sprintf('Call to undefined method %s.', $name)); - } - return SwooleCoroutine::$name(...$arguments); - } - /** * Returns the current coroutine ID. * Returns -1 when running in non-coroutine context. */ public static function id(): int { - return SwooleCoroutine::getCid(); + return Co::id(); + } + + public static function defer(callable $callable): void + { + Co::defer(static function () use ($callable) { + try { + $callable(); + } catch (Throwable $exception) { + di()->get(StdoutLoggerInterface::class)->error((string) $exception); + } + }); + } + + public static function sleep(float $seconds): void + { + usleep(intval($seconds * 1000 * 1000)); } /** * Returns the parent coroutine ID. - * Returns -1 when running in the top level coroutine. - * Returns null when running in non-coroutine context. - * - * @see https://github.com/swoole/swoole-src/pull/2669/files#diff-3bdf726b0ac53be7e274b60d59e6ec80R940 + * Returns 0 when running in the top level coroutine. + * @throws RunningInNonCoroutineException when running in non-coroutine context + * @throws CoroutineDestroyedException when the coroutine has been destroyed */ - public static function parentId(?int $coroutineId = null): ?int + public static function parentId(?int $coroutineId = null): int { - if ($coroutineId) { - $cid = SwooleCoroutine::getPcid($coroutineId); - } else { - $cid = SwooleCoroutine::getPcid(); - } - if ($cid === false) { - return null; - } - - return $cid; + return Co::pid($coroutineId); } /** @@ -295,12 +297,22 @@ class Coroutine */ public static function create(callable $callable): int { - return ApplicationContext::getContainer()->get(Co::class)->create($callable); + return di()->get(Go::class)->create($callable); } public static function inCoroutine(): bool { - return Coroutine::id() > 0; + return Co::id() > 0; + } + + public static function stats(): array + { + return Co::stats(); + } + + public static function exists(int $id): bool + { + return Co::exists($id); } } diff --git a/docs/zh-cn/async-queue.md b/docs/zh-cn/async-queue.md index b293eb2dc..3f49d300c 100644 --- a/docs/zh-cn/async-queue.md +++ b/docs/zh-cn/async-queue.md @@ -117,7 +117,7 @@ class AsyncQueueConsumer extends ConsumerProcess ### 如何使用多个配置 -有的开发者会在特殊场景穿件多个配置,比如某些消息要优先处理,所以会放到更加清闲的队列当中。例如以下配置 +有的开发者会在特殊场景创建多个配置,比如某些消息要优先处理,所以会放到更加清闲的队列当中。例如以下配置 ```php 该功能仅在 v1.1.13 及往后的版本上可用 - 要使 [hyperf/constants](https://github.com/hyperf/constants) 组件支持国际化,就必须要安装 [hyperf/translation](https://github.com/hyperf/translation) 组件并配置好语言文件,如下: ``` diff --git a/docs/zh-cn/coroutine.md b/docs/zh-cn/coroutine.md index 41dcf006e..416630ac7 100644 --- a/docs/zh-cn/coroutine.md +++ b/docs/zh-cn/coroutine.md @@ -2,7 +2,7 @@ ## 概念 -Hyperf 是运行于 `Swoole 4` 的协程和 `Swow` 协程之上的,这也是 Hyperf 能提供高性能的其中一个很大的因素。 +Hyperf 是运行于 `Swoole 5` 的协程和 `Swow` 协程之上的,这也是 Hyperf 能提供高性能的其中一个很大的因素。 ### PHP-FPM 的运作模式 diff --git a/docs/zh-cn/di.md b/docs/zh-cn/di.md index e15049b72..8cd21f525 100644 --- a/docs/zh-cn/di.md +++ b/docs/zh-cn/di.md @@ -65,8 +65,7 @@ class IndexController > 注意使用构造函数注入时,调用方也就是 `IndexController` 必须是由 DI 创建的对象才能完成自动注入,而 Controller 默认是由 DI 创建的,所以可以直接使用构造函数注入 -当您希望定义一个可选的依赖项时,可以通过给参数定义为 `nullable` 或将参数的默认值定义为 `null`,即表示该参数如果在 DI 容器中没有找到或无法创建对应的对象时,不抛出异常而是直接使用 `null` 来注入。*(该功能仅在 -1.1.0 或更高版本可用)* +当您希望定义一个可选的依赖项时,可以通过给参数定义为 `nullable` 或将参数的默认值定义为 `null`,即表示该参数如果在 DI 容器中没有找到或无法创建对应的对象时,不抛出异常而是直接使用 `null` 来注入。 ```php container = $container; $this->logger = $container->get(StdoutLoggerInterface::class); - if ($container->has(FormatterInterface::class)) { - $this->formatter = $container->get(FormatterInterface::class); - } } /** @@ -202,23 +200,26 @@ class Coroutine */ public function create(callable $callable): int { - $id = Utils\Coroutine::id(); - $result = SwooleCoroutine::create(function () use ($callable, $id) { + $id = Co::id(); + $coroutine = Co::create(function () use ($callable, $id) { try { - // 按需複製,禁止複製 Socket,不然會導致 Socket 跨協程調用從而報錯。 + // Shouldn't copy all contexts to avoid socket already been bound to another coroutine. Context::copy($id, [ + AppendRequestIdProcessor::REQUEST_ID, ServerRequestInterface::class, ]); - call($callable); + $callable(); } catch (Throwable $throwable) { - if ($this->formatter) { - $this->logger->warning($this->formatter->format($throwable)); - } else { - $this->logger->warning((string) $throwable); - } + $this->logger->warning((string) $throwable); } }); - return is_int($result) ? $result : -1; + + try { + return $coroutine->getId(); + } catch (Throwable $throwable) { + $this->logger->warning((string) $throwable); + return -1; + } } } @@ -226,6 +227,8 @@ class Coroutine 然後,我們實現一個跟 `Hyperf\Coroutine\Coroutine` 一模一樣的對象。其中 `create()` 方法替換成我們上述實現的方法。 +[Coroutine.php](https://github.com/hyperf/biz-skeleton/blob/master/app/Kernel/ClassMap/Coroutine.php) + `class_map/Hyperf/Coroutine/Coroutine.php` ```php @@ -236,57 +239,56 @@ declare(strict_types=1); * This file is part of Hyperf. * * @link https://www.hyperf.io - * @document https://doc.hyperf.io + * @document https://hyperf.wiki * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ + namespace Hyperf\Coroutine; -use App\Kernel\Context\Coroutine as Co; -use Swoole\Coroutine as SwooleCoroutine; -use Hyperf\Context\ApplicationContext; +use App\Kernel\Context\Coroutine as Go; +use Hyperf\Contract\StdoutLoggerInterface; +use Hyperf\Engine\Coroutine as Co; +use Hyperf\Engine\Exception\CoroutineDestroyedException; +use Hyperf\Engine\Exception\RunningInNonCoroutineException; +use Throwable; -/** - * @method static void defer(callable $callable) - */ class Coroutine { - public static function __callStatic($name, $arguments) - { - if (! method_exists(SwooleCoroutine::class, $name)) { - throw new \BadMethodCallException(sprintf('Call to undefined method %s.', $name)); - } - return SwooleCoroutine::$name(...$arguments); - } - /** * Returns the current coroutine ID. * Returns -1 when running in non-coroutine context. */ public static function id(): int { - return SwooleCoroutine::getCid(); + return Co::id(); + } + + public static function defer(callable $callable): void + { + Co::defer(static function () use ($callable) { + try { + $callable(); + } catch (Throwable $exception) { + di()->get(StdoutLoggerInterface::class)->error((string) $exception); + } + }); + } + + public static function sleep(float $seconds): void + { + usleep(intval($seconds * 1000 * 1000)); } /** * Returns the parent coroutine ID. - * Returns -1 when running in the top level coroutine. - * Returns null when running in non-coroutine context. - * - * @see https://github.com/swoole/swoole-src/pull/2669/files#diff-3bdf726b0ac53be7e274b60d59e6ec80R940 + * Returns 0 when running in the top level coroutine. + * @throws RunningInNonCoroutineException when running in non-coroutine context + * @throws CoroutineDestroyedException when the coroutine has been destroyed */ - public static function parentId(?int $coroutineId = null): ?int + public static function parentId(?int $coroutineId = null): int { - if ($coroutineId) { - $cid = SwooleCoroutine::getPcid($coroutineId); - } else { - $cid = SwooleCoroutine::getPcid(); - } - if ($cid === false) { - return null; - } - - return $cid; + return Co::pid($coroutineId); } /** @@ -295,12 +297,22 @@ class Coroutine */ public static function create(callable $callable): int { - return ApplicationContext::getContainer()->get(Co::class)->create($callable); + return di()->get(Go::class)->create($callable); } public static function inCoroutine(): bool { - return Coroutine::id() > 0; + return Co::id() > 0; + } + + public static function stats(): array + { + return Co::stats(); + } + + public static function exists(int $id): bool + { + return Co::exists($id); } } diff --git a/docs/zh-hk/async-queue.md b/docs/zh-hk/async-queue.md index c51142554..d623cb347 100644 --- a/docs/zh-hk/async-queue.md +++ b/docs/zh-hk/async-queue.md @@ -117,7 +117,7 @@ class AsyncQueueConsumer extends ConsumerProcess ### 如何使用多個配置 -有的開發者會在特殊場景穿件多個配置,比如某些消息要優先處理,所以會放到更加清閒的隊列當中。例如以下配置 +有的開發者會在特殊場景創建多個配置,比如某些消息要優先處理,所以會放到更加清閒的隊列當中。例如以下配置 ```php 該功能僅在 v1.1.13 及往後的版本上可用 - 要使 [hyperf/constants](https://github.com/hyperf/constants) 組件支持國際化,就必須要安裝 [hyperf/translation](https://github.com/hyperf/translation) 組件並配置好語言文件,如下: ``` diff --git a/docs/zh-hk/coroutine.md b/docs/zh-hk/coroutine.md index 6e24dab4e..8e0c7f73c 100644 --- a/docs/zh-hk/coroutine.md +++ b/docs/zh-hk/coroutine.md @@ -2,7 +2,7 @@ ## 概念 -Hyperf 是運行於 `Swoole 4` 的協程和 `Swow` 協程之上的,這也是 Hyperf 能提供高性能的其中一個很大的因素。 +Hyperf 是運行於 `Swoole 5` 的協程和 `Swow` 協程之上的,這也是 Hyperf 能提供高性能的其中一個很大的因素。 ### PHP-FPM 的運作模式 diff --git a/docs/zh-hk/di.md b/docs/zh-hk/di.md index b4bd82bf8..43f44abb3 100644 --- a/docs/zh-hk/di.md +++ b/docs/zh-hk/di.md @@ -65,8 +65,7 @@ class IndexController > 注意使用構造函數注入時,調用方也就是 `IndexController` 必須是由 DI 創建的對象才能完成自動注入,而 Controller 默認是由 DI 創建的,所以可以直接使用構造函數注入 -當您希望定義一個可選的依賴項時,可以通過給參數定義為 `nullable` 或將參數的默認值定義為 `null`,即表示該參數如果在 DI 容器中沒有找到或無法創建對應的對象時,不拋出異常而是直接使用 `null` 來注入。*(該功能僅在 -1.1.0 或更高版本可用)* +當您希望定義一個可選的依賴項時,可以通過給參數定義為 `nullable` 或將參數的默認值定義為 `null`,即表示該參數如果在 DI 容器中沒有找到或無法創建對應的對象時,不拋出異常而是直接使用 `null` 來注入。 ```php container = $container; $this->logger = $container->get(StdoutLoggerInterface::class); - if ($container->has(FormatterInterface::class)) { - $this->formatter = $container->get(FormatterInterface::class); - } } /** @@ -202,23 +200,26 @@ class Coroutine */ public function create(callable $callable): int { - $id = Utils\Coroutine::id(); - $result = SwooleCoroutine::create(function () use ($callable, $id) { + $id = Co::id(); + $coroutine = Co::create(function () use ($callable, $id) { try { - // 按需複製,禁止複製 Socket,不然會導致 Socket 跨協程呼叫從而報錯。 + // Shouldn't copy all contexts to avoid socket already been bound to another coroutine. Context::copy($id, [ + AppendRequestIdProcessor::REQUEST_ID, ServerRequestInterface::class, ]); - call($callable); + $callable(); } catch (Throwable $throwable) { - if ($this->formatter) { - $this->logger->warning($this->formatter->format($throwable)); - } else { - $this->logger->warning((string) $throwable); - } + $this->logger->warning((string) $throwable); } }); - return is_int($result) ? $result : -1; + + try { + return $coroutine->getId(); + } catch (Throwable $throwable) { + $this->logger->warning((string) $throwable); + return -1; + } } } @@ -226,6 +227,8 @@ class Coroutine 然後,我們實現一個跟 `Hyperf\Coroutine\Coroutine` 一模一樣的物件。其中 `create()` 方法替換成我們上述實現的方法。 +[Coroutine.php](https://github.com/hyperf/biz-skeleton/blob/master/app/Kernel/ClassMap/Coroutine.php) + `class_map/Hyperf/Coroutine/Coroutine.php` ```php @@ -236,57 +239,56 @@ declare(strict_types=1); * This file is part of Hyperf. * * @link https://www.hyperf.io - * @document https://doc.hyperf.io + * @document https://hyperf.wiki * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ + namespace Hyperf\Coroutine; -use App\Kernel\Context\Coroutine as Co; -use Swoole\Coroutine as SwooleCoroutine; -use Hyperf\Context\ApplicationContext; +use App\Kernel\Context\Coroutine as Go; +use Hyperf\Contract\StdoutLoggerInterface; +use Hyperf\Engine\Coroutine as Co; +use Hyperf\Engine\Exception\CoroutineDestroyedException; +use Hyperf\Engine\Exception\RunningInNonCoroutineException; +use Throwable; -/** - * @method static void defer(callable $callable) - */ class Coroutine { - public static function __callStatic($name, $arguments) - { - if (! method_exists(SwooleCoroutine::class, $name)) { - throw new \BadMethodCallException(sprintf('Call to undefined method %s.', $name)); - } - return SwooleCoroutine::$name(...$arguments); - } - /** * Returns the current coroutine ID. * Returns -1 when running in non-coroutine context. */ public static function id(): int { - return SwooleCoroutine::getCid(); + return Co::id(); + } + + public static function defer(callable $callable): void + { + Co::defer(static function () use ($callable) { + try { + $callable(); + } catch (Throwable $exception) { + di()->get(StdoutLoggerInterface::class)->error((string) $exception); + } + }); + } + + public static function sleep(float $seconds): void + { + usleep(intval($seconds * 1000 * 1000)); } /** * Returns the parent coroutine ID. - * Returns -1 when running in the top level coroutine. - * Returns null when running in non-coroutine context. - * - * @see https://github.com/swoole/swoole-src/pull/2669/files#diff-3bdf726b0ac53be7e274b60d59e6ec80R940 + * Returns 0 when running in the top level coroutine. + * @throws RunningInNonCoroutineException when running in non-coroutine context + * @throws CoroutineDestroyedException when the coroutine has been destroyed */ - public static function parentId(?int $coroutineId = null): ?int + public static function parentId(?int $coroutineId = null): int { - if ($coroutineId) { - $cid = SwooleCoroutine::getPcid($coroutineId); - } else { - $cid = SwooleCoroutine::getPcid(); - } - if ($cid === false) { - return null; - } - - return $cid; + return Co::pid($coroutineId); } /** @@ -295,12 +297,22 @@ class Coroutine */ public static function create(callable $callable): int { - return ApplicationContext::getContainer()->get(Co::class)->create($callable); + return di()->get(Go::class)->create($callable); } public static function inCoroutine(): bool { - return Coroutine::id() > 0; + return Co::id() > 0; + } + + public static function stats(): array + { + return Co::stats(); + } + + public static function exists(int $id): bool + { + return Co::exists($id); } } diff --git a/docs/zh-tw/async-queue.md b/docs/zh-tw/async-queue.md index 4288ed5dc..48ec83651 100644 --- a/docs/zh-tw/async-queue.md +++ b/docs/zh-tw/async-queue.md @@ -117,7 +117,7 @@ class AsyncQueueConsumer extends ConsumerProcess ### 如何使用多個配置 -有的開發者會在特殊場景穿件多個配置,比如某些訊息要優先處理,所以會放到更加清閒的隊列當中。例如以下配置 +有的開發者會在特殊場景建立多個配置,比如某些訊息要優先處理,所以會放到更加清閒的隊列當中。例如以下配置 ```php 該功能僅在 v1.1.13 及往後的版本上可用 - 要使 [hyperf/constants](https://github.com/hyperf/constants) 元件支援國際化,就必須要安裝 [hyperf/translation](https://github.com/hyperf/translation) 元件並配置好語言檔案,如下: ``` diff --git a/docs/zh-tw/coroutine.md b/docs/zh-tw/coroutine.md index 742d0e032..4826f015d 100644 --- a/docs/zh-tw/coroutine.md +++ b/docs/zh-tw/coroutine.md @@ -2,7 +2,7 @@ ## 概念 -Hyperf 是運行於 `Swoole 4` 的協程和 `Swow` 協程之上的,這也是 Hyperf 能提供高效能的其中一個很大的因素。 +Hyperf 是運行於 `Swoole 5` 的協程和 `Swow` 協程之上的,這也是 Hyperf 能提供高效能的其中一個很大的因素。 ### PHP-FPM 的運作模式 diff --git a/docs/zh-tw/di.md b/docs/zh-tw/di.md index 265261c91..58aa93569 100644 --- a/docs/zh-tw/di.md +++ b/docs/zh-tw/di.md @@ -65,8 +65,7 @@ class IndexController > 注意使用建構函式注入時,呼叫方也就是 `IndexController` 必須是由 DI 建立的物件才能完成自動注入,而 Controller 預設是由 DI 建立的,所以可以直接使用建構函式注入 -當您希望定義一個可選的依賴項時,可以透過給引數定義為 `nullable` 或將引數的預設值定義為 `null`,即表示該引數如果在 DI 容器中沒有找到或無法建立對應的物件時,不丟擲異常而是直接使用 `null` 來注入。*(該功能僅在 -1.1.0 或更高版本可用)* +當您希望定義一個可選的依賴項時,可以透過給引數定義為 `nullable` 或將引數的預設值定義為 `null`,即表示該引數如果在 DI 容器中沒有找到或無法建立對應的物件時,不丟擲異常而是直接使用 `null` 來注入。 ```php formatParams($value); - foreach ($formattedValue as $key => $val) { - if (property_exists($this, $key)) { - $this->{$key} = $val; - } - } - } - public function toArray(): array { $properties = ReflectionManager::reflectClass(static::class)->getProperties(ReflectionProperty::IS_PUBLIC); @@ -54,30 +41,4 @@ abstract class AbstractAnnotation implements AnnotationInterface, Arrayable { AnnotationCollector::collectProperty($className, $target, static::class, $this); } - - /** - * @deprecated will be removed in v3.1 - * @param mixed $value - */ - protected function formatParams($value): array - { - if (isset($value[0])) { - $value = $value[0]; - } - if (! is_array($value)) { - $value = ['value' => $value]; - } - return $value; - } - - /** - * @deprecated will be removed in v3.1 - */ - protected function bindMainProperty(string $key, array $value) - { - $formattedValue = $this->formatParams($value); - if (isset($formattedValue['value'])) { - $this->{$key} = $formattedValue['value']; - } - } }