bethrezen / nats
PHP 中的 nats.io 客户端
v1.0.1
2019-07-17 16:22 UTC
Requires
- ircmaxell/random-lib: ^1.2
Requires (Dev)
- leanphp/phpspec-code-coverage: ^4.2
- php-coveralls/php-coveralls: ^2.1
- phpspec/phpspec: ^4.0
- phpstan/phpstan: ^0.10.5
- phpunit/phpunit: ^7.0
- symfony/process: ^4.1
This package is auto-updated.
Last update: 2024-09-18 03:22:29 UTC
README
Travis
覆盖率
简介
PHP 客户端,用于NATS 消息系统。
要求
- php 5.6+
- gnatsd
用法
安装
首先,将 composer 下载到我们的项目目录中
curl -O https://getcomposer.org.cn/composer.phar
chmod +x composer.phar
现在让我们告诉 composer 我们项目的依赖项,在这种情况下,PHPNats。我们这样做是通过创建一个 composer.json 文件,并将其放置在我们项目的根目录中,紧挨着 composer.phar
{
"require": {
"repejota/nats": "dev-master"
}
}
让我们让 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 检查文档块和不同的性能提示。
创建者
Raül Pérez
许可
MIT,见 LICENSE