企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## 概述 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的一个依赖管理工具,允许你声明项目所依赖的外部库。 * **集成**:该项目提供了与`Psalm`和`PHPStan`的集成支持,`Psalm`是一个PHP静态分析工具,而`PHPStan`是另一个流行的PHP静态分析工具,这有助于开发者在开发过程中发现并修复潜在的问题。 ## 安装 libsodium ``` sudo /usr/local/php-8.3.1/bin/pecl install -f libsodium ``` 安装 `azjezz/psl` ``` /usr/local/php-8.3.1/bin/php composer require azjezz/psl ``` ## 异步协作 > **协作式多工(Cooperative Multitasking)**:是一种多工处理方式,多工是使电脑能同时处理多个程序的技术,相对于抢占式多工(Preemptive multitasking)由操作系统决定任务切换时机。协作式多工要求每一个运行中的程序,定时放弃(yield)自己的执行权利,告知操作系统可让下一个程序执行[1][2],因为需要程序之间合作达到调度,故称作协作式多工。 在多工处理中,处理器(CPU)会在相对于用户来说极短的时间内,依次分配处理能力给不同的程序,以实现看似同时运行多个程序的效果。举例来说,当你同时使用浏览器浏览网页和播放MP3音乐时,CPU会快速切换处理能力,让你感觉网页在下卷时音乐仍在播放。然而,如果其中一个程序出现问题,例如因为设计不良或出现故障而无法释放执行权,这样的行为会导致整个操作系统停顿,甚至死机。因此,在开发软件时,需要注意程序的质量和稳定性,避免出现此类问题,以确保系统的稳定运行。 维基百科:https://zh.wikipedia.org/wiki/%E5%8D%8F%E4%BD%9C%E5%BC%8F%E5%A4%9A%E4%BB%BB%E5%8A%A1 > Async组件构建在[RevoltPHP](https://github.com/revoltphp/event-loop)之上,这使得它与[Amphp](https://github.com/amphp)和其他使用相同事件循环的库兼容。 RevoltPHP:https://github.com/revoltphp/event-loop #### 简单使用 ``` Async\main(static function(): int { $watcher = Async\Scheduler::onSignal(SIGINT, function (): never { IO\write_error_line('SIGINT received, stopping...'); exit(0); }); Async\Scheduler::unreference($watcher); IO\write_line('Hello, World!'); [$version, $connection] = Async\concurrently([ static fn() => Shell\execute('php', ['-v']), static fn() => TCP\connect('172.30.237.24', 2023), ]); $messages = Str\split($version, "\n"); IO\write_line('php, -v!'.json_encode($messages)); foreach($messages as $message) { $connection->writeAll($message); } $connection->close(); return 0; }); ``` 输出 ``` 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",""] ``` ## 密码组件 ```php <?php /** * @desc password.php * @author Tinywan(ShaoBo Wan) * @date 2024/8/15 19:35 */ declare(strict_types=1); require 'vendor/autoload.php'; use function \Psl\Password\hash; use function \Psl\Password\verify; use function \Psl\Password\get_information; use function \Psl\Password\needs_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); ``` 执行输出 ``` [x] 加密Hash:$2y$10$//WxKTrzGmyqH50vWXygyeEvuwevBE6gTo.t3ucj07PxToIPnKdQW [x] 验证结果:1 [x] 获取Hash:{"algorithm":"bcrypt","options":{"cost":10}} ``` ## Unix套接字 ```php <?php /** * @desc concurrent.php * @author Tinywan(ShaoBo Wan) * @date 2024/8/15 20:12 */ declare(strict_types=1); use Psl\Async; use Psl\Filesystem; use Psl\IO; use Psl\Str; use Psl\Unix; require __DIR__.'/vendor/autoload.php'; Async\main(static function (): int { if (PHP_OS_FAMILY === 'Windows') { IO\write_error_line('This example requires does not support Windows.'); return 0; } $file = Filesystem\create_temporary_file(prefix: 'psl-examples') . ".sock"; Async\concurrently([ 'server' => static function () use ($file): void { $server = Unix\Server::create($file); IO\write_error_line('< server is listening.'); $connection = $server->nextConnection(); IO\write_error_line('< connection received.'); IO\write_error_line('< awaiting request.'); $request = $connection->read(); IO\write_error_line('< received request: "%s".', $request); IO\write_error_line('< sending response.'); $connection->writeAll(Str\reverse($request)); $connection->close(); IO\write_error_line('< connection closed.'); $server->close(); IO\write_error_line("< server stopped\n"); }, 'client' => static function () use ($file): void { $client = Unix\connect($file); IO\write_error_line('> client connected.'); IO\write_error_line('> sending request.'); $client->writeAll('Hello, World!'); IO\write_error_line('> awaiting response.'); $response = $client->readAll(); IO\write_error_line('> received response: "%s".', $response); $client->close(); IO\write_error_line('> client disconnected.'); }, ]); return 0; }); ``` 执行输出 ``` < 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并行 ``` <?php /** * @desc tcp.php * @author Tinywan(ShaoBo Wan) */ declare(strict_types=1); require 'vendor/autoload.php'; use Psl\Async; use Psl\IO; use Psl\Str; use Psl\TCP; Async\main(static function (): int { Async\concurrently([ 'server' => static function (): void { $server = TCP\Server::create('localhost', 20248); IO\write_error_line('< server is listening.'); $connection = $server->nextConnection(); IO\write_error_line('< connection received.'); IO\write_error_line('< awaiting request.'); $request = $connection->read(); IO\write_error_line('< received request: "%s".', $request); IO\write_error_line('< sending response.'); $connection->writeAll(Str\reverse($request)); $connection->close(); IO\write_error_line('< connection closed.'); $server->close(); IO\write_error_line('< server stopped.'); }, 'client' => static function (): void { $client = TCP\connect('localhost', 20248); IO\write_error_line('> client connected'); IO\write_error_line('> sending request'); $client->writeAll('Hello, World!'); IO\write_error_line('> awaiting response'); $response = $client->readAll(); IO\write_error_line('> received response: "%s".', $response); $client->close(); IO\write_error_line('> client disconnected'); }, ]); return 0; }); ``` > 以上监听:`20248` 端口 执行输出 ```ts < 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 ```