mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-11-29 18:27:44 +08:00
Update all documents about annotations. (#4264)
Co-authored-by: zmy <my.zhu@knowyourself.cc> Co-authored-by: 沈唁 <52o@qq52o.cn> Co-authored-by: 李铭昕 <l@hyperf.io>
This commit is contained in:
parent
804c237b87
commit
e1544beec5
@ -84,9 +84,7 @@ use Hyperf\Amqp\Annotation\Producer;
|
||||
use Hyperf\Amqp\Message\ProducerMessage;
|
||||
use App\Models\User;
|
||||
|
||||
/**
|
||||
* @Producer(exchange="hyperf", routingKey="hyperf")
|
||||
*/
|
||||
#[Producer(exchange: "hyperf", routingKey: "hyperf")]
|
||||
class DemoProducer extends ProducerMessage
|
||||
{
|
||||
public function __construct($id)
|
||||
@ -144,9 +142,7 @@ use Hyperf\Amqp\Message\ConsumerMessage;
|
||||
use Hyperf\Amqp\Result;
|
||||
use PhpAmqpLib\Message\AMQPMessage;
|
||||
|
||||
/**
|
||||
* @Consumer(exchange="hyperf", routingKey="hyperf", queue="hyperf", nums=1)
|
||||
*/
|
||||
#[Consumer(exchange: "hyperf", routingKey: "hyperf", queue: "hyperf", nums: 1)]
|
||||
class DemoConsumer extends ConsumerMessage
|
||||
{
|
||||
public function consumeMessage($data, AMQPMessage $message): string
|
||||
@ -176,9 +172,7 @@ use Hyperf\Amqp\Message\ConsumerMessage;
|
||||
use Hyperf\Amqp\Result;
|
||||
use PhpAmqpLib\Message\AMQPMessage;
|
||||
|
||||
/**
|
||||
* @Consumer(exchange="hyperf", routingKey="hyperf", queue="hyperf", nums=1, enable=false)
|
||||
*/
|
||||
#[Consumer(exchange: "hyperf", routingKey: "hyperf", queue: "hyperf", nums: 1, enable: false)]
|
||||
class DemoConsumer extends ConsumerMessage
|
||||
{
|
||||
public function consumeMessage($data, AMQPMessage $message): string
|
||||
@ -242,9 +236,7 @@ use Hyperf\Amqp\Message\ProducerDelayedMessageTrait;
|
||||
use Hyperf\Amqp\Message\ProducerMessage;
|
||||
use Hyperf\Amqp\Message\Type;
|
||||
|
||||
/**
|
||||
* @Producer()
|
||||
*/
|
||||
#[Producer]
|
||||
class DelayDirectProducer extends ProducerMessage
|
||||
{
|
||||
use ProducerDelayedMessageTrait;
|
||||
@ -286,9 +278,7 @@ use Hyperf\Amqp\Message\Type;
|
||||
use Hyperf\Amqp\Result;
|
||||
use PhpAmqpLib\Message\AMQPMessage;
|
||||
|
||||
/**
|
||||
* @Consumer(nums=1)
|
||||
*/
|
||||
#[Consumer(nums: 1)]
|
||||
class DelayDirectConsumer extends ConsumerMessage
|
||||
{
|
||||
use ProducerDelayedMessageTrait;
|
||||
@ -333,15 +323,10 @@ use Hyperf\Command\Command as HyperfCommand;
|
||||
use Hyperf\Utils\ApplicationContext;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
/**
|
||||
* @Command
|
||||
*/
|
||||
#[Command]
|
||||
class DelayCommand extends HyperfCommand
|
||||
{
|
||||
/**
|
||||
* @var ContainerInterface
|
||||
*/
|
||||
protected $container;
|
||||
protected ContainerInterface $container;
|
||||
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
@ -367,7 +352,6 @@ class DelayCommand extends HyperfCommand
|
||||
$message->setDelayMs(5000);
|
||||
$producer = ApplicationContext::getContainer()->get(Producer::class);
|
||||
$producer->produce($message);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -398,9 +382,7 @@ use Hyperf\Amqp\Message\ConsumerMessage;
|
||||
use Hyperf\Amqp\Result;
|
||||
use PhpAmqpLib\Message\AMQPMessage;
|
||||
|
||||
/**
|
||||
* @Consumer(exchange="hyperf", routingKey="hyperf", queue="rpc.reply", name="ReplyConsumer", nums=1, enable=true)
|
||||
*/
|
||||
#[Consumer(exchange: "hyperf", routingKey: "hyperf", queue: "rpc.reply", name: "ReplyConsumer", nums: 1, enable: true)]
|
||||
class ReplyConsumer extends ConsumerMessage
|
||||
{
|
||||
public function consumeMessage($data, AMQPMessage $message): string
|
||||
|
@ -100,19 +100,13 @@ namespace App\Annotation;
|
||||
|
||||
use Hyperf\Di\Annotation\AbstractAnnotation;
|
||||
|
||||
/**
|
||||
* @Annotation
|
||||
* @Target({"METHOD","PROPERTY"})
|
||||
*/
|
||||
#[Attribute(Attribute::TARGET_PROPERTY)]
|
||||
class Bar extends AbstractAnnotation
|
||||
{
|
||||
// some code
|
||||
}
|
||||
|
||||
/**
|
||||
* @Annotation
|
||||
* @Target("CLASS")
|
||||
*/
|
||||
#[Attribute(Attribute::TARGET_CLASS)]
|
||||
class Foo extends AbstractAnnotation
|
||||
{
|
||||
// some code
|
||||
@ -215,20 +209,9 @@ use Swoole\Coroutine as SwooleCoroutine;
|
||||
|
||||
class Coroutine
|
||||
{
|
||||
/**
|
||||
* @var ContainerInterface
|
||||
*/
|
||||
protected $container;
|
||||
|
||||
/**
|
||||
* @var StdoutLoggerInterface
|
||||
*/
|
||||
protected $logger;
|
||||
|
||||
/**
|
||||
* @var null|FormatterInterface
|
||||
*/
|
||||
protected $formatter;
|
||||
protected StdoutLoggerInterface $logger;
|
||||
|
||||
protected ?FormatterInterface $formatter = null;
|
||||
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
|
@ -29,9 +29,7 @@ use Hyperf\Di\Annotation\Aspect;
|
||||
use Hyperf\Di\Aop\AbstractAspect;
|
||||
use Hyperf\Di\Aop\ProceedingJoinPoint;
|
||||
|
||||
/**
|
||||
* @Aspect
|
||||
*/
|
||||
#[Aspect]
|
||||
class FooAspect extends AbstractAspect
|
||||
{
|
||||
// 要切入的类或 Trait,可以多个,亦可通过 :: 标识到具体的某个方法,通过 * 可以模糊匹配
|
||||
@ -74,18 +72,18 @@ use Hyperf\Di\Annotation\Aspect;
|
||||
use Hyperf\Di\Aop\AbstractAspect;
|
||||
use Hyperf\Di\Aop\ProceedingJoinPoint;
|
||||
|
||||
/**
|
||||
* @Aspect(
|
||||
* classes={
|
||||
* SomeClass::class,
|
||||
* "App\Service\SomeClass::someMethod",
|
||||
* "App\Service\SomeClass::*Method"
|
||||
* },
|
||||
* annotations={
|
||||
* SomeAnnotation::class
|
||||
* }
|
||||
* )
|
||||
*/
|
||||
#[
|
||||
Aspect(
|
||||
classes: [
|
||||
SomeClass::class,
|
||||
"App\Service\SomeClass::someMethod",
|
||||
"App\Service\SomeClass::*Method"
|
||||
],
|
||||
annotations: [
|
||||
SomeAnnotation::class
|
||||
]
|
||||
)
|
||||
]
|
||||
class FooAspect extends AbstractAspect
|
||||
{
|
||||
public function process(ProceedingJoinPoint $proceedingJoinPoint)
|
||||
|
@ -109,9 +109,7 @@ namespace App\Process;
|
||||
use Hyperf\AsyncQueue\Process\ConsumerProcess;
|
||||
use Hyperf\Process\Annotation\Process;
|
||||
|
||||
/**
|
||||
* @Process(name="async-queue")
|
||||
*/
|
||||
#[Process(name: "async-queue")]
|
||||
class AsyncQueueConsumer extends ConsumerProcess
|
||||
{
|
||||
}
|
||||
@ -176,10 +174,8 @@ class ExampleJob extends Job
|
||||
|
||||
/**
|
||||
* 任务执行失败后的重试次数,即最大执行次数为 $maxAttempts+1 次
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $maxAttempts = 2;
|
||||
protected int $maxAttempts = 2;
|
||||
|
||||
public function __construct($params)
|
||||
{
|
||||
@ -212,10 +208,7 @@ use Hyperf\AsyncQueue\Driver\DriverInterface;
|
||||
|
||||
class QueueService
|
||||
{
|
||||
/**
|
||||
* @var DriverInterface
|
||||
*/
|
||||
protected $driver;
|
||||
protected DriverInterface $driver;
|
||||
|
||||
public function __construct(DriverFactory $driverFactory)
|
||||
{
|
||||
@ -252,16 +245,11 @@ use App\Service\QueueService;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Hyperf\HttpServer\Annotation\AutoController;
|
||||
|
||||
/**
|
||||
* @AutoController
|
||||
*/
|
||||
#[AutoController]
|
||||
class QueueController extends AbstractController
|
||||
{
|
||||
/**
|
||||
* @Inject
|
||||
* @var QueueService
|
||||
*/
|
||||
protected $service;
|
||||
#[Inject]
|
||||
protected QueueService $service;
|
||||
|
||||
/**
|
||||
* 传统模式投递消息
|
||||
@ -299,9 +287,7 @@ use Hyperf\AsyncQueue\Annotation\AsyncQueueMessage;
|
||||
|
||||
class QueueService
|
||||
{
|
||||
/**
|
||||
* @AsyncQueueMessage
|
||||
*/
|
||||
#[AsyncQueueMessage]
|
||||
public function example($params)
|
||||
{
|
||||
// 需要异步执行的代码逻辑
|
||||
@ -327,15 +313,13 @@ use App\Service\QueueService;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Hyperf\HttpServer\Annotation\AutoController;
|
||||
|
||||
/**
|
||||
* @AutoController
|
||||
*/
|
||||
#[AutoController]
|
||||
class QueueController extends AbstractController
|
||||
{
|
||||
/**
|
||||
* @Inject
|
||||
* @var QueueService
|
||||
*/
|
||||
#[Inject]
|
||||
protected $service;
|
||||
|
||||
/**
|
||||
@ -468,15 +452,10 @@ namespace App\Process;
|
||||
use Hyperf\AsyncQueue\Process\ConsumerProcess;
|
||||
use Hyperf\Process\Annotation\Process;
|
||||
|
||||
/**
|
||||
* @Process()
|
||||
*/
|
||||
#[Process]
|
||||
class OtherConsumerProcess extends ConsumerProcess
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $queue = 'other';
|
||||
protected string $queue = 'other';
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -56,9 +56,7 @@ use Hyperf\Cache\Annotation\Cacheable;
|
||||
|
||||
class UserService
|
||||
{
|
||||
/**
|
||||
* @Cacheable(prefix="user", ttl=9000, listener="user-update")
|
||||
*/
|
||||
#[Cacheable(prefix: "user", ttl: 9000, listener: "user-update")]
|
||||
public function user($id)
|
||||
{
|
||||
$user = User::query()->where('id',$id)->first();
|
||||
@ -89,11 +87,8 @@ use Psr\EventDispatcher\EventDispatcherInterface;
|
||||
|
||||
class SystemService
|
||||
{
|
||||
/**
|
||||
* @Inject
|
||||
* @var EventDispatcherInterface
|
||||
*/
|
||||
protected $dispatcher;
|
||||
#[Inject]
|
||||
protected EventDispatcherInterface $dispatcher;
|
||||
|
||||
public function flushCache($userId)
|
||||
{
|
||||
@ -117,9 +112,8 @@ use Hyperf\Cache\Annotation\Cacheable;
|
||||
|
||||
class DemoService
|
||||
{
|
||||
/**
|
||||
* @Cacheable(prefix="cache", value="_#{id}", listener="user-update")
|
||||
*/
|
||||
|
||||
#[Cacheable(prefix: "cache", value: "_#{id}", listener: "user-update")]
|
||||
public function getCache(int $id)
|
||||
{
|
||||
return $id . '_' . uniqid();
|
||||
@ -142,11 +136,8 @@ use Psr\EventDispatcher\EventDispatcherInterface;
|
||||
|
||||
class SystemService
|
||||
{
|
||||
/**
|
||||
* @Inject
|
||||
* @var EventDispatcherInterface
|
||||
*/
|
||||
protected $dispatcher;
|
||||
#[Inject]
|
||||
protected EventDispatcherInterface $dispatcher;
|
||||
|
||||
public function flushCache($userId)
|
||||
{
|
||||
@ -164,38 +155,52 @@ class SystemService
|
||||
例如以下配置,缓存前缀为 `user`, 超时时间为 `7200`, 删除事件名为 `USER_CACHE`。生成对应缓存 KEY 为 `c:user:1`。
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Models\User;
|
||||
use Hyperf\Cache\Annotation\Cacheable;
|
||||
|
||||
/**
|
||||
* @Cacheable(prefix="user", ttl=7200, listener="USER_CACHE")
|
||||
*/
|
||||
public function user(int $id): array
|
||||
class UserService
|
||||
{
|
||||
$user = User::query()->find($id);
|
||||
#[Cacheable(prefix: "user", ttl: 7200, listener: "USER_CACHE")]
|
||||
public function user(int $id): array
|
||||
{
|
||||
$user = User::query()->find($id);
|
||||
|
||||
return [
|
||||
'user' => $user->toArray(),
|
||||
'uuid' => $this->unique(),
|
||||
];
|
||||
return [
|
||||
'user' => $user->toArray(),
|
||||
'uuid' => $this->unique(),
|
||||
];
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
当设置 `value` 后,框架会根据设置的规则,进行缓存 `KEY` 键命名。如下实例,当 `$user->id = 1` 时,缓存 `KEY` 为 `c:userBook:_1`
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Models\User;
|
||||
use Hyperf\Cache\Annotation\Cacheable;
|
||||
|
||||
/**
|
||||
* @Cacheable(prefix="userBook", ttl=6666, value="_#{user.id}")
|
||||
*/
|
||||
public function userBook(User $user): array
|
||||
class UserBookService
|
||||
{
|
||||
return [
|
||||
'book' => $user->book->toArray(),
|
||||
'uuid' => $this->unique(),
|
||||
];
|
||||
#[Cacheable(prefix: "userBook", ttl: 6666, value: "_#{user.id}")]
|
||||
public function userBook(User $user): array
|
||||
{
|
||||
return [
|
||||
'book' => $user->book->toArray(),
|
||||
'uuid' => $this->unique(),
|
||||
];
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@ -204,22 +209,29 @@ public function userBook(User $user): array
|
||||
`CachePut` 不同于 `Cacheable`,它每次调用都会执行函数体,然后再对缓存进行重写。所以当我们想更新缓存时,可以调用相关方法。
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Models\User;
|
||||
use Hyperf\Cache\Annotation\CachePut;
|
||||
|
||||
/**
|
||||
* @CachePut(prefix="user", ttl=3601)
|
||||
*/
|
||||
public function updateUser(int $id)
|
||||
class UserService
|
||||
{
|
||||
$user = User::query()->find($id);
|
||||
$user->name = 'HyperfDoc';
|
||||
$user->save();
|
||||
#[CachePut(prefix: "user", ttl: 3601)]
|
||||
public function updateUser(int $id)
|
||||
{
|
||||
$user = User::query()->find($id);
|
||||
$user->name = 'HyperfDoc';
|
||||
$user->save();
|
||||
|
||||
return [
|
||||
'user' => $user->toArray(),
|
||||
'uuid' => $this->unique(),
|
||||
];
|
||||
return [
|
||||
'user' => $user->toArray(),
|
||||
'uuid' => $this->unique(),
|
||||
];
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@ -228,14 +240,21 @@ public function updateUser(int $id)
|
||||
CacheEvict 更容易理解了,当执行方法体后,会主动清理缓存。
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use Hyperf\Cache\Annotation\CacheEvict;
|
||||
|
||||
/**
|
||||
* @CacheEvict(prefix="userBook", value="_#{id}")
|
||||
*/
|
||||
public function updateUserBook(int $id)
|
||||
class UserBookService
|
||||
{
|
||||
return true;
|
||||
#[CacheEvict(prefix: "userBook", value: "_#{id}")]
|
||||
public function updateUserBook(int $id)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@ -253,16 +272,14 @@ public function updateUserBook(int $id)
|
||||
<?php
|
||||
use Hyperf\Cache\Annotation\Cacheable;
|
||||
|
||||
class Demo {
|
||||
|
||||
class Demo
|
||||
{
|
||||
public function get($userId, $id)
|
||||
{
|
||||
return $this->getArray($userId)[$id] ?? 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Cacheable(prefix="test", group="co")
|
||||
*/
|
||||
#[Cacheable(prefix: "test", group: "co")]
|
||||
public function getArray(int $userId): array
|
||||
{
|
||||
return $this->redis->hGetAll($userId);
|
||||
|
@ -1182,8 +1182,8 @@
|
||||
```
|
||||
class Example {
|
||||
/**
|
||||
* @Inject
|
||||
*/
|
||||
* @Inject
|
||||
*/
|
||||
private ExampleService $exampleService;
|
||||
}
|
||||
```
|
||||
|
@ -27,15 +27,10 @@ use Hyperf\Di\Annotation\Inject;
|
||||
|
||||
class UserService
|
||||
{
|
||||
/**
|
||||
* @Inject
|
||||
* @var UserServiceClient
|
||||
*/
|
||||
private $client;
|
||||
#[Inject]
|
||||
private UserServiceClient $client;
|
||||
|
||||
/**
|
||||
* @CircuitBreaker(timeout=0.05, failCounter=1, successCounter=1, fallback="App\Service\UserService::searchFallback")
|
||||
*/
|
||||
#[CircuitBreaker(timeout: 0.05, failCounter: 1, successCounter: 1, fallback: "App\Service\UserService::searchFallback")]
|
||||
public function search($offset, $limit)
|
||||
{
|
||||
return $this->client->users($offset, $limit);
|
||||
|
@ -41,17 +41,13 @@ namespace App\Command;
|
||||
use Hyperf\Command\Command as HyperfCommand;
|
||||
use Hyperf\Command\Annotation\Command;
|
||||
|
||||
/**
|
||||
* @Command
|
||||
*/
|
||||
#[Command]
|
||||
class FooCommand extends HyperfCommand
|
||||
{
|
||||
/**
|
||||
* 执行的命令行
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'foo:hello';
|
||||
protected string $name = 'foo:hello';
|
||||
}
|
||||
```
|
||||
|
||||
@ -67,9 +63,7 @@ namespace App\Command;
|
||||
use Hyperf\Command\Command as HyperfCommand;
|
||||
use Hyperf\Command\Annotation\Command;
|
||||
|
||||
/**
|
||||
* @Command
|
||||
*/
|
||||
#[Command]
|
||||
class FooCommand extends HyperfCommand
|
||||
{
|
||||
public function __construct()
|
||||
@ -93,17 +87,13 @@ namespace App\Command;
|
||||
use Hyperf\Command\Command as HyperfCommand;
|
||||
use Hyperf\Command\Annotation\Command;
|
||||
|
||||
/**
|
||||
* @Command
|
||||
*/
|
||||
#[Command]
|
||||
class FooCommand extends HyperfCommand
|
||||
{
|
||||
/**
|
||||
* 执行的命令行
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'foo:hello';
|
||||
protected string $name = 'foo:hello';
|
||||
|
||||
public function handle()
|
||||
{
|
||||
@ -132,17 +122,13 @@ use Hyperf\Command\Annotation\Command;
|
||||
use Hyperf\Command\Command as HyperfCommand;
|
||||
use Symfony\Component\Console\Input\InputArgument;
|
||||
|
||||
/**
|
||||
* @Command
|
||||
*/
|
||||
#[Command]
|
||||
class FooCommand extends HyperfCommand
|
||||
{
|
||||
/**
|
||||
* 执行的命令行
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $name = 'foo:hello';
|
||||
protected string $name = 'foo:hello';
|
||||
|
||||
public function handle()
|
||||
{
|
||||
@ -387,15 +373,10 @@ use Hyperf\Command\Annotation\Command;
|
||||
use Hyperf\Command\Command as HyperfCommand;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
/**
|
||||
* @Command
|
||||
*/
|
||||
#[Command]
|
||||
class DebugCommand extends HyperfCommand
|
||||
{
|
||||
/**
|
||||
* @var ContainerInterface
|
||||
*/
|
||||
protected $container;
|
||||
protected ContainerInterface $container;
|
||||
|
||||
protected $signature = 'test:test {id : user_id} {--name= : user_name}';
|
||||
|
||||
@ -443,15 +424,10 @@ use Hyperf\Command\Command as HyperfCommand;
|
||||
use Hyperf\Command\Annotation\Command;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
/**
|
||||
* @Command
|
||||
*/
|
||||
#[Command]
|
||||
class FooCommand extends HyperfCommand
|
||||
{
|
||||
/**
|
||||
* @var ContainerInterface
|
||||
*/
|
||||
protected $container;
|
||||
protected ContainerInterface $container;
|
||||
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
|
@ -48,9 +48,7 @@ namespace App\Constants;
|
||||
use Hyperf\Constants\AbstractConstants;
|
||||
use Hyperf\Constants\Annotation\Constants;
|
||||
|
||||
/**
|
||||
* @Constants
|
||||
*/
|
||||
#[Constants]
|
||||
class ErrorCode extends AbstractConstants
|
||||
{
|
||||
/**
|
||||
@ -128,9 +126,7 @@ class IndexController extends AbstractController
|
||||
use Hyperf\Constants\AbstractConstants;
|
||||
use Hyperf\Constants\Annotation\Constants;
|
||||
|
||||
/**
|
||||
* @Constants
|
||||
*/
|
||||
#[Constants]
|
||||
class ErrorCode extends AbstractConstants
|
||||
{
|
||||
/**
|
||||
@ -169,9 +165,7 @@ return [
|
||||
use Hyperf\Constants\AbstractConstants;
|
||||
use Hyperf\Constants\Annotation\Constants;
|
||||
|
||||
/**
|
||||
* @Constants
|
||||
*/
|
||||
#[Constants]
|
||||
class ErrorCode extends AbstractConstants
|
||||
{
|
||||
/**
|
||||
|
@ -73,25 +73,18 @@ use Hyperf\Contract\StdoutLoggerInterface;
|
||||
use Hyperf\Crontab\Annotation\Crontab;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
|
||||
/**
|
||||
* @Crontab(name="Foo", rule="* * * * *", callback="execute", memo="这是一个示例的定时任务")
|
||||
*/
|
||||
#[Crontab(name: "Foo", rule: "* * * * *", callback: "execute", memo: "这是一个示例的定时任务")]
|
||||
class FooTask
|
||||
{
|
||||
/**
|
||||
* @Inject()
|
||||
* @var \Hyperf\Contract\StdoutLoggerInterface
|
||||
*/
|
||||
private $logger;
|
||||
#[Inject]
|
||||
private StdoutLoggerInterface $logger;
|
||||
|
||||
public function execute()
|
||||
{
|
||||
$this->logger->info(date('Y-m-d H:i:s', time()));
|
||||
}
|
||||
|
||||
/**
|
||||
* @Crontab(rule="* * * * * *", memo="foo")
|
||||
*/
|
||||
|
||||
#[Crontab(rule: "* * * * *", memo: "foo")]
|
||||
public function foo()
|
||||
{
|
||||
var_dump('foo');
|
||||
@ -149,9 +142,7 @@ namespace App\Crontab;
|
||||
use Carbon\Carbon;
|
||||
use Hyperf\Crontab\Annotation\Crontab;
|
||||
|
||||
/**
|
||||
* @Crontab(name="Echo", rule="* * * * * *", callback="execute", enable="isEnable", memo="这是一个示例的定时任务")
|
||||
*/
|
||||
#[Crontab(name: "Echo", rule: "* * * * *", callback: "execute", enable: "isEnable", memo: "这是一个示例的定时任务")]
|
||||
class EchoCrontab
|
||||
{
|
||||
public function execute()
|
||||
@ -190,9 +181,7 @@ namespace App\Crontab;
|
||||
use Carbon\Carbon;
|
||||
use Hyperf\Crontab\Annotation\Crontab;
|
||||
|
||||
/**
|
||||
* @Crontab(name="Echo", rule="* * * * * *", callback="execute", enable={EnableChecker::class, "isEnable"}, memo="这是一个示例的定时任务")
|
||||
*/
|
||||
#[Crontab(name: "Echo", rule: "* * * * *", callback: "execute", enable: [EnableChecker::class, "isEnable"], memo: "这是一个示例的定时任务")]
|
||||
class EchoCrontab
|
||||
{
|
||||
public function execute()
|
||||
|
@ -35,15 +35,10 @@ use Hyperf\Utils\Str;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
/**
|
||||
* @Listener
|
||||
*/
|
||||
#[Listener]
|
||||
class DbQueryExecutedListener implements ListenerInterface
|
||||
{
|
||||
/**
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
private $logger;
|
||||
private LoggerInterface $logger;
|
||||
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
@ -164,9 +159,7 @@ use Hyperf\Event\Annotation\Listener;
|
||||
use Hyperf\Event\Contract\ListenerInterface;
|
||||
use Hyperf\ModelCache\CacheableInterface;
|
||||
|
||||
/**
|
||||
* @Listener
|
||||
*/
|
||||
#[Listener]
|
||||
class DeleteCacheListener implements ListenerInterface
|
||||
{
|
||||
public function listen(): array
|
||||
|
@ -6,16 +6,10 @@
|
||||
|
||||
Hyperf 提供了创建模型的命令,您可以很方便的根据数据表创建对应模型。命令通过 `AST` 生成模型,所以当您增加了某些方法后,也可以使用脚本方便的重置模型。
|
||||
|
||||
1.1.0 + 版本:
|
||||
```
|
||||
$ php bin/hyperf.php gen:model table_name
|
||||
```
|
||||
|
||||
1.0.* 版本:
|
||||
```
|
||||
$ php bin/hyperf.php db:model table_name
|
||||
```
|
||||
|
||||
可选参数如下:
|
||||
|
||||
| 参数 | 类型 | 默认值 | 备注 |
|
||||
|
@ -44,9 +44,7 @@ use Hyperf\Event\Annotation\Listener;
|
||||
use Hyperf\Event\Contract\ListenerInterface;
|
||||
use PDO;
|
||||
|
||||
/**
|
||||
* @Listener
|
||||
*/
|
||||
#[Listener]
|
||||
class FetchModeListener implements ListenerInterface
|
||||
{
|
||||
public function listen(): array
|
||||
|
@ -429,9 +429,7 @@ use Hyperf\Event\Annotation\Listener;
|
||||
use Hyperf\Event\Contract\ListenerInterface;
|
||||
use Hyperf\Framework\Event\BootApplication;
|
||||
|
||||
/**
|
||||
* @Listener
|
||||
*/
|
||||
#[Listener]
|
||||
class MorphMapRelationListener implements ListenerInterface
|
||||
{
|
||||
public function listen(): array
|
||||
|
@ -46,10 +46,7 @@ use App\Service\UserService;
|
||||
|
||||
class IndexController
|
||||
{
|
||||
/**
|
||||
* @var UserService
|
||||
*/
|
||||
private $userService;
|
||||
private UserService $userService;
|
||||
|
||||
// 通过在构造函数的参数上声明参数类型完成自动注入
|
||||
public function __construct(UserService $userService)
|
||||
@ -79,10 +76,7 @@ use App\Service\UserService;
|
||||
|
||||
class IndexController
|
||||
{
|
||||
/**
|
||||
* @var null|UserService
|
||||
*/
|
||||
private $userService;
|
||||
private ?UserService $userService;
|
||||
|
||||
// 通过设置参数为 nullable,表明该参数为一个可选参数
|
||||
public function __construct(?UserService $userService)
|
||||
@ -102,7 +96,7 @@ class IndexController
|
||||
}
|
||||
```
|
||||
|
||||
#### 通过 `@Inject` 注解注入
|
||||
#### 通过 `#[Inject]` 注解注入
|
||||
|
||||
```php
|
||||
<?php
|
||||
@ -114,11 +108,11 @@ use Hyperf\Di\Annotation\Inject;
|
||||
class IndexController
|
||||
{
|
||||
/**
|
||||
* 通过 `@Inject` 注解注入由 `@var` 注解声明的属性类型对象
|
||||
* 通过 `#[Inject]` 注解注入由 `@var` 注解声明的属性类型对象
|
||||
*
|
||||
* @Inject
|
||||
* @var UserService
|
||||
*/
|
||||
#[Inject]
|
||||
private $userService;
|
||||
|
||||
public function index()
|
||||
@ -130,9 +124,9 @@ class IndexController
|
||||
}
|
||||
```
|
||||
|
||||
> 通过 `@Inject` 注解注入可作用于 DI 创建的(单例)对象,也可作用于通过 `new` 关键词创建的对象;
|
||||
> 通过 `#[Inject]` 注解注入可作用于 DI 创建的(单例)对象,也可作用于通过 `new` 关键词创建的对象;
|
||||
|
||||
> 使用 `@Inject` 注解时需 `use Hyperf\Di\Annotation\Inject;` 命名空间;
|
||||
> 使用 `#[Inject]` 注解时需 `use Hyperf\Di\Annotation\Inject;` 命名空间;
|
||||
|
||||
##### Required 参数
|
||||
|
||||
@ -149,12 +143,12 @@ use Hyperf\Di\Annotation\Inject;
|
||||
class IndexController
|
||||
{
|
||||
/**
|
||||
* 通过 `@Inject` 注解注入由 `@var` 注解声明的属性类型对象
|
||||
* 通过 `#[Inject]` 注解注入由 `@var` 注解声明的属性类型对象
|
||||
* 当 UserService 不存在于 DI 容器内或不可创建时,则注入 null
|
||||
*
|
||||
* @Inject(required=false)
|
||||
* @var UserService
|
||||
*/
|
||||
#[Inject(required: false)]
|
||||
private $userService;
|
||||
|
||||
public function index()
|
||||
@ -223,9 +217,9 @@ use Hyperf\Di\Annotation\Inject;
|
||||
class IndexController
|
||||
{
|
||||
/**
|
||||
* @Inject
|
||||
* @var UserServiceInterface
|
||||
*/
|
||||
#[Inject]
|
||||
private $userService;
|
||||
|
||||
public function index()
|
||||
@ -272,11 +266,7 @@ namespace App\Service;
|
||||
|
||||
class UserService implements UserServiceInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $enableCache;
|
||||
private bool $enableCache;
|
||||
|
||||
public function __construct(bool $enableCache)
|
||||
{
|
||||
@ -347,17 +337,18 @@ class Foo{
|
||||
}
|
||||
````
|
||||
|
||||
您还可以通过注解 `@Inject(lazy=true)` 注入懒加载代理。通过注解实现懒加载不用创建配置文件。
|
||||
您还可以通过注解 `#[Inject(lazy: true)]` 注入懒加载代理。通过注解实现懒加载不用创建配置文件。
|
||||
|
||||
```php
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use App\Service\UserServiceInterface;
|
||||
|
||||
class Foo{
|
||||
class Foo
|
||||
{
|
||||
/**
|
||||
* @Inject(lazy=true)
|
||||
* @var UserServiceInterface
|
||||
*/
|
||||
#[Inject(lazy: true)]
|
||||
public $service;
|
||||
}
|
||||
````
|
||||
@ -395,7 +386,7 @@ $userService = make(UserService::class, ['enableCache' => true]);
|
||||
## 获取容器对象
|
||||
|
||||
有些时候我们可能希望去实现一些更动态的需求时,会希望可以直接获取到 `容器(Container)` 对象,在绝大部分情况下,框架的入口类(比如命令类、控制器、RPC 服务提供者等)都是由 `容器(Container)`
|
||||
创建并维护的,也就意味着您所写的绝大部分业务代码都是在 `容器(Container)` 的管理作用之下的,也就意味着在绝大部分情况下您都可以通过在 `构造函数(Constructor)` 声明或通过 `@Inject`
|
||||
创建并维护的,也就意味着您所写的绝大部分业务代码都是在 `容器(Container)` 的管理作用之下的,也就意味着在绝大部分情况下您都可以通过在 `构造函数(Constructor)` 声明或通过 `#[Inject]`
|
||||
注解注入 `Psr\Container\ContainerInterface` 接口类都能够获得 `Hyperf\Di\Container` 容器对象,我们通过代码来演示一下:
|
||||
|
||||
```php
|
||||
@ -407,10 +398,7 @@ use Psr\Container\ContainerInterface;
|
||||
|
||||
class IndexController
|
||||
{
|
||||
/**
|
||||
* @var ContainerInterface
|
||||
*/
|
||||
private $container;
|
||||
private ContainerInterface $container;
|
||||
|
||||
// 通过在构造函数的参数上声明参数类型完成自动注入
|
||||
public function __construct(ContainerInterface $container)
|
||||
@ -434,9 +422,9 @@ $container = \Hyperf\Utils\ApplicationContext::getContainer();
|
||||
换种方式理解就是容器内管理的对象**都是单例**,这样的设计对于长生命周期的应用来说会更加的高效,减少了大量无意义的对象创建和销毁,这样的设计也就意味着所有需要交由 DI 容器管理的对象**均不能包含** `状态` 值。
|
||||
`状态` 可直接理解为会随着请求而变化的值,事实上在 [协程](zh-cn/coroutine.md) 编程中,这些状态值也是应该存放于 `协程上下文` 中的,即 `Hyperf\Utils\Context`。
|
||||
|
||||
### @Inject 注入覆盖顺序
|
||||
### #[Inject] 注入覆盖顺序
|
||||
|
||||
`@Inject` 覆盖顺序为子类覆盖 `Trait` 覆盖 父类,即 下述 `Origin` 的 `foo` 变量为本身注入的 `Foo1`。
|
||||
`#[Inject]` 覆盖顺序为子类覆盖 `Trait` 覆盖 父类,即 下述 `Origin` 的 `foo` 变量为本身注入的 `Foo1`。
|
||||
|
||||
同理,假如 `Origin` 不存在变量 `$foo` 时,`$foo` 会被第一个 `Trait` 完成注入,注入类 `Foo2`。
|
||||
|
||||
@ -446,25 +434,27 @@ use Hyperf\Di\Annotation\Inject;
|
||||
class ParentClass
|
||||
{
|
||||
/**
|
||||
* @Inject
|
||||
* @var Foo4
|
||||
*/
|
||||
#[Inject]
|
||||
protected $foo;
|
||||
}
|
||||
|
||||
trait Foo1{
|
||||
trait Foo1
|
||||
{
|
||||
/**
|
||||
* @Inject
|
||||
* @var Foo2
|
||||
*/
|
||||
#[Inject]
|
||||
protected $foo;
|
||||
}
|
||||
|
||||
trait Foo2{
|
||||
trait Foo2
|
||||
{
|
||||
/**
|
||||
* @Inject
|
||||
* @var Foo3
|
||||
*/
|
||||
#[Inject]
|
||||
protected $foo;
|
||||
}
|
||||
|
||||
@ -472,10 +462,11 @@ class Origin extends ParentClass
|
||||
{
|
||||
use Foo1;
|
||||
use Foo2;
|
||||
|
||||
/**
|
||||
* @Inject
|
||||
* @var Foo1
|
||||
*/
|
||||
#[Inject]
|
||||
protected $foo;
|
||||
}
|
||||
```
|
||||
|
@ -100,9 +100,7 @@ use App\Event\UserRegistered;
|
||||
use Hyperf\Event\Annotation\Listener;
|
||||
use Hyperf\Event\Contract\ListenerInterface;
|
||||
|
||||
/**
|
||||
* @Listener
|
||||
*/
|
||||
#[Listener]
|
||||
class UserRegisteredListener implements ListenerInterface
|
||||
{
|
||||
public function listen(): array
|
||||
@ -144,9 +142,9 @@ use App\Event\UserRegistered;
|
||||
class UserService
|
||||
{
|
||||
/**
|
||||
* @Inject
|
||||
* @var EventDispatcherInterface
|
||||
*/
|
||||
#[Inject]
|
||||
private $eventDispatcher;
|
||||
|
||||
public function register()
|
||||
|
@ -24,20 +24,16 @@ use Hyperf\HttpServer\Annotation\Controller;
|
||||
use Hyperf\HttpServer\Annotation\PostMapping;
|
||||
use Hyperf\HttpServer\Contract\RequestInterface;
|
||||
|
||||
/**
|
||||
* @Controller()
|
||||
*/
|
||||
#[Controller]
|
||||
class GraphQLController
|
||||
{
|
||||
/**
|
||||
* @Inject()
|
||||
* @var Schema
|
||||
*/
|
||||
#[Inject]
|
||||
protected $schema;
|
||||
|
||||
/**
|
||||
* @PostMapping(path="/graphql")
|
||||
*/
|
||||
|
||||
#[PostMapping(path: "/graphql")]
|
||||
public function test(RequestInterface $request)
|
||||
{
|
||||
$rawInput = $request->getBody()->getContents();
|
||||
@ -46,10 +42,8 @@ class GraphQLController
|
||||
$variableValues = isset($input['variables']) ? $input['variables'] : null;
|
||||
return GraphQL::executeQuery($this->schema, $query, null, null, $variableValues)->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* @Query()
|
||||
*/
|
||||
|
||||
#[Query]
|
||||
public function hello(string $name): string
|
||||
{
|
||||
return $name;
|
||||
@ -80,9 +74,7 @@ namespace App\Model;
|
||||
use Hyperf\GraphQL\Annotation\Type;
|
||||
use Hyperf\GraphQL\Annotation\Field;
|
||||
|
||||
/**
|
||||
* @Type()
|
||||
*/
|
||||
#[Type]
|
||||
class Product
|
||||
{
|
||||
protected $name;
|
||||
@ -94,17 +86,13 @@ class Product
|
||||
$this->price = $price;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Field()
|
||||
*/
|
||||
#[Field]
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @Field()
|
||||
*/
|
||||
#[Field]
|
||||
public function getPrice(): ?float
|
||||
{
|
||||
return $this->price;
|
||||
@ -117,10 +105,9 @@ class Product
|
||||
```php
|
||||
<?php
|
||||
use App\Model\Product;
|
||||
use Hyperf\GraphQL\Annotation\Query;
|
||||
|
||||
/**
|
||||
* @Query()
|
||||
*/
|
||||
#[Query]
|
||||
public function product(string $name, float $price): Product
|
||||
{
|
||||
return new Product($name, $price);
|
||||
|
@ -151,8 +151,8 @@ public function hello()
|
||||
$request->setSex(1);
|
||||
|
||||
/**
|
||||
* @var \Grpc\HiReply $reply
|
||||
*/
|
||||
* @var \Grpc\HiReply $reply
|
||||
*/
|
||||
list($reply, $status) = $client->sayHello($request);
|
||||
|
||||
$message = $reply->getMessage();
|
||||
|
@ -16,12 +16,10 @@ composer require hyperf/guzzle
|
||||
<?php
|
||||
use Hyperf\Guzzle\ClientFactory;
|
||||
|
||||
class Foo {
|
||||
/**
|
||||
* @var \Hyperf\Guzzle\ClientFactory
|
||||
*/
|
||||
private $clientFactory;
|
||||
|
||||
class Foo
|
||||
{
|
||||
private ClientFactory $clientFactory;
|
||||
|
||||
public function __construct(ClientFactory $clientFactory)
|
||||
{
|
||||
$this->clientFactory = $clientFactory;
|
||||
|
@ -40,8 +40,8 @@ use Hyperf\RpcServer\Annotation\RpcService;
|
||||
|
||||
/**
|
||||
* 注意,如希望通过服务中心来管理服务,需在注解内增加 publishTo 属性
|
||||
* @RpcService(name="CalculatorService", protocol="jsonrpc-http", server="jsonrpc-http")
|
||||
*/
|
||||
#[RpcService(name: "CalculatorService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
|
||||
class CalculatorService implements CalculatorServiceInterface
|
||||
{
|
||||
// 实现一个加法方法,这里简单的认为参数都是 int 类型
|
||||
@ -281,15 +281,13 @@ class CalculatorServiceConsumer extends AbstractServiceClient implements Calcula
|
||||
{
|
||||
/**
|
||||
* 定义对应服务提供者的服务名称
|
||||
* @var string
|
||||
*/
|
||||
protected $serviceName = 'CalculatorService';
|
||||
protected string $serviceName = 'CalculatorService';
|
||||
|
||||
/**
|
||||
* 定义对应服务提供者的服务协议
|
||||
* @var string
|
||||
*/
|
||||
protected $protocol = 'jsonrpc-http';
|
||||
protected string $protocol = 'jsonrpc-http';
|
||||
|
||||
public function add(int $a, int $b): int
|
||||
{
|
||||
|
@ -138,9 +138,7 @@ use Hyperf\Kafka\AbstractConsumer;
|
||||
use Hyperf\Kafka\Annotation\Consumer;
|
||||
use longlang\phpkafka\Consumer\ConsumeMessage;
|
||||
|
||||
/**
|
||||
* @Consumer(topic="hyperf", nums=5, groupId="hyperf", autoCommit=true)
|
||||
*/
|
||||
#[Consumer(topic: "hyperf", nums: 5, groupId: "hyperf", autoCommit: true)]
|
||||
class KafkaConsumer extends AbstractConsumer
|
||||
{
|
||||
public function consume(ConsumeMessage $message): string
|
||||
@ -165,9 +163,7 @@ namespace App\Controller;
|
||||
use Hyperf\HttpServer\Annotation\AutoController;
|
||||
use Hyperf\Kafka\Producer;
|
||||
|
||||
/**
|
||||
* @AutoController()
|
||||
*/
|
||||
#[AutoController]
|
||||
class IndexController extends AbstractController
|
||||
{
|
||||
public function index(Producer $producer)
|
||||
@ -194,9 +190,7 @@ use Hyperf\HttpServer\Annotation\AutoController;
|
||||
use Hyperf\Kafka\Producer;
|
||||
use longlang\phpkafka\Producer\ProduceMessage;
|
||||
|
||||
/**
|
||||
* @AutoController()
|
||||
*/
|
||||
#[AutoController]
|
||||
class IndexController extends AbstractController
|
||||
{
|
||||
public function index(Producer $producer)
|
||||
@ -206,7 +200,6 @@ class IndexController extends AbstractController
|
||||
new ProduceMessage('hyperf2', 'hyperf2_value', 'hyperf2_key'),
|
||||
new ProduceMessage('hyperf3', 'hyperf3_value', 'hyperf3_key'),
|
||||
]);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -50,11 +50,8 @@ use Hyperf\Logger\LoggerFactory;
|
||||
|
||||
class DemoService
|
||||
{
|
||||
|
||||
/**
|
||||
* @var \Psr\Log\LoggerInterface
|
||||
*/
|
||||
protected $logger;
|
||||
|
||||
protected LoggerInterface $logger;
|
||||
|
||||
public function __construct(LoggerFactory $loggerFactory)
|
||||
{
|
||||
|
@ -232,14 +232,15 @@ declare(strict_types=1);
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Model\Order;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Hyperf\Metric\Contract\MetricFactoryInterface;
|
||||
|
||||
class IndexController extends AbstractController
|
||||
{
|
||||
/**
|
||||
* @Inject
|
||||
* @var MetricFactoryInterface
|
||||
*/
|
||||
#[Inject]
|
||||
private $metricFactory;
|
||||
|
||||
public function create(Order $order)
|
||||
@ -278,10 +279,7 @@ use Redis;
|
||||
|
||||
class OnMetricFactoryReady implements ListenerInterface
|
||||
{
|
||||
/**
|
||||
* @var ContainerInterface
|
||||
*/
|
||||
protected $container;
|
||||
protected ContainerInterface $container;
|
||||
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
|
@ -79,10 +79,8 @@ use App\Middleware\FooMiddleware;
|
||||
use Hyperf\HttpServer\Annotation\AutoController;
|
||||
use Hyperf\HttpServer\Annotation\Middleware;
|
||||
|
||||
/**
|
||||
* @AutoController()
|
||||
* @Middleware(FooMiddleware::class)
|
||||
*/
|
||||
#[AutoController]
|
||||
#[Middleware(FooMiddleware::class)]
|
||||
class IndexController
|
||||
{
|
||||
public function index()
|
||||
@ -104,13 +102,8 @@ use Hyperf\HttpServer\Annotation\AutoController;
|
||||
use Hyperf\HttpServer\Annotation\Middleware;
|
||||
use Hyperf\HttpServer\Annotation\Middlewares;
|
||||
|
||||
/**
|
||||
* @AutoController()
|
||||
* @Middlewares({
|
||||
* @Middleware(FooMiddleware::class),
|
||||
* @Middleware(BarMiddleware::class)
|
||||
* })
|
||||
*/
|
||||
#[AutoController]
|
||||
#[Middlewares(FooMiddleware::class, BarMiddleware::class)]
|
||||
class IndexController
|
||||
{
|
||||
public function index()
|
||||
@ -119,7 +112,6 @@ class IndexController
|
||||
}
|
||||
}
|
||||
```
|
||||
> 注意: 使用 `PHP8` 的 `Attributes` 语法时, 应简化为`#[Middlewares(FooMiddleware::class, BarMiddleware::class)]`这种格式, 下同。
|
||||
|
||||
#### 定义方法级别的中间件
|
||||
|
||||
@ -136,26 +128,18 @@ use Hyperf\HttpServer\Annotation\AutoController;
|
||||
use Hyperf\HttpServer\Annotation\Middleware;
|
||||
use Hyperf\HttpServer\Annotation\Middlewares;
|
||||
|
||||
/**
|
||||
* @AutoController()
|
||||
* @Middlewares({
|
||||
* @Middleware(FooMiddleware::class)
|
||||
* })
|
||||
*/
|
||||
#[AutoController]
|
||||
#[Middlewares([FooMiddleware::class])]
|
||||
class IndexController
|
||||
{
|
||||
|
||||
/**
|
||||
* @Middlewares({
|
||||
* @Middleware(BarMiddleware::class)
|
||||
* })
|
||||
*/
|
||||
#[Middleware(BarMiddleware::class)]
|
||||
public function index()
|
||||
{
|
||||
return 'Hello Hyperf.';
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
#### 中间件相关的代码
|
||||
|
||||
生成中间件
|
||||
@ -181,20 +165,11 @@ use Psr\Http\Server\RequestHandlerInterface;
|
||||
|
||||
class FooMiddleware implements MiddlewareInterface
|
||||
{
|
||||
/**
|
||||
* @var ContainerInterface
|
||||
*/
|
||||
protected $container;
|
||||
protected ContainerInterface $container;
|
||||
|
||||
/**
|
||||
* @var RequestInterface
|
||||
*/
|
||||
protected $request;
|
||||
protected RequestInterface $request;
|
||||
|
||||
/**
|
||||
* @var HttpResponse
|
||||
*/
|
||||
protected $response;
|
||||
protected HttpResponse $response;
|
||||
|
||||
public function __construct(ContainerInterface $container, HttpResponse $response, RequestInterface $request)
|
||||
{
|
||||
@ -243,7 +218,7 @@ $response = \Hyperf\Utils\Context::set(ResponseInterface::class, $response);
|
||||
|
||||
## 自定义 CoreMiddleWare 的行为
|
||||
|
||||
默认情况下,Hyperf 在处理路由找不到或 HTTP 方法不允许时,即 HTTP 状态码为 `404` `405` 的时候,是由 `CoreMiddleware` 直接处理并返回对应的响应对象的,得益于 Hyperf 依赖注入的设计,您可以通过替换对象的方式来把 `CoreMiddleware` 指向由您自己实现的 `CoreMiddleware` 去。
|
||||
默认情况下,Hyperf 在处理路由找不到或 HTTP 方法不允许时,即 HTTP 状态码为 `404`、`405` 的时候,是由 `CoreMiddleware` 直接处理并返回对应的响应对象的,得益于 Hyperf 依赖注入的设计,您可以通过替换对象的方式来把 `CoreMiddleware` 指向由您自己实现的 `CoreMiddleware` 去。
|
||||
|
||||
比如我们希望定义一个 `App\Middleware\CoreMiddleware` 类来重写默认的行为,我们可以先定义一个 `App\Middleware\CoreMiddleware` 类如下,这里我们仅以 HTTP Server 为例,其它 Server 也可采用同样的做法来达到同样的目的。
|
||||
|
||||
|
@ -29,9 +29,6 @@ use Hyperf\Nats\AbstractConsumer;
|
||||
use Hyperf\Nats\Annotation\Consumer;
|
||||
use Hyperf\Nats\Message;
|
||||
|
||||
/**
|
||||
* @Consumer(subject="hyperf.demo", queue="hyperf.demo", name="DemoConsumer", nums=1)
|
||||
*/
|
||||
#[Consumer(subject: 'hyperf.demo', queue: 'hyperf.demo', name: 'DemoConsumer', nums: 1)]
|
||||
class DemoConsumer extends AbstractConsumer
|
||||
{
|
||||
@ -57,16 +54,11 @@ use Hyperf\Di\Annotation\Inject;
|
||||
use Hyperf\HttpServer\Annotation\AutoController;
|
||||
use Hyperf\Nats\Driver\DriverInterface;
|
||||
|
||||
/**
|
||||
* @AutoController(prefix="nats")
|
||||
*/
|
||||
#[AutoController(prefix: "nats")]
|
||||
class NatsController extends AbstractController
|
||||
{
|
||||
/**
|
||||
* @Inject
|
||||
* @var DriverInterface
|
||||
*/
|
||||
protected $nats;
|
||||
#[Inject]
|
||||
protected DriverInterface $nats;
|
||||
|
||||
public function publish()
|
||||
{
|
||||
@ -94,16 +86,11 @@ use Hyperf\HttpServer\Annotation\AutoController;
|
||||
use Hyperf\Nats\Driver\DriverInterface;
|
||||
use Hyperf\Nats\Message;
|
||||
|
||||
/**
|
||||
* @AutoController(prefix="nats")
|
||||
*/
|
||||
#[AutoController(prefix: "nats")]
|
||||
class NatsController extends AbstractController
|
||||
{
|
||||
/**
|
||||
* @Inject
|
||||
* @var DriverInterface
|
||||
*/
|
||||
protected $nats;
|
||||
#[Inject]
|
||||
protected DriverInterface $nats;
|
||||
|
||||
public function request()
|
||||
{
|
||||
@ -133,16 +120,11 @@ use Hyperf\HttpServer\Annotation\AutoController;
|
||||
use Hyperf\Nats\Driver\DriverInterface;
|
||||
use Hyperf\Nats\Message;
|
||||
|
||||
/**
|
||||
* @AutoController(prefix="nats")
|
||||
*/
|
||||
#[AutoController(prefix: "nats")]
|
||||
class NatsController extends AbstractController
|
||||
{
|
||||
/**
|
||||
* @Inject
|
||||
* @var DriverInterface
|
||||
*/
|
||||
protected $nats;
|
||||
#[Inject]
|
||||
protected DriverInterface $nats;
|
||||
|
||||
public function sync()
|
||||
{
|
||||
|
@ -67,14 +67,7 @@ use Hyperf\Nsq\Annotation\Consumer;
|
||||
use Hyperf\Nsq\Message;
|
||||
use Hyperf\Nsq\Result;
|
||||
|
||||
/**
|
||||
* @Consumer(
|
||||
* topic="hyperf",
|
||||
* channel="hyperf",
|
||||
* name ="DemoNsqConsumer",
|
||||
* nums=1
|
||||
* )
|
||||
*/
|
||||
#[Consumer(topic: "hyperf", channel: "hyperf", name: "DemoNsqConsumer", nums: 1)]
|
||||
class DemoNsqConsumer extends AbstractConsumer
|
||||
{
|
||||
public function consume(Message $payload): string
|
||||
@ -113,14 +106,7 @@ use Hyperf\Nsq\Message;
|
||||
use Hyperf\Nsq\Result;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
/**
|
||||
* @Consumer(
|
||||
* topic="demo_topic",
|
||||
* channel="demo_channel",
|
||||
* name ="DemoConsumer",
|
||||
* nums=1
|
||||
* )
|
||||
*/
|
||||
#[Consumer(topic: "demo_topic", channel: "demo_channel", name: "DemoConsumer", nums: 1)]
|
||||
class DemoConsumer extends AbstractConsumer
|
||||
{
|
||||
public function __construct(ContainerInterface $container)
|
||||
@ -157,9 +143,7 @@ use Hyperf\Command\Command as HyperfCommand;
|
||||
use Hyperf\Command\Annotation\Command;
|
||||
use Hyperf\Nsq\Nsq;
|
||||
|
||||
/**
|
||||
* @Command
|
||||
*/
|
||||
#[Command]
|
||||
class NsqCommand extends HyperfCommand
|
||||
{
|
||||
protected $name = 'nsq:pub';
|
||||
@ -192,9 +176,7 @@ use Hyperf\Command\Command as HyperfCommand;
|
||||
use Hyperf\Command\Annotation\Command;
|
||||
use Hyperf\Nsq\Nsq;
|
||||
|
||||
/**
|
||||
* @Command
|
||||
*/
|
||||
#[Command]
|
||||
class NsqCommand extends HyperfCommand
|
||||
{
|
||||
protected $name = 'nsq:pub';
|
||||
@ -231,9 +213,7 @@ use Hyperf\Command\Command as HyperfCommand;
|
||||
use Hyperf\Command\Annotation\Command;
|
||||
use Hyperf\Nsq\Nsq;
|
||||
|
||||
/**
|
||||
* @Command
|
||||
*/
|
||||
#[Command]
|
||||
class NsqCommand extends HyperfCommand
|
||||
{
|
||||
protected $name = 'nsq:pub';
|
||||
|
@ -23,9 +23,7 @@ use Hyperf\HttpServer\Contract\RequestInterface;
|
||||
use Hyperf\Paginator\Paginator;
|
||||
use Hyperf\Utils\Collection;
|
||||
|
||||
/**
|
||||
* @AutoController()
|
||||
*/
|
||||
#[AutoController]
|
||||
class UserController
|
||||
{
|
||||
public function index(RequestInterface $request)
|
||||
|
@ -79,21 +79,18 @@ class Frequency extends \Hyperf\Pool\Frequency
|
||||
{
|
||||
/**
|
||||
* 被计算频率的时间间隔
|
||||
* @var int
|
||||
*/
|
||||
protected $time = 10;
|
||||
protected int $time = 10;
|
||||
|
||||
/**
|
||||
* 触发低频的频率
|
||||
* @var int
|
||||
*/
|
||||
protected $lowFrequency = 5;
|
||||
protected int $lowFrequency = 5;
|
||||
|
||||
/**
|
||||
* 连续触发低频的最小时间间隔
|
||||
* @var int
|
||||
*/
|
||||
protected $lowFrequencyInterval = 60;
|
||||
protected int $lowFrequencyInterval = 60;
|
||||
}
|
||||
|
||||
```
|
||||
|
@ -49,9 +49,7 @@ namespace App\Process;
|
||||
use Hyperf\Process\AbstractProcess;
|
||||
use Hyperf\Process\Annotation\Process;
|
||||
|
||||
/**
|
||||
* @Process(name="foo_process")
|
||||
*/
|
||||
#[Process(name: "foo_process")]
|
||||
class FooProcess extends AbstractProcess
|
||||
{
|
||||
public function handle(): void
|
||||
@ -76,9 +74,7 @@ namespace App\Process;
|
||||
use Hyperf\Process\AbstractProcess;
|
||||
use Hyperf\Process\Annotation\Process;
|
||||
|
||||
/**
|
||||
* @Process(name="foo_process")
|
||||
*/
|
||||
#[Process(name: "foo_process")]
|
||||
class FooProcess extends AbstractProcess
|
||||
{
|
||||
public function handle(): void
|
||||
@ -107,40 +103,33 @@ namespace App\Process;
|
||||
use Hyperf\Process\AbstractProcess;
|
||||
use Hyperf\Process\Annotation\Process;
|
||||
|
||||
/**
|
||||
* @Process(name="user-process",redirectStdinStdout=false, pipeType=2, enableCoroutine=true)
|
||||
*/
|
||||
#[Process(name: "user-process", redirectStdinStdout: false, pipeType: 2, enableCoroutine: true)]
|
||||
class FooProcess extends AbstractProcess
|
||||
{
|
||||
/**
|
||||
* 进程数量
|
||||
* @var int
|
||||
*/
|
||||
public $nums = 1;
|
||||
public int $nums = 1;
|
||||
|
||||
/**
|
||||
* 进程名称
|
||||
* @var string
|
||||
*/
|
||||
public $name = 'user-process';
|
||||
public string $name = 'user-process';
|
||||
|
||||
/**
|
||||
* 重定向自定义进程的标准输入和输出
|
||||
* @var bool
|
||||
*/
|
||||
public $redirectStdinStdout = false;
|
||||
public bool $redirectStdinStdout = false;
|
||||
|
||||
/**
|
||||
* 管道类型
|
||||
* @var int
|
||||
*/
|
||||
public $pipeType = 2;
|
||||
public int $pipeType = 2;
|
||||
|
||||
/**
|
||||
* 是否启用协程
|
||||
* @var bool
|
||||
*/
|
||||
public $enableCoroutine = true;
|
||||
public bool $enableCoroutine = true;
|
||||
}
|
||||
```
|
||||
|
||||
@ -158,9 +147,7 @@ use Hyperf\Process\AbstractProcess;
|
||||
use Hyperf\Process\Annotation\Process;
|
||||
use Hyperf\Contract\StdoutLoggerInterface;
|
||||
|
||||
/**
|
||||
* @Process(name="demo_process")
|
||||
*/
|
||||
#[Process(name: "demo_process")]
|
||||
class DemoProcess extends AbstractProcess
|
||||
{
|
||||
public function handle(): void
|
||||
|
@ -60,9 +60,7 @@ namespace App\Controller;
|
||||
use Hyperf\HttpServer\Contract\RequestInterface;
|
||||
use Hyperf\HttpServer\Annotation\AutoController;
|
||||
|
||||
/**
|
||||
* @AutoController()
|
||||
*/
|
||||
#[AutoController]
|
||||
class IndexController
|
||||
{
|
||||
// Hyperf 会自动为此方法生成一个 /index/index 的路由,允许通过 GET 或 POST 方式请求
|
||||
@ -97,15 +95,11 @@ use Hyperf\HttpServer\Contract\RequestInterface;
|
||||
use Hyperf\HttpServer\Annotation\Controller;
|
||||
use Hyperf\HttpServer\Annotation\RequestMapping;
|
||||
|
||||
/**
|
||||
* @Controller()
|
||||
*/
|
||||
#[Controller]
|
||||
class IndexController
|
||||
{
|
||||
// Hyperf 会自动为此方法生成一个 /index/index 的路由,允许通过 GET 或 POST 方式请求
|
||||
/**
|
||||
* @RequestMapping(path="index", methods="get,post")
|
||||
*/
|
||||
#[RequestMapping(path: "index", methods: "get,post")]
|
||||
public function index(RequestInterface $request)
|
||||
{
|
||||
// 从请求中获得 id 参数
|
||||
@ -130,9 +124,7 @@ namespace App\Controller;
|
||||
use Hyperf\HttpServer\Contract\RequestInterface;
|
||||
use Hyperf\HttpServer\Annotation\AutoController;
|
||||
|
||||
/**
|
||||
* @AutoController()
|
||||
*/
|
||||
#[AutoController]
|
||||
class IndexController
|
||||
{
|
||||
// Hyperf 会自动为此方法生成一个 /index/index 的路由,允许通过 GET 或 POST 方式请求
|
||||
@ -149,7 +141,7 @@ class IndexController
|
||||
## 依赖自动注入
|
||||
|
||||
依赖自动注入是 `Hyperf` 提供的一个非常强大的功能,也是保持框架灵活性的根基。
|
||||
`Hyperf` 提供了两种注入方式,一种是大家常见的通过构造函数注入,另一种是通过 `@Inject` 注解注入,下面我们举个例子并分别以两种方式展示注入的实现;
|
||||
`Hyperf` 提供了两种注入方式,一种是大家常见的通过构造函数注入,另一种是通过 `#[Inject]` 注解注入,下面我们举个例子并分别以两种方式展示注入的实现;
|
||||
假设我们存在一个 `\App\Service\UserService` 类,类中存在一个 `getInfoById(int $id)` 方法通过传递一个 `id` 并最终返回一个用户实体,由于返回值并不是我们这里所需要关注的,所以不做过多阐述,我们要关注的是在任意的类中获取 `UserService` 并调用里面的方法,一般的方法是通过 `new UserService()` 来实例化该服务类,但在 `Hyperf` 下,我们有更优的解决方法。
|
||||
|
||||
### 通过构造函数注入
|
||||
@ -164,15 +156,10 @@ use Hyperf\HttpServer\Contract\RequestInterface;
|
||||
use Hyperf\HttpServer\Annotation\AutoController;
|
||||
use App\Service\UserService;
|
||||
|
||||
/**
|
||||
* @AutoController()
|
||||
*/
|
||||
#[AutoController]
|
||||
class IndexController
|
||||
{
|
||||
/**
|
||||
* @var UserService
|
||||
*/
|
||||
private $userService;
|
||||
private UserService $userService;
|
||||
|
||||
// 在构造函数声明参数的类型,Hyperf 会自动注入对应的对象或值
|
||||
public function __construct(UserService $userService)
|
||||
@ -190,9 +177,9 @@ class IndexController
|
||||
```
|
||||
|
||||
### 通过 `@Inject` 注解注入
|
||||
只需对对应的类属性通过 `@var` 声明参数的类型,并使用 `@Inject` 注解标记属性 ,`Hyperf` 会自动注入对应的对象或值。
|
||||
只需对对应的类属性通过 `@var` 声明参数的类型,并使用 `#[Inject]` 注解标记属性 ,`Hyperf` 会自动注入对应的对象或值。
|
||||
|
||||
> 使用 `@Inject` 注解时需 `use Hyperf\Di\Annotation\Inject;` 命名空间;
|
||||
> 使用 `#[Inject]` 注解时需 `use Hyperf\Di\Annotation\Inject;` 命名空间;
|
||||
|
||||
```php
|
||||
<?php
|
||||
@ -205,15 +192,13 @@ use Hyperf\HttpServer\Annotation\AutoController;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use App\Service\UserService;
|
||||
|
||||
/**
|
||||
* @AutoController()
|
||||
*/
|
||||
#[AutoController]
|
||||
class IndexController
|
||||
{
|
||||
/**
|
||||
* @Inject()
|
||||
* @var UserService
|
||||
*/
|
||||
#[Inject]
|
||||
private $userService;
|
||||
|
||||
// /index/info
|
||||
@ -311,9 +296,7 @@ namespace App\Controller;
|
||||
|
||||
use Hyperf\HttpServer\Annotation\AutoController;
|
||||
|
||||
/**
|
||||
* @AutoController(server="innerHttp")
|
||||
*/
|
||||
#[AutoController(server: "innerHttp")]
|
||||
class IndexController
|
||||
{
|
||||
public function index()
|
||||
|
@ -118,9 +118,9 @@ use Hyperf\Di\Annotation\Inject;
|
||||
trait TestTrait
|
||||
{
|
||||
/**
|
||||
* @Inject()
|
||||
* @var ResponseInterface
|
||||
*/
|
||||
#[Inject]
|
||||
protected $response;
|
||||
}
|
||||
```
|
||||
|
@ -37,24 +37,18 @@ use Hyperf\HttpServer\Annotation\Controller;
|
||||
use Hyperf\HttpServer\Annotation\RequestMapping;
|
||||
use Hyperf\RateLimit\Annotation\RateLimit;
|
||||
|
||||
/**
|
||||
* @Controller(prefix="rate-limit")
|
||||
*/
|
||||
#[Controller(prefix: "rate-limit")]
|
||||
class RateLimitController
|
||||
{
|
||||
/**
|
||||
* @RequestMapping(path="test")
|
||||
* @RateLimit(create=1, capacity=3)
|
||||
*/
|
||||
#[RequestMapping(path: "test")]
|
||||
#[RateLimit(create: 1, capacity: 3)]
|
||||
public function test()
|
||||
{
|
||||
return ["QPS 1, 峰值3"];
|
||||
}
|
||||
|
||||
/**
|
||||
* @RequestMapping(path="test2")
|
||||
* @RateLimit(create=2, consume=2, capacity=4)
|
||||
*/
|
||||
#[RequestMapping(path: "test2")]
|
||||
#[RateLimit(create: 2, consume: 2, capacity: 4)]
|
||||
public function test2()
|
||||
{
|
||||
return ["QPS 2, 峰值2"];
|
||||
@ -79,16 +73,12 @@ use Hyperf\HttpServer\Annotation\Controller;
|
||||
use Hyperf\HttpServer\Annotation\RequestMapping;
|
||||
use Hyperf\RateLimit\Annotation\RateLimit;
|
||||
|
||||
/**
|
||||
* @Controller(prefix="rate-limit")
|
||||
* @RateLimit(limitCallback={RateLimitController::class, "limitCallback"})
|
||||
*/
|
||||
#[Controller(prefix: "rate-limit")]
|
||||
#[RateLimit(limitCallback: [RateLimitController::class, "limitCallback"])]
|
||||
class RateLimitController
|
||||
{
|
||||
/**
|
||||
* @RequestMapping(path="test")
|
||||
* @RateLimit(create=1, capacity=3)
|
||||
*/
|
||||
#[RequestMapping(path: "test")]
|
||||
#[RateLimit(create: 1, capacity: 3)]
|
||||
public function test()
|
||||
{
|
||||
return ["QPS 1, 峰值3"];
|
||||
|
@ -282,10 +282,7 @@ use Swoole\WebSocket\Server as WebSocketServer;
|
||||
|
||||
class WebSocketController implements OnMessageInterface, OnOpenInterface, OnCloseInterface
|
||||
{
|
||||
/**
|
||||
* @var IpcSubject
|
||||
*/
|
||||
private $subject;
|
||||
private IpcSubject $subject;
|
||||
|
||||
private $subscriber = [];
|
||||
|
||||
|
@ -26,9 +26,7 @@ namespace App\Controller;
|
||||
use Hyperf\HttpServer\Contract\RequestInterface;
|
||||
use Hyperf\HttpServer\Annotation\AutoController;
|
||||
|
||||
/**
|
||||
* @AutoController()
|
||||
*/
|
||||
#[AutoController]
|
||||
class IndexController
|
||||
{
|
||||
public function info(RequestInterface $request)
|
||||
@ -44,10 +42,7 @@ class IndexController
|
||||
|
||||
```php
|
||||
// 注解方式
|
||||
/**
|
||||
* @GetMapping(path="/user/{id:\d+}")
|
||||
*/
|
||||
|
||||
#[GetMapping(path: "/user/{id:\d+}")]
|
||||
// 配置方式
|
||||
use Hyperf\HttpServer\Router\Router;
|
||||
|
||||
@ -64,9 +59,7 @@ namespace App\Controller;
|
||||
use Hyperf\HttpServer\Contract\RequestInterface;
|
||||
use Hyperf\HttpServer\Annotation\AutoController;
|
||||
|
||||
/**
|
||||
* @AutoController()
|
||||
*/
|
||||
#[AutoController]
|
||||
class IndexController
|
||||
{
|
||||
public function info(RequestInterface $request, int $id)
|
||||
@ -86,9 +79,7 @@ namespace App\Controller;
|
||||
use Hyperf\HttpServer\Contract\RequestInterface;
|
||||
use Hyperf\HttpServer\Annotation\AutoController;
|
||||
|
||||
/**
|
||||
* @AutoController()
|
||||
*/
|
||||
#[AutoController]
|
||||
class IndexController
|
||||
{
|
||||
public function info(RequestInterface $request)
|
||||
|
@ -13,13 +13,13 @@ composer require hyperf/retry
|
||||
|
||||
## Hello World
|
||||
|
||||
在需要重试的方法上加入注解 `@Retry`。
|
||||
在需要重试的方法上加入注解 `#[Retry]`。
|
||||
|
||||
```php
|
||||
/**
|
||||
* 异常时重试该方法
|
||||
* @Retry
|
||||
*/
|
||||
#[Retry]
|
||||
public function foo()
|
||||
{
|
||||
// 发起一次远程调用
|
||||
@ -47,10 +47,7 @@ namespace App\Annotation;
|
||||
|
||||
use Doctrine\Common\Annotations\Annotation\Target;
|
||||
|
||||
/**
|
||||
* @Annotation
|
||||
* @Target({"METHOD"})
|
||||
*/
|
||||
#[Attribute(Attribute::TARGET_METHOD)]
|
||||
class MyRetry extends \Hyperf\Retry\Annotation\AbstractRetry
|
||||
{
|
||||
}
|
||||
@ -67,10 +64,7 @@ namespace App\Annotation;
|
||||
|
||||
use Doctrine\Common\Annotations\Annotation\Target;
|
||||
|
||||
/**
|
||||
* @Annotation
|
||||
* @Target({"METHOD"})
|
||||
*/
|
||||
#[Attribute(Attribute::TARGET_METHOD)]
|
||||
class MyRetry extends \Hyperf\Retry\Annotation\AbstractRetry
|
||||
{
|
||||
public $policies = [
|
||||
@ -91,10 +85,7 @@ namespace App\Annotation;
|
||||
|
||||
use Doctrine\Common\Annotations\Annotation\Target;
|
||||
|
||||
/**
|
||||
* @Annotation
|
||||
* @Target({"METHOD"})
|
||||
*/
|
||||
#[Attribute(Attribute::TARGET_METHOD)]
|
||||
class MyRetry extends \Hyperf\Retry\Annotation\AbstractRetry
|
||||
{
|
||||
public $policies = [
|
||||
@ -116,10 +107,7 @@ namespace App\Annotation;
|
||||
|
||||
use Doctrine\Common\Annotations\Annotation\Target;
|
||||
|
||||
/**
|
||||
* @Annotation
|
||||
* @Target({"METHOD"})
|
||||
*/
|
||||
#[Attribute(Attribute::TARGET_METHOD)]
|
||||
class MyRetry extends \Hyperf\Retry\Annotation\Retry
|
||||
{
|
||||
public $policies = [
|
||||
@ -155,15 +143,13 @@ public $policies = [
|
||||
|
||||
/**
|
||||
* The algorithm for retry intervals.
|
||||
* @var string
|
||||
*/
|
||||
public $sleepStrategyClass = SleepStrategyInterface::class;
|
||||
public string $sleepStrategyClass = SleepStrategyInterface::class;
|
||||
|
||||
/**
|
||||
* Max Attampts.
|
||||
* @var int
|
||||
*/
|
||||
public $maxAttempts = 10;
|
||||
public int $maxAttempts = 10;
|
||||
|
||||
/**
|
||||
* Retry Budget.
|
||||
@ -182,9 +168,8 @@ public $retryBudget = [
|
||||
/**
|
||||
* Base time inteval (ms) for each try. For backoff strategy this is the interval for the first try
|
||||
* while for flat strategy this is the interval for every try.
|
||||
* @var int
|
||||
*/
|
||||
public $base = 0;
|
||||
public int $base = 0;
|
||||
|
||||
/**
|
||||
* Configures a Predicate which evaluates if an exception should be retried.
|
||||
|
@ -107,9 +107,7 @@ namespace App\Controller;
|
||||
use Hyperf\HttpServer\Contract\RequestInterface;
|
||||
use Hyperf\HttpServer\Annotation\AutoController;
|
||||
|
||||
/**
|
||||
* @AutoController()
|
||||
*/
|
||||
#[AutoController]
|
||||
class UserController
|
||||
{
|
||||
// Hyperf 会自动为此方法生成一个 /user/index 的路由,允许通过 GET 或 POST 方式请求
|
||||
@ -145,15 +143,11 @@ use Hyperf\HttpServer\Contract\RequestInterface;
|
||||
use Hyperf\HttpServer\Annotation\Controller;
|
||||
use Hyperf\HttpServer\Annotation\RequestMapping;
|
||||
|
||||
/**
|
||||
* @Controller()
|
||||
*/
|
||||
#[Controller]
|
||||
class UserController
|
||||
{
|
||||
// Hyperf 会自动为此方法生成一个 /user/index 的路由,允许通过 GET 或 POST 方式请求
|
||||
/**
|
||||
* @RequestMapping(path="index", methods="get,post")
|
||||
*/
|
||||
#[RequestMapping(path: "index", methods: "get,post")]
|
||||
public function index(RequestInterface $request)
|
||||
{
|
||||
// 从请求中获得 id 参数
|
||||
|
@ -57,9 +57,7 @@ use App\JsonRpc\CalculatorServiceInterface;
|
||||
use Hyperf\RpcMultiplex\Constant;
|
||||
use Hyperf\RpcServer\Annotation\RpcService;
|
||||
|
||||
/**
|
||||
* @RpcService(name="CalculatorService", server="rpc", protocol=Constant::PROTOCOL_DEFAULT)
|
||||
*/
|
||||
#[RpcService(name: "CalculatorService", server: "rpc", protocol: Constant::PROTOCOL_DEFAULT)]
|
||||
class CalculatorService implements CalculatorServiceInterface
|
||||
{
|
||||
}
|
||||
|
@ -83,9 +83,7 @@ namespace App\JsonRpc;
|
||||
|
||||
use Hyperf\RpcServer\Annotation\RpcService;
|
||||
|
||||
/**
|
||||
* @RpcService(name="CalculatorService", protocol="jsonrpc-http", server="jsonrpc-http")
|
||||
*/
|
||||
#[RpcService(name: "CalculatorService", protocol: "jsonrpc-http", server: "jsonrpc-http")]
|
||||
class CalculatorService implements CalculatorServiceInterface
|
||||
{
|
||||
// 实现一个加法方法,这里简单的认为参数都是 int 类型
|
||||
@ -160,15 +158,10 @@ use Hyperf\Event\Contract\ListenerInterface;
|
||||
use Hyperf\Framework\Event\BootApplication;
|
||||
use Hyperf\ServiceGovernance\DriverManager;
|
||||
|
||||
/**
|
||||
* @Listener
|
||||
*/
|
||||
#[Listener]
|
||||
class RegisterDriverListener implements ListenerInterface
|
||||
{
|
||||
/**
|
||||
* @var DriverManager
|
||||
*/
|
||||
protected $driverManager;
|
||||
protected DriverManager $driverManager;
|
||||
|
||||
public function __construct(DriverManager $manager)
|
||||
{
|
||||
|
@ -53,20 +53,17 @@ return [
|
||||
namespace App\Controller;
|
||||
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Hyperf\Contract\SessionInterface;
|
||||
|
||||
class IndexController
|
||||
{
|
||||
/**
|
||||
* @Inject()
|
||||
* @var \Hyperf\Contract\SessionInterface
|
||||
*/
|
||||
private $session;
|
||||
#[Inject]
|
||||
private SessionInterface $session;
|
||||
|
||||
public function index()
|
||||
{
|
||||
// 直接通过 $this->session 来使用
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -30,9 +30,7 @@ namespace App\Signal;
|
||||
use Hyperf\Signal\Annotation\Signal;
|
||||
use Hyperf\Signal\SignalHandlerInterface;
|
||||
|
||||
/**
|
||||
* @Signal
|
||||
*/
|
||||
#[Signal]
|
||||
class TermSignalHandler implements SignalHandlerInterface
|
||||
{
|
||||
public function listen(): array
|
||||
@ -89,15 +87,10 @@ use Psr\Container\ContainerInterface;
|
||||
|
||||
class CoroutineServerStopHandler implements SignalHandlerInterface
|
||||
{
|
||||
/**
|
||||
* @var ContainerInterface
|
||||
*/
|
||||
protected $container;
|
||||
|
||||
/**
|
||||
* @var ConfigInterface
|
||||
*/
|
||||
protected $config;
|
||||
protected ContainerInterface $container;
|
||||
|
||||
protected ConfigInterface $config;
|
||||
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
|
@ -102,16 +102,13 @@ $meta = $generator->degenerate($id);
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Hyperf\Snowflake\IdGenerator;
|
||||
use Hyperf\Snowflake\IdGenerator\SnowflakeIdGenerator;
|
||||
|
||||
class UserDefinedIdGenerator
|
||||
{
|
||||
/**
|
||||
* @var IdGenerator\SnowflakeIdGenerator
|
||||
*/
|
||||
protected $idGenerator;
|
||||
protected SnowflakeIdGenerator $idGenerator;
|
||||
|
||||
public function __construct(IdGenerator\SnowflakeIdGenerator $idGenerator)
|
||||
public function __construct(SnowflakeIdGenerator $idGenerator)
|
||||
{
|
||||
$this->idGenerator = $idGenerator;
|
||||
}
|
||||
|
@ -43,15 +43,13 @@ use Hyperf\SocketIOServer\BaseNamespace;
|
||||
use Hyperf\SocketIOServer\Socket;
|
||||
use Hyperf\Utils\Codec\Json;
|
||||
|
||||
/**
|
||||
* @SocketIONamespace("/")
|
||||
*/
|
||||
#[SocketIONamespace("/")]
|
||||
class WebSocketController extends BaseNamespace
|
||||
{
|
||||
/**
|
||||
* @Event("event")
|
||||
* @param string $data
|
||||
*/
|
||||
#[Event("event")]
|
||||
public function onEvent(Socket $socket, $data)
|
||||
{
|
||||
// 应答
|
||||
@ -59,9 +57,9 @@ class WebSocketController extends BaseNamespace
|
||||
}
|
||||
|
||||
/**
|
||||
* @Event("join-room")
|
||||
* @param string $data
|
||||
*/
|
||||
#[Event("join-room")]
|
||||
public function onJoinRoom(Socket $socket, $data)
|
||||
{
|
||||
// 将当前用户加入房间
|
||||
@ -73,9 +71,9 @@ class WebSocketController extends BaseNamespace
|
||||
}
|
||||
|
||||
/**
|
||||
* @Event("say")
|
||||
* @param string $data
|
||||
*/
|
||||
#[Event("say")]
|
||||
public function onSay(Socket $socket, $data)
|
||||
{
|
||||
$data = Json::decode($data);
|
||||
@ -116,9 +114,7 @@ class WebSocketController extends BaseNamespace
|
||||
|
||||
```php
|
||||
<?php
|
||||
/**
|
||||
* @Event("SomeEvent")
|
||||
*/
|
||||
#[Event("SomeEvent")]
|
||||
function onSomeEvent(\Hyperf\SocketIOServer\Socket $socket){
|
||||
|
||||
// sending to the client
|
||||
@ -193,8 +189,8 @@ $io->of('/foo')->emit();
|
||||
|
||||
/**
|
||||
* class内使用也等价
|
||||
* @SocketIONamespace("/foo")
|
||||
*/
|
||||
#[SocketIONamespace("/foo")]
|
||||
class FooNamespace extends BaseNamespace {
|
||||
public function onEvent(){
|
||||
$this->emit();
|
||||
@ -326,10 +322,8 @@ use Hyperf\SocketIOServer\Annotation\Event;
|
||||
use Hyperf\SocketIOServer\BaseNamespace;
|
||||
use Hyperf\SocketIOServer\Socket;
|
||||
|
||||
/**
|
||||
* @SocketIONamespace("/")
|
||||
* @Event()
|
||||
*/
|
||||
#[SocketIONamespace("/")]
|
||||
#[Event]
|
||||
class WebSocketController extends BaseNamespace
|
||||
{
|
||||
public function echo(Socket $socket, $data)
|
||||
@ -414,10 +408,7 @@ use Psr\Http\Server\RequestHandlerInterface;
|
||||
|
||||
class WebSocketAuthMiddleware implements MiddlewareInterface
|
||||
{
|
||||
/**
|
||||
* @var ContainerInterface
|
||||
*/
|
||||
protected $container;
|
||||
protected ContainerInterface $container;
|
||||
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
|
@ -81,9 +81,7 @@ use Hyperf\Task\Annotation\Task;
|
||||
|
||||
class AnnotationTask
|
||||
{
|
||||
/**
|
||||
* @Task
|
||||
*/
|
||||
#[Task]
|
||||
public function handle($cid)
|
||||
{
|
||||
return [
|
||||
@ -142,14 +140,9 @@ use MongoDB\Driver\WriteConcern;
|
||||
|
||||
class MongoTask
|
||||
{
|
||||
/**
|
||||
* @var Manager
|
||||
*/
|
||||
public $manager;
|
||||
public Manager $manager;
|
||||
|
||||
/**
|
||||
* @Task
|
||||
*/
|
||||
#[Task]
|
||||
public function insert(string $namespace, array $document)
|
||||
{
|
||||
$writeConcern = new WriteConcern(WriteConcern::MAJORITY, 1000);
|
||||
@ -160,9 +153,7 @@ class MongoTask
|
||||
return $result->getUpsertedCount();
|
||||
}
|
||||
|
||||
/**
|
||||
* @Task
|
||||
*/
|
||||
#[Task]
|
||||
public function query(string $namespace, array $filter = [], array $options = [])
|
||||
{
|
||||
$query = new Query($filter, $options);
|
||||
|
@ -120,10 +120,7 @@ use PHPUnit\Framework\TestCase;
|
||||
*/
|
||||
class ExampleTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var Client
|
||||
*/
|
||||
protected $client;
|
||||
protected Client $client;
|
||||
|
||||
public function __construct($name = null, array $data = [], $dataName = '')
|
||||
{
|
||||
@ -253,10 +250,7 @@ use App\Api\DemoApi;
|
||||
|
||||
class DemoLogic
|
||||
{
|
||||
/**
|
||||
* @var DemoApi $demoApi
|
||||
*/
|
||||
private $demoApi;
|
||||
private DemoApi $demoApi;
|
||||
|
||||
public function __construct(DemoApi $demoApi)
|
||||
{
|
||||
@ -345,11 +339,8 @@ use Hyperf\Di\Annotation\Inject;
|
||||
|
||||
class DemoLogic
|
||||
{
|
||||
/**
|
||||
* @var DemoApi $demoApi
|
||||
* @Inject()
|
||||
*/
|
||||
private $demoApi;
|
||||
#[Inject]
|
||||
private DemoApi $demoApi;
|
||||
|
||||
public function test()
|
||||
{
|
||||
@ -471,6 +462,3 @@ class DemoLogicTest extends HttpTestCase
|
||||
```shell
|
||||
phpdbg -dmemory_limit=1024M -qrr ./vendor/bin/co-phpunit -c phpunit.xml --colors=always
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
@ -64,11 +64,8 @@ use Hyperf\Contract\TranslatorInterface;
|
||||
|
||||
class FooController
|
||||
{
|
||||
/**
|
||||
* @Inject
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
private $translator;
|
||||
#[Inject]
|
||||
private TranslatorInterface $translator;
|
||||
|
||||
public function index()
|
||||
{
|
||||
@ -92,11 +89,8 @@ use Hyperf\Contract\TranslatorInterface;
|
||||
|
||||
class FooController
|
||||
{
|
||||
/**
|
||||
* @Inject
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
private $translator;
|
||||
#[Inject]
|
||||
private TranslatorInterface $translator;
|
||||
|
||||
public function index()
|
||||
{
|
||||
|
@ -170,11 +170,8 @@ use Hyperf\Validation\Contract\ValidatorFactoryInterface;
|
||||
|
||||
class IndexController
|
||||
{
|
||||
/**
|
||||
* @Inject()
|
||||
* @var ValidatorFactoryInterface
|
||||
*/
|
||||
protected $validationFactory;
|
||||
#[Inject]
|
||||
protected ValidatorFactoryInterface $validationFactory;
|
||||
|
||||
public function foo(RequestInterface $request)
|
||||
{
|
||||
@ -264,11 +261,8 @@ use Hyperf\Validation\Contract\ValidatorFactoryInterface;
|
||||
|
||||
class IndexController
|
||||
{
|
||||
/**
|
||||
* @Inject()
|
||||
* @var ValidatorFactoryInterface
|
||||
*/
|
||||
protected $validationFactory;
|
||||
#[Inject]
|
||||
protected ValidatorFactoryInterface $validationFactory;
|
||||
|
||||
public function foo(RequestInterface $request)
|
||||
{
|
||||
@ -966,9 +960,7 @@ use Hyperf\Event\Contract\ListenerInterface;
|
||||
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
|
||||
use Hyperf\Validation\Event\ValidatorFactoryResolved;
|
||||
|
||||
/**
|
||||
* @Listener
|
||||
*/
|
||||
#[Listener]
|
||||
class ValidatorFactoryResolvedListener implements ListenerInterface
|
||||
{
|
||||
|
||||
|
@ -121,9 +121,7 @@ namespace App\Controller;
|
||||
use Hyperf\HttpServer\Annotation\AutoController;
|
||||
use function Hyperf\ViewEngine\view;
|
||||
|
||||
/**
|
||||
* @AutoController(prefix="view")
|
||||
*/
|
||||
#[AutoController(prefix: "view")]
|
||||
class ViewController extends AbstractController
|
||||
{
|
||||
public function child()
|
||||
@ -210,15 +208,10 @@ use Hyperf\Framework\Event\BootApplication;
|
||||
use Hyperf\ViewEngine\Blade;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
/**
|
||||
* @Listener
|
||||
*/
|
||||
#[Listener]
|
||||
class BladeWithoutDoubleEncodingListener implements ListenerInterface
|
||||
{
|
||||
/**
|
||||
* @var ContainerInterface
|
||||
*/
|
||||
private $container;
|
||||
private ContainerInterface $container;
|
||||
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
|
@ -190,9 +190,7 @@ namespace App\Controller;
|
||||
use Hyperf\HttpServer\Annotation\AutoController;
|
||||
use Hyperf\View\RenderInterface;
|
||||
|
||||
/**
|
||||
* @AutoController
|
||||
*/
|
||||
#[AutoController]
|
||||
class ViewController
|
||||
{
|
||||
public function index(RenderInterface $render)
|
||||
|
@ -24,11 +24,8 @@ use Hyperf\WebSocketClient\Frame;
|
||||
|
||||
class IndexController
|
||||
{
|
||||
/**
|
||||
* @Inject
|
||||
* @var ClientFactory
|
||||
*/
|
||||
protected $clientFactory;
|
||||
#[Inject]
|
||||
protected ClientFactory $clientFactory;
|
||||
|
||||
public function index()
|
||||
{
|
||||
@ -54,4 +51,4 @@ class IndexController
|
||||
```php
|
||||
$autoClose = false;
|
||||
$client = $clientFactory->create($host, $autoClose);
|
||||
```
|
||||
```
|
||||
|
@ -195,15 +195,13 @@ use Hyperf\Di\Annotation\Inject;
|
||||
use Hyperf\HttpServer\Annotation\AutoController;
|
||||
use Hyperf\WebSocketServer\Sender;
|
||||
|
||||
/**
|
||||
* @AutoController
|
||||
*/
|
||||
#[AutoController]
|
||||
class ServerController
|
||||
{
|
||||
/**
|
||||
* @Inject
|
||||
* @var Sender
|
||||
*/
|
||||
#[Inject]
|
||||
protected $sender;
|
||||
|
||||
public function close(int $fd)
|
||||
|
Loading…
Reference in New Issue
Block a user