workfront / nats
PHP 的 nats.io 客户端
2.2.3
2020-02-27 07:13 UTC
Requires
- php: ^7.1
Requires (Dev)
- leanphp/phpspec-code-coverage: ^3.2@dev
- phpspec/phpspec: ^3.0
- phpunit/phpunit: 5.*
- satooshi/php-coveralls: 2.0.x-dev
This package is auto-updated.
Last update: 2024-09-05 16:23:30 UTC
README
介绍
PHP 的 NATS 消息系统客户端。这是由 repejota 开发的 phpnats 分支,支持更近版本的 NATS(2.2.0-beta)并对代码进行了微小改进,使其更适合 PHP7 环境。
要求
- php 7.1+
- gnatsd
使用方法
安装
首先,将 composer 下载到我们的项目目录中
curl -O https://getcomposer.org.cn/composer.phar
chmod +x composer.phar
现在,让我们告诉 composer 我们项目的依赖项,在这种情况下是 PHPNats。我们这样做是通过创建一个 composer.json 文件,并将其放置在我们的项目的根目录中,紧邻 composer.phar
{
"require": {
"workfront/nats": "^2.2.3"
}
}
让我们让 Composer 发挥其魔力
php composer.phar install
Composer 将下载 composer.json 中定义的所有依赖项,并准备所有必要的文件以进行自动加载。
基本使用方法
$client = new \Nats\Connection(); $client->connect(); // Publish Subscribe // Simple Subscriber. $client->subscribe( 'foo', function ($message) { printf("Data: %s\r\n", $message->getBody()); } ); // Simple Publisher. $client->publish('foo', 'Marty McFly'); // Wait for 1 message. $client->wait(1); // Request Response // Responding to requests. $sid = $client->subscribe( 'sayhello', function ($message) { $message->reply('Reply: Hello, '.$message->getBody().' !!!'); } ); // Request. $client->request( 'sayhello', 'Marty McFly', function ($message) { echo $message->getBody(); } );
编码连接
$encoder = new \Nats\Encoders\JSONEncoder(); $options = new \Nats\ConnectionOptions(); $client = new \Nats\EncodedConnection($options, $encoder); $client->connect(); // Publish Subscribe // Simple Subscriber. $client->subscribe( 'foo', function ($payload) { printf("Data: %s\r\n", $payload->getBody()[1]); } ); // Simple Publisher. $client->publish( 'foo', [ 'Marty', 'McFly', ] ); // Wait for 1 message. $client->wait(1); // Request Response // Responding to requests. $sid = $client->subscribe( 'sayhello', function ($message) { $message->reply('Reply: Hello, '.$message->getBody()[1].' !!!'); } ); // Request. $client->request( 'sayhello', [ 'Marty', 'McFly', ], function ($message) { echo $message->getBody(); } );
开发者信息
版本
测试
测试在 tests
目录中。要运行它们,您需要 PHPUnit
并执行 make test-tdd
。
我们还在 spec
目录下有一个 BDD 测试套件。要运行套件,您需要 PHPSpec
并执行 make test-bdd
。
您还可以使用 make test
执行所有套件(TDD + BDD)。
代码质量
我们使用 PHP Code Sniffer 来确保我们的代码遵循高质量标准。
要执行代码分析,请执行 make lint
。
在执行代码检查时,目前有三个步骤
- 首先使用 PHP 自身进行代码检查
php -l
- 然后使用 PSR2 标准
- 最后使用自定义的 ruleset.xml 进行检查,该文件检查 doxygen 注释和不同的性能提示。
创建者
Raül Pérez
许可证
MIT,见 LICENSE