Skip to content

Commit

Permalink
convert the socket class to use the config trait
Browse files Browse the repository at this point in the history
  • Loading branch information
AD7six committed Mar 29, 2014
1 parent c80f741 commit c1efe23
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 33 deletions.
51 changes: 24 additions & 27 deletions src/Network/Socket.php
Expand Up @@ -16,6 +16,7 @@
*/
namespace Cake\Network;

use Cake\Core\InstanceConfigTrait;
use Cake\Error;
use Cake\Validation\Validation;

Expand All @@ -27,6 +28,8 @@
*/
class Socket {

use InstanceConfigTrait;

/**
* Object description
*
Expand All @@ -35,25 +38,18 @@ class Socket {
public $description = 'Remote DataSource Network Socket Interface';

/**
* Base configuration settings for the socket connection
* Default configuration settings for the socket connection
*
* @var array
*/
protected $_baseConfig = array(
protected $_defaultConfig = array(
'persistent' => false,
'host' => 'localhost',
'protocol' => 'tcp',
'port' => 80,
'timeout' => 30
);

/**
* Configuration settings for the socket connection
*
* @var array
*/
public $config = array();

/**
* Reference to socket connection resource
*
Expand Down Expand Up @@ -115,9 +111,10 @@ class Socket {
* @see Socket::$_baseConfig
*/
public function __construct($config = array()) {
$this->config = array_merge($this->_baseConfig, $config);
if (!is_numeric($this->config['protocol'])) {
$this->config['protocol'] = getprotobyname($this->config['protocol']);
$this->config($config);

if (!is_numeric($this->_config['protocol'])) {
$this->_config['protocol'] = getprotobyname($this->_config['protocol']);
}
}

Expand All @@ -133,27 +130,27 @@ public function connect() {
}

$scheme = null;
if (isset($this->config['request']['uri']) && $this->config['request']['uri']['scheme'] === 'https') {
if (isset($this->_config['request']['uri']) && $this->_config['request']['uri']['scheme'] === 'https') {
$scheme = 'ssl://';
}

if (!empty($this->config['context'])) {
$context = stream_context_create($this->config['context']);
if (!empty($this->_config['context'])) {
$context = stream_context_create($this->_config['context']);
} else {
$context = stream_context_create();
}

$connectAs = STREAM_CLIENT_CONNECT;
if ($this->config['persistent']) {
if ($this->_config['persistent']) {
$connectAs |= STREAM_CLIENT_PERSISTENT;
}

set_error_handler(array($this, '_connectionErrorHandler'));
$this->connection = stream_socket_client(
$scheme . $this->config['host'] . ':' . $this->config['port'],
$scheme . $this->_config['host'] . ':' . $this->_config['port'],
$errNum,
$errStr,
$this->config['timeout'],
$this->_config['timeout'],
$connectAs,
$context
);
Expand All @@ -171,7 +168,7 @@ public function connect() {

$this->connected = is_resource($this->connection);
if ($this->connected) {
stream_set_timeout($this->connection, $this->config['timeout']);
stream_set_timeout($this->connection, $this->_config['timeout']);
}
return $this->connected;
}
Expand Down Expand Up @@ -208,8 +205,8 @@ public function context() {
* @return string Host name
*/
public function host() {
if (Validation::ip($this->config['host'])) {
return gethostbyaddr($this->config['host']);
if (Validation::ip($this->_config['host'])) {
return gethostbyaddr($this->_config['host']);
}
return gethostbyaddr($this->address());
}
Expand All @@ -220,10 +217,10 @@ public function host() {
* @return string IP address
*/
public function address() {
if (Validation::ip($this->config['host'])) {
return $this->config['host'];
if (Validation::ip($this->_config['host'])) {
return $this->_config['host'];
}
return gethostbyname($this->config['host']);
return gethostbyname($this->_config['host']);
}

/**
Expand All @@ -232,10 +229,10 @@ public function address() {
* @return array IP addresses
*/
public function addresses() {
if (Validation::ip($this->config['host'])) {
return array($this->config['host']);
if (Validation::ip($this->_config['host'])) {
return array($this->_config['host']);
}
return gethostbynamel($this->config['host']);
return gethostbynamel($this->_config['host']);
}

/**
Expand Down
24 changes: 18 additions & 6 deletions tests/TestCase/Network/SocketTest.php
Expand Up @@ -52,7 +52,7 @@ public function tearDown() {
*/
public function testConstruct() {
$this->Socket = new Socket();
$config = $this->Socket->config;
$config = $this->Socket->config();
$this->assertSame($config, array(
'persistent' => false,
'host' => 'localhost',
Expand All @@ -64,16 +64,16 @@ public function testConstruct() {
$this->Socket->reset();
$this->Socket->__construct(array('host' => 'foo-bar'));
$config['host'] = 'foo-bar';
$this->assertSame($this->Socket->config, $config);
$this->assertSame($this->Socket->config(), $config);

$this->Socket = new Socket(array('host' => 'www.cakephp.org', 'port' => 23, 'protocol' => 'udp'));
$config = $this->Socket->config;
$config = $this->Socket->config();

$config['host'] = 'www.cakephp.org';
$config['port'] = 23;
$config['protocol'] = 17;

$this->assertSame($this->Socket->config, $config);
$this->assertSame($this->Socket->config(), $config);
}

/**
Expand Down Expand Up @@ -121,7 +121,7 @@ public static function invalidConnections() {
* return void
*/
public function testInvalidConnection($data) {
$this->Socket->config = array_merge($this->Socket->config, $data);
$this->Socket->config($data);
$this->Socket->connect();
}

Expand Down Expand Up @@ -231,7 +231,19 @@ public function testReset() {
);
$anotherSocket = new Socket($config);
$anotherSocket->reset();
$this->assertEquals(array(), $anotherSocket->config);

$expected = [
'persistent' => false,
'host' => 'localhost',
'protocol' => 'tcp',
'port' => 80,
'timeout' => 30
];
$this->assertEquals(
$expected,
$anotherSocket->config(),
'Reset should cause config to return the defaults defined in _defaultConfig'
);
}

/**
Expand Down

0 comments on commit c1efe23

Please sign in to comment.