-
Notifications
You must be signed in to change notification settings - Fork 64
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
base: master
Are you sure you want to change the base?
Feature/php tidy #14
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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()) { | ||
|
||
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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing NL, add empty line before Same for every method |
||
$this->_flags[$flag] = TRUE; | ||
return $this; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing NL and phpdoc