PHP code example of xhtkyy / hyperf-tools
1. Go to this page and download the library: Download xhtkyy/hyperf-tools library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
xhtkyy / hyperf-tools example snippets
$stream = new Stream();
//支持流式 多次写入
$stream->write($response)
$stream->write($response)
$stream->write($response)
...
// 记得关闭,不然还继续走 普通响应
$stream->close();
return [
'scan' => [
...
'class_map' => [
//需要反射增加
\Google\Protobuf\Internal\DescriptorPool::class => BASE_PATH.'/vendor/xhtkyy/hyperf-tools/class_map/protobuf/DescriptorPool.php',
//需要nacos支持阿里nacos的需要增加
\Hyperf\Nacos\AbstractProvider::class => BASE_PATH.'/vendor/xhtkyy/hyperf-tools/class_map/nacos/AbstractProvider.php',
]
],
];
//使用
\Xhtkyy\HyperfTools\Grpc\Exception\GrpcError::throw("错误信息", -1);
public function __construct(protected Xhtkyy\HyperfTools\App\ContainerInterface $container) {
}
//使用
$this->container->get(...);
// 或者 通过声明注入
[Inject]
protected Xhtkyy\HyperfTools\App\ContainerInterface
//继承
class MessageRepo extends CURDRepo {
//声明要操作的对象
protected string $model = Message::class;
}
//开始使用
class MessageController {
#[Inject]
protected MessageRepo $messageRepo;
/**
* 获取列表
* @return array
*/
public function getList(): array {
$res = $this->messageRepo
//支持关联
->with([
"app" => function ($query) {
$query->selectRaw("id,app_name");
}
])
//等比查询
->eqWhere("id,platform_id,app_id,channel_id,role,status,task_id") //要查询的字段 已经处理判断 请求对象中是否存在
//范围查询
->betweenWhere("created_at")
//模糊查询
->likeWhere("title,accept,user_name")
//获取列表
->getList();
return $res;
}
}
Router::addServer('grpc', function () {
// 注意添加健康检查
Router::addGroup('/grpc.health.v1.Health', function () {
Router::post('/Check', [\Xhtkyy\HyperfTools\Grpc\Health\HealthController::class, 'check']);
Router::post('/Watch', [\Xhtkyy\HyperfTools\Grpc\Health\HealthController::class, 'watch']);
},[
"register" => false //配置不注册到服务发现中心,默认注册
]);
...
});
php bin/hyperf.php vendor:publish hyperf/service-governance
php bin/hyperf.php vendor:publish xhtkyy/hyperf-tools