PHP现代化非阻塞标准库PSL

2024-08-19 16:11:08 浏览数 (2)

概述

azjezz/psl 是一个PHP标准库(PHP Standard Library)项目。这个项目是一个为PHP程序员提供的一致性、集中化且类型良好的API集合。它受到hhvm/hsl的启发,旨在为PHP开发提供的一套现代化、一致性、集中化、良好类型化、非阻塞的 API 集合工具集,从而简化开发过程并提高代码的可维护性和可移植性。提高 PHP 编程的效率和一致性,通过提供一套标准化的 API 来简化开发过程。

特点

  • 目标:为PHP程序员提供一套标准的、集中的、类型良好的API。
  • 灵感来源:该项目受到hhvm/hsl的启发,后者是HHVM(HipHop Virtual Machine,一种由Facebook开发的PHP执行引擎)的标准库。
  • 功能:通过其API集合,该库提供了多种功能。如:异步编程支持(通过Async命名空间)、TCP连接管理(通过TCP命名空间)、输入输出操作(通过IO命名空间)、Shell命令执行(通过Shell命名空间)以及字符串处理(通过Str命名空间)等。
  • 安装:该项目支持通过Composer进行安装,这是PHP的一个依赖管理工具,允许你声明项目所依赖的外部库。
  • 集成:该项目提供了与PsalmPHPStan的集成支持,Psalm是一个PHP静态分析工具,而PHPStan是另一个流行的PHP静态分析工具,这有助于开发者在开发过程中发现并修复潜在的问题。

安装

安装libsodium

代码语言:javascript复制
sudo /usr/local/php-8.3.1/bin/pecl install -f libsodium

请参考:PHP官方现代化核心加密库 Sodium

安装 azjezz/psl

代码语言:javascript复制
/usr/local/php-8.3.1/bin/php composer require azjezz/psl

异步协作

协作式多工(Cooperative Multitasking):是一种多工处理方式,多工是使电脑能同时处理多个程序的技术,相对于抢占式多工(Preemptive multitasking)由操作系统决定任务切换时机。协作式多工要求每一个运行中的程序,定时放弃(yield)自己的执行权利,告知操作系统可让下一个程序执行[1][2],因为需要程序之间合作达到调度,故称作协作式多工。

在多工处理中,处理器(CPU)会在相对于用户来说极短的时间内,依次分配处理能力给不同的程序,以实现看似同时运行多个程序的效果。举例来说,当你同时使用浏览器浏览网页和播放MP3音乐时,CPU会快速切换处理能力,让你感觉网页在下卷时音乐仍在播放。然而,如果其中一个程序出现问题,例如因为设计不良或出现故障而无法释放执行权,这样的行为会导致整个操作系统停顿,甚至死机。因此,在开发软件时,需要注意程序的质量和稳定性,避免出现此类问题,以确保系统的稳定运行。

Async组件构建在RevoltPHP之上,这使得它与Amphp和其他使用相同事件循环的库兼容。

RevoltPHP:https://github.com/revoltphp/event-loop

简单使用
代码语言:javascript复制
Asyncmain(static function(): int {
    $watcher = AsyncScheduler::onSignal(SIGINT, function (): never {
        IOwrite_error_line('SIGINT received, stopping...');
        exit(0);
    });

    AsyncScheduler::unreference($watcher);
    
    IOwrite_line('Hello, World!');

    [$version, $connection] = Asyncconcurrently([
        static fn() => Shellexecute('php', ['-v']),
        static fn() => TCPconnect('172.30.237.24', 2023),
    ]);
    $messages = Strsplit($version, "n");
    IOwrite_line('php, -v!'.json_encode($messages));
    foreach($messages as $message) {
        $connection->writeAll($message);
    }

    $connection->close();

    return 0;
});

输出

代码语言:javascript复制
Hello, World!
php, -v!["PHP 8.3.1 (cli) (built: Dec 25 2023 21:14:50) (NTS)","Copyright (c) The PHP Group","Zend Engine v4.3.1, Copyright (c) Zend Technologies",""]

密码组件

代码语言:javascript复制
<?php
/**
 * @desc password.php
 * @author Tinywan(ShaoBo Wan)
 */
declare(strict_types=1);

require 'vendor/autoload.php';

use function PslPasswordhash;
use function PslPasswordverify;
use function PslPasswordget_information;
use function PslPasswordneeds_rehash;

/** 1. 加密密码 */
$password = 'Resty2024';
$hash = hash($password);
echo '[x] 加密Hash:'. $hash . PHP_EOL;

/** 2. 验证密码 */
$check = verify($password,$hash);
echo '[x] 验证结果:'. $check . PHP_EOL;

/** 3. 获取hash信息 */
$informatio =  get_information($hash);
echo '[x] 获取Hash:'. json_encode($informatio) . PHP_EOL;

/** 4. 刷新重置hash值 */
needs_rehash($hash);

执行输出

代码语言:javascript复制
[x] 加密Hash:$2y$10$//WxKTrzGmyqH50vWXygyeEvuwevBE6gTo.t3ucj07PxToIPnKdQW
[x] 验证结果:1
[x] 获取Hash:{"algorithm":"bcrypt","options":{"cost":10}}

Unix套接字

代码语言:javascript复制
<?php
/**
 * @desc concurrent.php
 * @author Tinywan(ShaoBo Wan)
 */
declare(strict_types=1);

use PslAsync;
use PslFilesystem;
use PslIO;
use PslStr;
use PslUnix;

require __DIR__.'/vendor/autoload.php';

Asyncmain(static function (): int {
    if (PHP_OS_FAMILY === 'Windows') {
        IOwrite_error_line('This example requires does not support Windows.');

        return 0;
    }

    $file = Filesystemcreate_temporary_file(prefix: 'psl-examples') . ".sock";

    Asyncconcurrently([
        'server' => static function () use ($file): void {
            $server = UnixServer::create($file);

            IOwrite_error_line('< server is listening.');

            $connection = $server->nextConnection();

            IOwrite_error_line('< connection received.');
            IOwrite_error_line('< awaiting request.');

            $request = $connection->read();

            IOwrite_error_line('< received request: "%s".', $request);
            IOwrite_error_line('< sending response.');

            $connection->writeAll(Strreverse($request));
            $connection->close();

            IOwrite_error_line('< connection closed.');

            $server->close();

            IOwrite_error_line("< server stoppedn");
        },
        'client' => static function () use ($file): void {
            $client = Unixconnect($file);

            IOwrite_error_line('> client connected.');
            IOwrite_error_line('> sending request.');

            $client->writeAll('Hello, World!');

            IOwrite_error_line('> awaiting response.');

            $response = $client->readAll();

            IOwrite_error_line('> received response: "%s".', $response);

            $client->close();

            IOwrite_error_line('> client disconnected.');
        },
    ]);

    return 0;
});

执行输出

代码语言:javascript复制
< server is listening.
< connection received.
< awaiting request.
> client connected.
> sending request.
> awaiting response.
< received request: "Hello, World!".
< sending response.
< connection closed.
< server stopped

> received response: "!dlroW ,olleH".
> client disconnected.

TCP并行

代码语言:javascript复制
<?php
/**
 * @desc tcp.php
 * @author Tinywan(ShaoBo Wan)
 */
declare(strict_types=1);

require 'vendor/autoload.php';

use PslAsync;
use PslIO;
use PslStr;
use PslTCP;

Asyncmain(static function (): int {
    Asyncconcurrently([
        'server' => static function (): void {
            $server = TCPServer::create('localhost', 20248);

            IOwrite_error_line('< server is listening.');

            $connection = $server->nextConnection();

            IOwrite_error_line('< connection received.');
            IOwrite_error_line('< awaiting request.');

            $request = $connection->read();

            IOwrite_error_line('< received request: "%s".', $request);
            IOwrite_error_line('< sending response.');

            $connection->writeAll(Strreverse($request));
            $connection->close();

            IOwrite_error_line('< connection closed.');

            $server->close();

            IOwrite_error_line('< server stopped.');
        },
        'client' => static function (): void {
            $client = TCPconnect('localhost', 20248);

            IOwrite_error_line('> client connected');
            IOwrite_error_line('> sending request');

            $client->writeAll('Hello, World!');

            IOwrite_error_line('> awaiting response');

            $response = $client->readAll();

            IOwrite_error_line('> received response: "%s".', $response);

            $client->close();

            IOwrite_error_line('> client disconnected');
        },
    ]);

    return 0;
});

以上监听:20248 端口

执行输出

代码语言:javascript复制
< server is listening.
< connection received.
< awaiting request.
> client connected
> sending request
> awaiting response
< received request: "Hello, World!".
< sending response.
< connection closed.
< server stopped.
> received response: "!dlroW ,olleH".
> client disconnected

0 人点赞