Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/php tidy #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ $emitter = new SocketIO\Emitter($redis);
$emitter->emit('event', 'payload str');
```

### Namespacing keys
You can set a base namespace on initialisation:
```php
$redis = new \Redis();
$redis->connect('127.0.0.1', '6379');
$emitter = new SocketIO\Emitter($redis, array(
'key' => 'socket-io:'
));
```

You can then specify events on specific emits:
```php
$emitter->emit('my-event', 'payload str');
```
Which will show up as:

```sh
"PUBLISH" "socket-io:my-event" ...
```

### Broadcasting and other flags
Possible flags
* json
Expand Down
274 changes: 165 additions & 109 deletions src/Emitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,140 +2,196 @@

namespace SocketIO;

define('EVENT', 2);
define('BINARY_EVENT', 5);

if (!function_exists('msgpack_pack')) {
require(__DIR__ . '/msgpack_pack.php');
}

class Emitter {
public function __construct($redis = FALSE, $opts = array()) {
if (is_array($redis)) {
$opts = $redis;
$redis = FALSE;
}

// Apply default arguments
$opts = array_merge(array('host' => 'localhost', 'port' => 6379), $opts);

if (!$redis) {
// Default to phpredis
if (extension_loaded('redis')) {
if (!isset($opts['socket']) && !isset($opts['host'])) throw new \Exception('Host should be provided when not providing a redis instance');
if (!isset($opts['socket']) && !isset($opts['port'])) throw new \Exception('Port should be provided when not providing a redis instance');
class Emitter
{
/**
* Event
* @var int
*/
public $event = 2;

/**
* Binary event
* @var int
*/
public $binaryEvent = 5;

/**
* Redis
* @var \Redis
*/
protected $redis;

/**
* Event key
* @var string
*/
protected $key;

/**
* Rooms
* @var array
*/
protected $_rooms = array();

/**
* Flags
* @var array
*/
protected $_flags = array();


public function __construct($redis = false, $opts = array()) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing NL and phpdoc


if (is_array($redis)) {
$opts = $redis;
$redis = false;
}

$redis = new \Redis();
if (isset($opts['socket'])) {
$redis->connect($opts['socket']);
} else {
$redis->connect($opts['host'], $opts['port']);
// Apply default arguments
$opts = array_merge(array('host' => 'localhost', 'port' => 6379), $opts);

if ($redis == false) {
// Default to phpredis
if (extension_loaded('redis')) {
if (!isset($opts['socket']) && !isset($opts['host'])) throw new \Exception('Host should be provided when not providing a redis instance');
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not use one line if syntax, always add {}

if (!isset($opts['socket']) && !isset($opts['port'])) throw new \Exception('Port should be provided when not providing a redis instance');

$redis = new \Redis();
if (isset($opts['socket'])) {
$redis->connect($opts['socket']);
} else {
$redis->connect($opts['host'], $opts['port']);
}
} else {
$redis = new \TinyRedisClient($opts['host'].':'.$opts['port']);
}
}
} else {
$redis = new \TinyRedisClient($opts['host'].':'.$opts['port']);
}
}

if (!is_callable(array($redis, 'publish'))) {
throw new \Exception('The Redis client provided is invalid. The client needs to implement the publish method. Try using the default client.');
}
// Fail on unsupported redis
if (!is_callable(array($redis, 'publish'))) {
throw new \Exception('The Redis client provided is invalid. The client needs to implement the publish method. Try using the default client.');
}

$this->redis = $redis;
$this->key = (isset($opts['key']) ? $opts['key'] : 'socket.io') . '#emitter';
$this->redis = $redis;
$this->key = (isset($opts['key']) ? $opts['key'] : 'socket.io');

$this->_rooms = array();
$this->_flags = array();
}
$this->_rooms = array();
$this->_flags = array();
}

/*
* Flags
*/
/**
* Flags
*
* @param string $flag
* @return SocketIO\Emitter
*/
public function __get($flag) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing NL, add empty line before @return as well

Same for every method

$this->_flags[$flag] = TRUE;
return $this;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing NL before return

}

public function __get($flag) {
$this->_flags[$flag] = TRUE;
return $this;
}
/**
* Read flags
*
* @param string $flag
* @return bool
*/
private function readFlag($flag) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private methods should not have a docblock

return isset($this->_flags[$flag]) ? $this->_flags[$flag] : false;
}

private function readFlag($flag) {
return isset($this->_flags[$flag]) ? $this->_flags[$flag] : false;
}
/**
* Broadcasting
*
* @param string $room
* @return SocketIO\Emitter
*/
public function in($room) {
if (!in_array($room, $this->_rooms)) {
$this->_rooms[] = $room;
}

/*
* Broadcasting
*/
return $this;
}

public function in($room) {
if (!in_array($room, $this->_rooms)) {
$this->_rooms[] = $room;
/**
* Alias for $this->in()
*
* @param string $room
* @return SocketIO\Emitter
*/
public function to($room) {
return $this->in($room);
}

return $this;
}

// Alias for in
public function to($room) {
return $this->in($room);
}

/*
* Namespace
*/

public function of($nsp) {
$this->_flags['nsp'] = $nsp;
return $this;
}

/*
* Emitting
*/

public function emit() {
$args = func_get_args();
$packet = array();

$packet['type'] = EVENT;
// handle binary wrapper args
for ($i = 0; $i < count($args); $i++) {
$arg = $args[$i];
if ($arg instanceof Binary) {
$args[$i] = strval($arg);
$this->binary;
}
/**
* Namespaces
*
* @param string $nsp
* @return SocketIO\Emitter
*/
public function of($nsp) {
$this->_flags['nsp'] = $nsp;
return $this;
}

if ($this->readFlag('binary')) $packet['type'] = BINARY_EVENT;
/**
* Emit the data
*
* @return SocketIO\Emitter
*/
public function emit($key = '', $data) {

$packet = array();
$packet['type'] = $this->event;

// handle binary wrapper args
for ($i = 0; $i < count($data); $i++) {
$arg = $data[$i];
if ($arg instanceof Binary) {
$data[$i] = strval($arg);
}
}

$packet['data'] = $args;
// If catch
if ($this->readFlag('binary')) $packet['type'] = $this->binaryEvent;

// set namespace
if (isset($this->_flags['nsp'])) {
$packet['nsp'] = $this->_flags['nsp'];
unset($this->_flags['nsp']);
} else {
$packet['nsp'] = '/';
}
$packet['data'] = $data;

// publish
$packed = msgpack_pack(array($packet, array(
'rooms' => $this->_rooms,
'flags' => $this->_flags
)));
// set namespaces
if (isset($this->_flags['nsp'])) {
$packet['nsp'] = $this->_flags['nsp'];
unset($this->_flags['nsp']);
} else {
$packet['nsp'] = '/';
}

// hack buffer extensions for msgpack with binary
if ($packet['type'] == BINARY_EVENT) {
$packed = str_replace(pack('c', 0xda), pack('c', 0xd8), $packed);
$packed = str_replace(pack('c', 0xdb), pack('c', 0xd9), $packed);
}
// publish
$packed = msgpack_pack( array($packet, array(
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove extra space, every argument should have its own line if multiline.

'rooms' => $this->_rooms,
'flags' => $this->_flags
))
);

// hack buffer extensions for msgpack with binary
if ($packet['type'] == $this->binaryEvent) {
$packed = str_replace(pack('c', 0xda), pack('c', 0xd8), $packed);
$packed = str_replace(pack('c', 0xdb), pack('c', 0xd9), $packed);
}

$this->redis->publish($this->key, $packed);
$this->redis->publish($this->key . $key, $packed);

// reset state
$this->_rooms = array();
$this->_flags = array();
// reset state
$this->_rooms = array();
$this->_flags = array();

return $this;
}
return $this;
}
}