36、Hyperf 3 快速使用 - Hyperf 3 辅助类(完结)

作者: 温新

分类: 【Hyperf 3 基础系列】

阅读: 437

时间: 2023-04-25 16:17:52

hi,我是温新,一名 PHPer

Hypref 版本:Hyperf 3.0

学习目标:了解辅助类的使用及有哪些辅助类

Hyperf 官方文档没有列出所有辅助类的使用方法,仅列出以几个常用的好用的出来。本篇文章将参考扩展包列出一些辅助类的使用。

参考:https://github.com/hyperf/utils/tree/master/src

Coroutine 协程辅助类

文件:Hyperf\Utils\Coroutine.php

含义:该辅助类用于协助进行协程相关的判断或操作。

准备工作:

<?php

namespace App\Controller\demo;

use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\Utils\Coroutine;

#[Controller]
class HelpController
{
}

id

含义:通过静态方法 id() 获得当前所处的 协程 ID,如当前不处于协程环境下,则返回 -1

案例:

<?php

#[GetMapping('/co/index')]
public function coIndex()
{
    echo Coroutine::id();
}

create

含义:创建一个协程。,还可以通过全局函数 co(callable $callable)go(callable $callable) 创建协程。

案例:

<?php

#[GetMapping('/co/index')]
public function coIndex()
{
    // 输出情况根据你的情况而定
    echo Coroutine::id() . PHP_EOL; // 输出 2
    Coroutine::create(function () {
        echo Coroutine::id() . PHP_EOL; // 输出 3
    });
}

isCoroutine

含义:通过静态方法 inCoroutine() 判断当前是否处于协程环境下。

案例:

<?php
    
#[GetMapping('/co/index')]
public function coIndex()
{
   if (Coroutine::inCoroutine()) {
       echo '处于协程环境' . PHP_EOL;
   }
}

parendId

含义:获取当前协程的父 ID。

案例:

<?php
    
#[GetMapping('/co/index')]
public function coIndex()
{
    echo Coroutine::id() . PHP_EOL;

   Coroutine::create(function () {
       echo '当前协程ID' . Coroutine::id() . ',其父ID是:' . Coroutine::parentId() . PHP_EOL;
   });
}

exists

含义:判断协程是否存在。

案例:

<?php

#[GetMapping('/co/index')]
public function coIndex()
{
   Coroutine::create(function () {
       if (Coroutine::exists(Coroutine::parentId())) {
            echo '父协程存在' . PHP_EOL;
       }
   });
}

defer

含义:defer 用于资源的释放,会在协程关闭之前 (即协程函数执行完毕时) 进行调用,就算抛出了异常,已注册的 defer 也会被执行。

案例:

<?php
    
#[GetMapping('/co/index')]
public function coIndex()
{
   Coroutine::create(function () {
       Coroutine::defer(function () {
           echo '结束了吗' . PHP_EOL;
       });
   });
}

sleep

含义:休眠多久。

案例:

<?php
    
#[GetMapping('/co/index')]
public function coIndex()
{
   Coroutine::create(function () {
       sleep(3);
       // 后输出
       echo '睡了 3 秒之后我醒了' . PHP_EOL;
   });

   // 先输出
   echo '你总算醒了' . PHP_EOL;
}

Context 处理协程上下文

文件:Hyperf/Context/Context

含义:用于处理协程上下文,本质上是对 Swoole\Coroutine::getContext() 方法的一个封装,但区别在于这里兼容了非协程环境下的运行。

set

含义:设置当前协程上下文内容。

get

含义:获取协程上下文内容。

has

含义:判断是否设置协程上下文。

destroy

含义:不在协程环境时,释放上下文。

override

含义:检索值并通过闭包重写它。

案例:

<?php
// App\Controller\demo\HelpController.php
use Hyperf\Context\Context;

#[GetMapping('/cn/index')]
public function cnIndex()
{
    $id = Coroutine::id();
    Context::set($id, '协程上下文处理');
    Coroutine::sleep(3);

    Coroutine::create(function () {
        echo '子协程' . PHP_EOL;
    });

    if (Context::has($id)) {
        echo '获取当前协程上下文内容:' . Context::get($id) . PHP_EOL;
        Context::override($id, function () {
           return '王美丽';
        });
        echo '重写之后的值 ' . Context::get($id) . PHP_EOL;
    }
}

测试:

$ curl http://192.168.31.90:9501/cn/index
子协程
获取当前协程上下文内容:协程上下文处理
重写之后的值 王美丽

Arr 数组相关操作

文件位置:Hyperf\Utils\Arr.php

含义:该类用于对数组进行操作。数组中有很多方法,不一一列举,查看文档。

set

含义:数组中添加元素。

<?php
use Hyperf\Utils\Arr;
    
#[GetMapping('/arr/index')]
public function arrIndex()
{
    $data = [
        'name'=>'王美丽'
    ];
    
    Arr::set($data, 'age', 19);

    print_r(Arr::get());
}

get

含义:获取数组中的元素

<?php
use Hyperf\Utils\Arr;

#[GetMapping('/arr/index')]
public function arrIndex()
{
    $data = [
        'name'=>'王美丽'
    ];

    print_r(Arr::get($data, 'name'));
}

Str 字符串

文件:use Hyperf\Utils\Str

该类用于操作字符串。

<?php
use Hyperf\Utils\Str;

#[GetMapping('/str/index')]
public function strIndex()
{
    $newStr = Str::substr('hello world', 6, 11);
    print_r($newStr);
}

Network

ip

含义:获取 IP

<?php
    
use use Hyperf\Utils\Network;

#[GetMapping('/net/index')]
public function netIndex()
{
    echo Network::ip();
}

System

getCpuCoresNum

含义:获取 CPU 核数

use Hyperf\Utils\System;

echo System::getCpuCoresNum();

关于更多的类,还是要参考该助手包,去看代码。

我是温新,本篇文章结束。

Hyperf 3 基础系列到此就算结束了,还有一些并没有学习到,但这并不算结束,没有学习到的后续遇到了,将还是会通过文章的形式记录下来。

学了这这么多,需要动手加以复习,不然太容易忘记了。

Hyperf 3 基础系列到此结束,下一篇幅将是 Hyperf3 微服务系列。

我是温新,再见。

请登录后再评论