Skip to content

Commit

Permalink
phpcent V4 for Centrifugo v3 (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
FZambia authored Sep 6, 2021
1 parent 3590d26 commit 146734a
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 98 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

name: "test"

on: [push, pull_request]

jobs:
test:
name: Test with PHP ${{ matrix.php-version }}
runs-on: ubuntu-latest
# Prevent duplicate builds on internal PRs.
if: github.event_name == 'push' || github.event.pull_request.head.repo.full_name != github.repository
strategy:
matrix:
php-version: ["7.4", "8.0"]
steps:
- uses: "actions/checkout@v2"

- uses: "shivammathur/setup-php@v2"
with:
php-version: "${{ matrix.php-version }}"

- name: Composer install
uses: "ramsey/composer-install@v1"

- name: Start Centrifugo
run: docker run -d -p 8000:8000 centrifugo/centrifugo:latest centrifugo --api_insecure

- name: Run tests
run: vendor/bin/phpunit
19 changes: 0 additions & 19 deletions .travis.yml

This file was deleted.

10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
phpcent
=======

[![Build Status](https://img.shields.io/travis/centrifugal/phpcent.svg?style=flat-square)](https://travis-ci.org/centrifugal/phpcent)
[![Build Status](https://github.com/centrifugal/phpcent/workflows/build/badge.svg?branch=master)](https://github.com/centrifugal/phpcent/actions)
[![Latest Version](https://img.shields.io/github/release/centrifugal/phpcent.svg?style=flat-square)](https://github.com/centrifugal/phpcent/releases)

PHP library to communicate with Centrifugo v2 HTTP API.
PHP library to communicate with Centrifugo v3 HTTP API.

Library is published on the Composer: https://packagist.org/packages/centrifugal/phpcent

```bash
composer require centrifugal/phpcent:~3.0
composer require centrifugal/phpcent:~4.0
```

See [Centrifugo documentation](https://centrifugal.github.io/centrifugo/)
See [Centrifugo server API documentation](https://centrifugal.dev/docs/server/server_api).

Basic Usage:

Expand Down Expand Up @@ -43,7 +43,7 @@ $token = $client->setSecret("Centrifugo secret key")->generatePrivateChannelToke
Also API key and secret can be set in constructor:

```php
$client = new \phpcent\Client("http://localhost:8000/api", "Centrifugo API key", "Centrifugo secret key");
$client = new \phpcent\Client("http://localhost:8000/api", "<API key>", "<secret key>");
```

Timeouts:
Expand Down
8 changes: 8 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# v4.0.0

Adapt to work with Centrifugo v3.

* Deprecated `history_remove` removed (in favour of `historyRemove`)
* Deprecated `presence_stats` removed (in favour of `presenceStats`)
* Drop support for PHP < 7.0.0

# v3.1.1

* option to force IPv4 addresses when resolving hostnames, see [#44](https://github.com/centrifugal/phpcent/issues/44) and [#45](https://github.com/centrifugal/phpcent/pull/45). Thanks [Steve Therrien](https://github.com/SteveTherrien)!
Expand Down
27 changes: 17 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
{
"name": "centrifugal/phpcent",
"description":"PHP library to communicate with Centrifugo HTTP API",
"keywords": ["centrifugo", "websocket", "realtime", "messaging"],
"description": "PHP library to communicate with Centrifugo HTTP API",
"keywords": [
"centrifugo",
"websocket",
"realtime",
"messaging"
],
"homepage": "https://github.com/centrifugal/phpcent",
"version": "3.1.1",
"version": "4.0.0",
"license": "MIT",
"require":{
"php": ">=5.4.0",
"ext-json":"*",
"ext-curl":"*"
"require": {
"php": ">=7.0.0",
"ext-json": "*",
"ext-curl": "*"
},
"require-dev": {
"phpunit/phpunit": "5.*"
"phpunit/phpunit": "9.*"
},
"autoload": {
"psr-4": { "phpcent\\": "src" }
"psr-4": {
"phpcent\\": "src"
}
}
}
}
131 changes: 71 additions & 60 deletions src/Client.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php namespace phpcent;
<?php

namespace phpcent;

/**
* Centrifugo API Client
Expand Down Expand Up @@ -134,13 +136,15 @@ public function forceIpResolveV4()
*
* @param string $channel
* @param array $data
* @param boolean $skipHistory (optional)
* @return mixed
*/
public function publish($channel, $data)
public function publish($channel, $data, $skipHistory = false)
{
return $this->send('publish', array(
'channel' => $channel,
'data' => $data,
'skip_history' => $skipHistory,
));
}

Expand All @@ -149,13 +153,32 @@ public function publish($channel, $data)
*
* @param array $channels
* @param array $data
* @param boolean $skipHistory (optional)
* @return mixed
*/
public function broadcast($channels, $data)
public function broadcast($channels, $data, $skipHistory = false)
{
return $this->send('broadcast', array(
'channels' => $channels,
'data' => $data,
'skip_history' => $skipHistory,
));
}

/**
* Subscribe user to channel.
*
* @param string $channel
* @param string $user
* @param string $client (optional)
* @return mixed
*/
public function subscribe($channel, $user, $client = '')
{
return $this->send('subscribe', array(
'channel' => $channel,
'user' => $user,
'client' => $client,
));
}

Expand All @@ -164,26 +187,30 @@ public function broadcast($channels, $data)
*
* @param string $channel
* @param string $user
* @param string $client (optional)
* @return mixed
*/
public function unsubscribe($channel, $user)
public function unsubscribe($channel, $user, $client = '')
{
return $this->send('unsubscribe', array(
'channel' => $channel,
'user' => $user,
'client' => $client,
));
}

/**
* Disconnect user.
*
* @param string $user
* @param string $client (optional)
* @return mixed
*/
public function disconnect($user)
public function disconnect($user, $client = '')
{
return $this->send('disconnect', array(
'user' => $user,
'client' => $client,
));
}

Expand All @@ -200,18 +227,6 @@ public function presence($channel)
));
}

/**
* Get channel presence stats.
* Deprecated: use presenceStats instead.
*
* @param string $channel
* @return mixed
*/
public function presence_stats($channel)
{
return $this->presenceStats($channel);
}

/**
* Get channel presence stats.
*
Expand All @@ -229,25 +244,18 @@ public function presenceStats($channel)
* Get channel history.
*
* @param string $channel
* @param int $limit (optional)
* @param array $since (optional)
* @param boolean $reverse (optional)
* @return mixed
*/
public function history($channel)
public function history($channel, $limit = 0, $since = array(), $reverse = false)
{
return $this->send('history', array(
'channel' => $channel,
));
}

/**
* Remove channel history.
* Deprecated: use historyRemove instead.
*
* @param string $channel
* @return mixed
*/
public function history_remove($channel)
{
return $this->historyRemove($channel);
$params = array('channel' => $channel, 'limit' => $limit, 'reverse' => $reverse);
if (!empty($since)) {
$params['since'] = $since;
}
return $this->send('history', $params);
}

/**
Expand All @@ -265,12 +273,15 @@ public function historyRemove($channel)

/**
* Get all active channels.
*
*
* @param string $pattern (optional)
* @return mixed
*/
public function channels()
public function channels($pattern = '')
{
return $this->send('channels');
return $this->send("channels", array(
'pattern' => $pattern,
));
}

/**
Expand Down Expand Up @@ -342,28 +353,28 @@ public function generatePrivateChannelToken($client, $channel, $exp = 0, $info =
return implode('.', $segments);
}

/*
* Function added for backward compatibility with PHP version < 5.5
*/
/*
* Function added for backward compatibility with PHP version < 5.5
*/
public function _json_last_error_msg()
{
if (function_exists('json_last_error_msg')) {
return json_last_error_msg();
}
static $ERRORS = array(
JSON_ERROR_NONE => 'No error',
JSON_ERROR_DEPTH => 'Maximum stack depth exceeded',
JSON_ERROR_STATE_MISMATCH => 'State mismatch (invalid or malformed JSON)',
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
JSON_ERROR_SYNTAX => 'Syntax error',
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded'
);

$error = json_last_error();
return isset($ERRORS[$error]) ? $ERRORS[$error] : 'Unknown error';
}

public function _json_last_error_msg() {
if (function_exists('json_last_error_msg')) {
return json_last_error_msg();
}
static $ERRORS = array(
JSON_ERROR_NONE => 'No error',
JSON_ERROR_DEPTH => 'Maximum stack depth exceeded',
JSON_ERROR_STATE_MISMATCH => 'State mismatch (invalid or malformed JSON)',
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
JSON_ERROR_SYNTAX => 'Syntax error',
JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded'
);

$error = json_last_error();
return isset($ERRORS[$error]) ? $ERRORS[$error] : 'Unknown error';
}

private function send($method, $params = array())
private function send($method, $params = array())
{
$response = \json_decode($this->request($method, $params), $this->useAssoc);
if (JSON_ERROR_NONE !== json_last_error()) {
Expand Down Expand Up @@ -423,9 +434,9 @@ private function request($method, $params)
if (empty($headers["http_code"]) || ($headers["http_code"] != 200)) {
throw new \Exception(
"Response code: "
. $headers["http_code"]
. PHP_EOL
. "cURL error: " . $error . PHP_EOL
. $headers["http_code"]
. PHP_EOL
. "cURL error: " . $error . PHP_EOL
);
}
return $data;
Expand Down
8 changes: 4 additions & 4 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
class ClientTest extends PHPUnit_Framework_TestCase
class ClientTest extends PHPUnit\Framework\TestCase
{
protected $client;
public function setUp()
public function setUp(): void
{
$this->client = new \phpcent\Client("http://localhost:8000/api");
}
Expand All @@ -22,14 +22,14 @@ public function testPublish()
$this->client->setUseAssoc(true);
$res = $this->client->publish('channel', ["message" => "Hello World"]);
$this->assertNotNull($res);
$this->assertEquals([], $res);
$this->assertEquals(["result" => array()], $res);
}
public function testPublishCentrifugoError()
{
$this->client->setUseAssoc(true);
$res = $this->client->publish('namespace:channel', ["message" => "Hello World"]);
$this->assertNotNull($res);
$this->assertEquals(["error" => ["code" => 102, "message" => "namespace not found"]], $res);
$this->assertEquals(["error" => ["code" => 102, "message" => "unknown channel"]], $res);
}
public function testPublishNetworkError()
{
Expand Down

0 comments on commit 146734a

Please sign in to comment.