mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-11-29 18:27:44 +08:00
Removed some deprecated methods from Hyperf\Di\Annotation\AbstractAnnotation
. (#6040)
This commit is contained in:
parent
056f72a36d
commit
b6538c14da
@ -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
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
|
||||
<?php
|
||||
|
||||
@ -167,33 +169,29 @@ 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 App\Kernel\Context;
|
||||
|
||||
use App\Kernel\Log\AppendRequestIdProcessor;
|
||||
use Hyperf\Context\Context;
|
||||
use Hyperf\Contract\StdoutLoggerInterface;
|
||||
use Hyperf\ExceptionHandler\Formatter\FormatterInterface;
|
||||
use Hyperf\Utils;
|
||||
use Hyperf\Engine\Coroutine as Co;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Swoole\Coroutine as SwooleCoroutine;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Throwable;
|
||||
|
||||
class Coroutine
|
||||
{
|
||||
protected StdoutLoggerInterface $logger;
|
||||
|
||||
protected ?FormatterInterface $formatter = null;
|
||||
protected LoggerInterface $logger;
|
||||
|
||||
public function __construct(ContainerInterface $container)
|
||||
public function __construct(protected ContainerInterface $container)
|
||||
{
|
||||
$this->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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -117,7 +117,7 @@ class AsyncQueueConsumer extends ConsumerProcess
|
||||
|
||||
### 如何使用多个配置
|
||||
|
||||
有的开发者会在特殊场景穿件多个配置,比如某些消息要优先处理,所以会放到更加清闲的队列当中。例如以下配置
|
||||
有的开发者会在特殊场景创建多个配置,比如某些消息要优先处理,所以会放到更加清闲的队列当中。例如以下配置
|
||||
|
||||
```php
|
||||
<?php
|
||||
@ -155,7 +155,7 @@ return [
|
||||
|
||||
```
|
||||
|
||||
我们默认的 `Hyperf\AsyncQueue\Process\ConsumerProcess` 只会处理 `default` 配置,所以我们需要创建一个新的 Process
|
||||
但是,我们默认的 `Hyperf\AsyncQueue\Process\ConsumerProcess` 只会处理 `default` 配置,所以我们需要创建一个新的 `Process`
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
@ -143,8 +143,6 @@ $message = ErrorCode::getMessage(ErrorCode::PARAMS_INVALID, 'user_id');
|
||||
|
||||
### 国际化
|
||||
|
||||
> 该功能仅在 v1.1.13 及往后的版本上可用
|
||||
|
||||
要使 [hyperf/constants](https://github.com/hyperf/constants) 组件支持国际化,就必须要安装 [hyperf/translation](https://github.com/hyperf/translation) 组件并配置好语言文件,如下:
|
||||
|
||||
```
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
## 概念
|
||||
|
||||
Hyperf 是运行于 `Swoole 4` 的协程和 `Swow` 协程之上的,这也是 Hyperf 能提供高性能的其中一个很大的因素。
|
||||
Hyperf 是运行于 `Swoole 5` 的协程和 `Swow` 协程之上的,这也是 Hyperf 能提供高性能的其中一个很大的因素。
|
||||
|
||||
### PHP-FPM 的运作模式
|
||||
|
||||
|
@ -65,8 +65,7 @@ class IndexController
|
||||
|
||||
> 注意使用构造函数注入时,调用方也就是 `IndexController` 必须是由 DI 创建的对象才能完成自动注入,而 Controller 默认是由 DI 创建的,所以可以直接使用构造函数注入
|
||||
|
||||
当您希望定义一个可选的依赖项时,可以通过给参数定义为 `nullable` 或将参数的默认值定义为 `null`,即表示该参数如果在 DI 容器中没有找到或无法创建对应的对象时,不抛出异常而是直接使用 `null` 来注入。*(该功能仅在
|
||||
1.1.0 或更高版本可用)*
|
||||
当您希望定义一个可选的依赖项时,可以通过给参数定义为 `nullable` 或将参数的默认值定义为 `null`,即表示该参数如果在 DI 容器中没有找到或无法创建对应的对象时,不抛出异常而是直接使用 `null` 来注入。
|
||||
|
||||
```php
|
||||
<?php
|
||||
@ -141,11 +140,9 @@ class IndexController
|
||||
/**
|
||||
* 通过 `#[Inject]` 注解注入由注解声明的属性类型对象
|
||||
* 当 UserService 不存在于 DI 容器内或不可创建时,则注入 null
|
||||
*
|
||||
* @var UserService
|
||||
*/
|
||||
#[Inject(required: false)]
|
||||
private $userService;
|
||||
private ?UserService $userService;
|
||||
|
||||
public function index()
|
||||
{
|
||||
@ -212,11 +209,8 @@ use Hyperf\Di\Annotation\Inject;
|
||||
|
||||
class IndexController
|
||||
{
|
||||
/**
|
||||
* @var UserServiceInterface
|
||||
*/
|
||||
#[Inject]
|
||||
private $userService;
|
||||
private UserServiceInterface $userService;
|
||||
|
||||
public function index()
|
||||
{
|
||||
|
@ -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
|
||||
<?php
|
||||
|
||||
@ -167,33 +169,29 @@ 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 App\Kernel\Context;
|
||||
|
||||
use App\Kernel\Log\AppendRequestIdProcessor;
|
||||
use Hyperf\Context\Context;
|
||||
use Hyperf\Contract\StdoutLoggerInterface;
|
||||
use Hyperf\ExceptionHandler\Formatter\FormatterInterface;
|
||||
use Hyperf\Utils;
|
||||
use Hyperf\Engine\Coroutine as Co;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Swoole\Coroutine as SwooleCoroutine;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Throwable;
|
||||
|
||||
class Coroutine
|
||||
{
|
||||
protected StdoutLoggerInterface $logger;
|
||||
|
||||
protected ?FormatterInterface $formatter = null;
|
||||
protected LoggerInterface $logger;
|
||||
|
||||
public function __construct(ContainerInterface $container)
|
||||
public function __construct(protected ContainerInterface $container)
|
||||
{
|
||||
$this->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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -117,7 +117,7 @@ class AsyncQueueConsumer extends ConsumerProcess
|
||||
|
||||
### 如何使用多個配置
|
||||
|
||||
有的開發者會在特殊場景穿件多個配置,比如某些消息要優先處理,所以會放到更加清閒的隊列當中。例如以下配置
|
||||
有的開發者會在特殊場景創建多個配置,比如某些消息要優先處理,所以會放到更加清閒的隊列當中。例如以下配置
|
||||
|
||||
```php
|
||||
<?php
|
||||
@ -155,7 +155,7 @@ return [
|
||||
|
||||
```
|
||||
|
||||
我們默認的 `Hyperf\AsyncQueue\Process\ConsumerProcess` 只會處理 `default` 配置,所以我們需要創建一個新的 Process
|
||||
但是,我們默認的 `Hyperf\AsyncQueue\Process\ConsumerProcess` 只會處理 `default` 配置,所以我們需要創建一個新的 `Process`
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
@ -143,8 +143,6 @@ $message = ErrorCode::getMessage(ErrorCode::PARAMS_INVALID, 'user_id');
|
||||
|
||||
### 國際化
|
||||
|
||||
> 該功能僅在 v1.1.13 及往後的版本上可用
|
||||
|
||||
要使 [hyperf/constants](https://github.com/hyperf/constants) 組件支持國際化,就必須要安裝 [hyperf/translation](https://github.com/hyperf/translation) 組件並配置好語言文件,如下:
|
||||
|
||||
```
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
## 概念
|
||||
|
||||
Hyperf 是運行於 `Swoole 4` 的協程和 `Swow` 協程之上的,這也是 Hyperf 能提供高性能的其中一個很大的因素。
|
||||
Hyperf 是運行於 `Swoole 5` 的協程和 `Swow` 協程之上的,這也是 Hyperf 能提供高性能的其中一個很大的因素。
|
||||
|
||||
### PHP-FPM 的運作模式
|
||||
|
||||
|
@ -65,8 +65,7 @@ class IndexController
|
||||
|
||||
> 注意使用構造函數注入時,調用方也就是 `IndexController` 必須是由 DI 創建的對象才能完成自動注入,而 Controller 默認是由 DI 創建的,所以可以直接使用構造函數注入
|
||||
|
||||
當您希望定義一個可選的依賴項時,可以通過給參數定義為 `nullable` 或將參數的默認值定義為 `null`,即表示該參數如果在 DI 容器中沒有找到或無法創建對應的對象時,不拋出異常而是直接使用 `null` 來注入。*(該功能僅在
|
||||
1.1.0 或更高版本可用)*
|
||||
當您希望定義一個可選的依賴項時,可以通過給參數定義為 `nullable` 或將參數的默認值定義為 `null`,即表示該參數如果在 DI 容器中沒有找到或無法創建對應的對象時,不拋出異常而是直接使用 `null` 來注入。
|
||||
|
||||
```php
|
||||
<?php
|
||||
@ -141,11 +140,9 @@ class IndexController
|
||||
/**
|
||||
* 通過 `#[Inject]` 註解注入由註解聲明的屬性類型對象
|
||||
* 當 UserService 不存在於 DI 容器內或不可創建時,則注入 null
|
||||
*
|
||||
* @var UserService
|
||||
*/
|
||||
#[Inject(required: false)]
|
||||
private $userService;
|
||||
private ?UserService $userService;
|
||||
|
||||
public function index()
|
||||
{
|
||||
@ -212,11 +209,8 @@ use Hyperf\Di\Annotation\Inject;
|
||||
|
||||
class IndexController
|
||||
{
|
||||
/**
|
||||
* @var UserServiceInterface
|
||||
*/
|
||||
#[Inject]
|
||||
private $userService;
|
||||
private UserServiceInterface $userService;
|
||||
|
||||
public function index()
|
||||
{
|
||||
|
@ -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
|
||||
<?php
|
||||
|
||||
@ -167,33 +169,29 @@ 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 App\Kernel\Context;
|
||||
|
||||
use App\Kernel\Log\AppendRequestIdProcessor;
|
||||
use Hyperf\Context\Context;
|
||||
use Hyperf\Contract\StdoutLoggerInterface;
|
||||
use Hyperf\ExceptionHandler\Formatter\FormatterInterface;
|
||||
use Hyperf\Utils;
|
||||
use Hyperf\Engine\Coroutine as Co;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Swoole\Coroutine as SwooleCoroutine;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Throwable;
|
||||
|
||||
class Coroutine
|
||||
{
|
||||
protected StdoutLoggerInterface $logger;
|
||||
|
||||
protected ?FormatterInterface $formatter = null;
|
||||
protected LoggerInterface $logger;
|
||||
|
||||
public function __construct(ContainerInterface $container)
|
||||
public function __construct(protected ContainerInterface $container)
|
||||
{
|
||||
$this->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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -117,7 +117,7 @@ class AsyncQueueConsumer extends ConsumerProcess
|
||||
|
||||
### 如何使用多個配置
|
||||
|
||||
有的開發者會在特殊場景穿件多個配置,比如某些訊息要優先處理,所以會放到更加清閒的隊列當中。例如以下配置
|
||||
有的開發者會在特殊場景建立多個配置,比如某些訊息要優先處理,所以會放到更加清閒的隊列當中。例如以下配置
|
||||
|
||||
```php
|
||||
<?php
|
||||
@ -155,7 +155,7 @@ return [
|
||||
|
||||
```
|
||||
|
||||
我們預設的 `Hyperf\AsyncQueue\Process\ConsumerProcess` 只會處理 `default` 配置,所以我們需要建立一個新的 Process
|
||||
但是,我們預設的 `Hyperf\AsyncQueue\Process\ConsumerProcess` 只會處理 `default` 配置,所以我們需要建立一個新的 `Process`
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
@ -143,8 +143,6 @@ $message = ErrorCode::getMessage(ErrorCode::PARAMS_INVALID, 'user_id');
|
||||
|
||||
### 國際化
|
||||
|
||||
> 該功能僅在 v1.1.13 及往後的版本上可用
|
||||
|
||||
要使 [hyperf/constants](https://github.com/hyperf/constants) 元件支援國際化,就必須要安裝 [hyperf/translation](https://github.com/hyperf/translation) 元件並配置好語言檔案,如下:
|
||||
|
||||
```
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
## 概念
|
||||
|
||||
Hyperf 是運行於 `Swoole 4` 的協程和 `Swow` 協程之上的,這也是 Hyperf 能提供高效能的其中一個很大的因素。
|
||||
Hyperf 是運行於 `Swoole 5` 的協程和 `Swow` 協程之上的,這也是 Hyperf 能提供高效能的其中一個很大的因素。
|
||||
|
||||
### PHP-FPM 的運作模式
|
||||
|
||||
|
@ -65,8 +65,7 @@ class IndexController
|
||||
|
||||
> 注意使用建構函式注入時,呼叫方也就是 `IndexController` 必須是由 DI 建立的物件才能完成自動注入,而 Controller 預設是由 DI 建立的,所以可以直接使用建構函式注入
|
||||
|
||||
當您希望定義一個可選的依賴項時,可以透過給引數定義為 `nullable` 或將引數的預設值定義為 `null`,即表示該引數如果在 DI 容器中沒有找到或無法建立對應的物件時,不丟擲異常而是直接使用 `null` 來注入。*(該功能僅在
|
||||
1.1.0 或更高版本可用)*
|
||||
當您希望定義一個可選的依賴項時,可以透過給引數定義為 `nullable` 或將引數的預設值定義為 `null`,即表示該引數如果在 DI 容器中沒有找到或無法建立對應的物件時,不丟擲異常而是直接使用 `null` 來注入。
|
||||
|
||||
```php
|
||||
<?php
|
||||
@ -141,11 +140,9 @@ class IndexController
|
||||
/**
|
||||
* 透過 `#[Inject]` 註解注入由註解宣告的屬性型別物件
|
||||
* 當 UserService 不存在於 DI 容器內或不可建立時,則注入 null
|
||||
*
|
||||
* @var UserService
|
||||
*/
|
||||
#[Inject(required: false)]
|
||||
private $userService;
|
||||
private ?UserService $userService;
|
||||
|
||||
public function index()
|
||||
{
|
||||
@ -212,11 +209,8 @@ use Hyperf\Di\Annotation\Inject;
|
||||
|
||||
class IndexController
|
||||
{
|
||||
/**
|
||||
* @var UserServiceInterface
|
||||
*/
|
||||
#[Inject]
|
||||
private $userService;
|
||||
private UserServiceInterface $userService;
|
||||
|
||||
public function index()
|
||||
{
|
||||
|
@ -17,19 +17,6 @@ use ReflectionProperty;
|
||||
|
||||
abstract class AbstractAnnotation implements AnnotationInterface, Arrayable
|
||||
{
|
||||
/**
|
||||
* @deprecated will be removed in v3.1
|
||||
*/
|
||||
public function __construct(...$value)
|
||||
{
|
||||
$formattedValue = $this->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'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user