ソースを参照

convert the socket class to use the config trait

AD7six 12 年 前
コミット
c1efe239a9
2 ファイル変更42 行追加33 行削除
  1. 24 27
      src/Network/Socket.php
  2. 18 6
      tests/TestCase/Network/SocketTest.php

+ 24 - 27
src/Network/Socket.php

@@ -16,6 +16,7 @@
  */
 namespace Cake\Network;
 
+use Cake\Core\InstanceConfigTrait;
 use Cake\Error;
 use Cake\Validation\Validation;
 
@@ -27,6 +28,8 @@ use Cake\Validation\Validation;
  */
 class Socket {
 
+	use InstanceConfigTrait;
+
 /**
  * Object description
  *
@@ -35,11 +38,11 @@ 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',
@@ -48,13 +51,6 @@ class Socket {
 	);
 
 /**
- * Configuration settings for the socket connection
- *
- * @var array
- */
-	public $config = array();
-
-/**
  * Reference to socket connection resource
  *
  * @var resource
@@ -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']);
 		}
 	}
 
@@ -133,27 +130,27 @@ class Socket {
 		}
 
 		$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
 		);
@@ -171,7 +168,7 @@ class Socket {
 
 		$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;
 	}
@@ -208,8 +205,8 @@ class Socket {
  * @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());
 	}
@@ -220,10 +217,10 @@ class Socket {
  * @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']);
 	}
 
 /**
@@ -232,10 +229,10 @@ class Socket {
  * @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']);
 	}
 
 /**

+ 18 - 6
tests/TestCase/Network/SocketTest.php

@@ -52,7 +52,7 @@ class SocketTest extends TestCase {
  */
 	public function testConstruct() {
 		$this->Socket = new Socket();
-		$config = $this->Socket->config;
+		$config = $this->Socket->config();
 		$this->assertSame($config, array(
 			'persistent'	=> false,
 			'host'			=> 'localhost',
@@ -64,16 +64,16 @@ class SocketTest extends TestCase {
 		$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);
 	}
 
 /**
@@ -121,7 +121,7 @@ class SocketTest extends TestCase {
  * return void
  */
 	public function testInvalidConnection($data) {
-		$this->Socket->config = array_merge($this->Socket->config, $data);
+		$this->Socket->config($data);
 		$this->Socket->connect();
 	}
 
@@ -231,7 +231,19 @@ class SocketTest extends TestCase {
 		);
 		$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'
+		);
 	}
 
 /**