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
1sudo /usr/local/php-8.3.1/bin/pecl install -f libsodium
请参考: PHP官方现代化核心加密库 Sodium
安装 azjezz/psl
1/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
1Async\main(static function(): int {
2 $watcher = Async\Scheduler::onSignal(SIGINT, function (): never {
3 IO\write_error_line('SIGINT received, stopping...');
4 exit(0);
5 });
6
7 Async\Scheduler::unreference($watcher);
8
9 IO\write_line('Hello, World!');
10
11 [$version, $connection] = Async\concurrently([
12 static fn() => Shell\execute('php', ['-v']),
13 static fn() => TCP\connect('172.30.237.24', 2023),
14 ]);
15 $messages = Str\split($version, "\n");
16 IO\write_line('php, -v!'.json_encode($messages));
17 foreach($messages as $message) {
18 $connection->writeAll($message);
19 }
20
21 $connection->close();
22
23 return 0;
24});
输出
1Hello, World! 2php, -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",""]
1<?php
2/**
3 * @desc password.php
4 * @author Tinywan(ShaoBo Wan)
5 */
6declare(strict_types=1);
7
8require 'vendor/autoload.php';
9
10use function \Psl\Password\hash;
11use function \Psl\Password\verify;
12use function \Psl\Password\get_information;
13use function \Psl\Password\needs_rehash;
14
15/** 1. 加密密码 */
16$password = 'Resty2024';
17$hash = hash($password);
18echo '[x] 加密Hash:'. $hash . PHP_EOL;
19
20/** 2. 验证密码 */
21$check = verify($password,$hash);
22echo '[x] 验证结果:'. $check . PHP_EOL;
23
24/** 3. 获取hash信息 */
25$informatio = get_information($hash);
26echo '[x] 获取Hash:'. json_encode($informatio) . PHP_EOL;
27
28/** 4. 刷新重置hash值 */
29needs_rehash($hash);
执行输出
1[x] 加密Hash:$2y$10$//WxKTrzGmyqH50vWXygyeEvuwevBE6gTo.t3ucj07PxToIPnKdQW 2[x] 验证结果:1 3[x] 获取Hash:{"algorithm":"bcrypt","options":{"cost":10}}
1<?php
2/**
3 * @desc concurrent.php
4 * @author Tinywan(ShaoBo Wan)
5 */
6declare(strict_types=1);
7
8use Psl\Async;
9use Psl\Filesystem;
10use Psl\IO;
11use Psl\Str;
12use Psl\Unix;
13
14require __DIR__.'/vendor/autoload.php';
15
16Async\main(static function (): int {
17 if (PHP_OS_FAMILY === 'Windows') {
18 IO\write_error_line('This example requires does not support Windows.');
19
20 return 0;
21 }
22
23 $file = Filesystem\create_temporary_file(prefix: 'psl-examples') . ".sock";
24
25 Async\concurrently([
26 'server' => static function () use ($file): void {
27 $server = Unix\Server::create($file);
28
29 IO\write_error_line('< server is listening.');
30
31 $connection = $server->nextConnection();
32
33 IO\write_error_line('< connection received.');
34 IO\write_error_line('< awaiting request.');
35
36 $request = $connection->read();
37
38 IO\write_error_line('< received request: "%s".', $request);
39 IO\write_error_line('< sending response.');
40
41 $connection->writeAll(Str\reverse($request));
42 $connection->close();
43
44 IO\write_error_line('< connection closed.');
45
46 $server->close();
47
48 IO\write_error_line("< server stopped\n");
49 },
50 'client' => static function () use ($file): void {
51 $client = Unix\connect($file);
52
53 IO\write_error_line('> client connected.');
54 IO\write_error_line('> sending request.');
55
56 $client->writeAll('Hello, World!');
57
58 IO\write_error_line('> awaiting response.');
59
60 $response = $client->readAll();
61
62 IO\write_error_line('> received response: "%s".', $response);
63
64 $client->close();
65
66 IO\write_error_line('> client disconnected.');
67 },
68 ]);
69
70 return 0;
71});
执行输出
1< server is listening. 2< connection received. 3< awaiting request. 4> client connected. 5> sending request. 6> awaiting response. 7< received request: "Hello, World!". 8< sending response. 9< connection closed. 10< server stopped 11 12> received response: "!dlroW ,olleH". 13> client disconnected.
1<?php
2/**
3 * @desc tcp.php
4 * @author Tinywan(ShaoBo Wan)
5 */
6declare(strict_types=1);
7
8require 'vendor/autoload.php';
9
10use Psl\Async;
11use Psl\IO;
12use Psl\Str;
13use Psl\TCP;
14
15Async\main(static function (): int {
16 Async\concurrently([
17 'server' => static function (): void {
18 $server = TCP\Server::create('localhost', 20248);
19
20 IO\write_error_line('< server is listening.');
21
22 $connection = $server->nextConnection();
23
24 IO\write_error_line('< connection received.');
25 IO\write_error_line('< awaiting request.');
26
27 $request = $connection->read();
28
29 IO\write_error_line('< received request: "%s".', $request);
30 IO\write_error_line('< sending response.');
31
32 $connection->writeAll(Str\reverse($request));
33 $connection->close();
34
35 IO\write_error_line('< connection closed.');
36
37 $server->close();
38
39 IO\write_error_line('< server stopped.');
40 },
41 'client' => static function (): void {
42 $client = TCP\connect('localhost', 20248);
43
44 IO\write_error_line('> client connected');
45 IO\write_error_line('> sending request');
46
47 $client->writeAll('Hello, World!');
48
49 IO\write_error_line('> awaiting response');
50
51 $response = $client->readAll();
52
53 IO\write_error_line('> received response: "%s".', $response);
54
55 $client->close();
56
57 IO\write_error_line('> client disconnected');
58 },
59 ]);
60
61 return 0;
62});
以上监听:
20248
端口
执行输出
1< server is listening. 2< connection received. 3< awaiting request. 4> client connected 5> sending request 6> awaiting response 7< received request: "Hello, World!". 8< sending response. 9< connection closed. 10< server stopped. 11> received response: "!dlroW ,olleH". 12> client disconnected