Permalink
Browse files

Modified the streams driver to support context options, added a relat…

…ed setting to force use of IPv4 to the sample configuration file
  • Loading branch information...
1 parent 694ecac commit 2b9096e577fb575f7d2831e092d56b2ee233b2d9 @elazar elazar committed May 25, 2012
@@ -97,6 +97,13 @@ class Phergie_Connection
protected $hostmask;
/**
+ * Stream context options
+ *
+ * @var array
+ */
+ protected $context = array();
+
+ /**
* Constructor to initialize instance properties.
*
* @param array $options Optional associative array of property values
@@ -386,6 +393,28 @@ public function getPassword()
}
/**
+ * Sets the stream context options to use.
+ *
+ * @param array $context Context options as they will be passed to stream_context_create()
+ * @return Phergie_Connection_Streams Implements a fluent interface
+ */
+ public function setContext(array $context)
+ {
+ $this->context = $context;
+ return $this;
+ }
+
+ /**
+ * Returns the stream context options in use.
+ *
+ * @return array Context options as they will be passed to stream_context_create()
+ */
+ public function getContext()
+ {
+ return $this->context;
+ }
+
+ /**
* Sets multiple connection settings using an array.
*
* @param array $options Associative array of setting names mapped to
@@ -342,13 +342,21 @@ public function getEvent()
* @param string $remote Address to connect the socket to
* @param int &$errno System level error number if connection fails
* @param string &$errstr System level error message if connection fails
+ * @param array $context Optional socket context options
*
* @return resource Established socket
*/
- protected function connect($remote, &$errno, &$errstr)
+ protected function connect($remote, &$errno, &$errstr, array $context = array())
{
// @codeCoverageIgnoreStart
- return @stream_socket_client($remote, $errno, $errstr);
+ return @stream_socket_client(
+ $remote,
+ $errno,
+ $errstr,
+ ini_get('default_socket_timeout'),
+ STREAM_CLIENT_CONNECT,
+ stream_context_create($context)
+ );
// @codeCoverageIgnoreEnd
}
@@ -368,11 +376,12 @@ public function doConnect()
$nick = $connection->getNick();
$realname = $connection->getRealname();
$transport = $connection->getTransport();
+ $context = $connection->getContext();
// Establish and configure the socket connection
$remote = $transport . '://' . $hostname . ':' . $port;
$errno = $errstr = null;
- $this->socket = $this->connect($remote, $errno, $errstr);
+ $this->socket = $this->connect($remote, $errno, $errstr, $context);
if (!$this->socket) {
throw new Phergie_Driver_Exception(
'Unable to connect: socket error ' . $errno . ' ' . $errstr,
View
@@ -14,6 +14,7 @@ return array(
// 'password' => 'password goes here if needed',
// 'transport' => 'ssl', // uncomment to connect using SSL
// 'encoding' => 'UTF-8', // uncomment if using UTF-8
+ // 'context' => array('socket' => array('bindto' => '0.0.0.0:0')), // uncomment to force use of IPv4
)
),
@@ -44,6 +44,7 @@ class Phergie_ConnectionTest extends PHPUnit_Framework_TestCase
'username' => 'MyUsername',
'realname' => 'MyRealName',
'password' => 'MyPassword',
+ 'context' => array('socket' => array('bindto' => '0.0.0.0:0')),
);
/**
@@ -60,11 +61,12 @@ public function dataProviderTestGetOptionReturnsDefault()
array('encoding', 'ISO-8859-1'),
array('port', 6667),
array('password', null),
+ array('context', array()),
);
}
/**
- * Tests that a default values are used for some options.
+ * Tests that default values are used for some options.
*
* @param string $option Name of the option with a default value
* @param mixed $value Default value of the option
@@ -79,17 +81,6 @@ public function testGetOptionReturnsDefault($option, $value)
}
/**
- * Tests that a default encoding is used if one isn't specified.
- *
- * @return void
- */
- public function testGetEncodingReturnsDefault()
- {
- $connection = new Phergie_Connection;
- $this->assertEquals('ISO-8859-1', $connection->getEncoding());
- }
-
- /**
* Tests that options can be set via the constructor.
*
* @return void
@@ -101,7 +101,8 @@ protected function getMockConnection()
'port' => 6667,
'username' => 'username',
'realname' => 'realname',
- 'transport' => 'tcp'
+ 'transport' => 'tcp',
+ 'context' => array(),
);
$connection = parent::getMockConnection();
foreach ($options as $key => $value) {

0 comments on commit 2b9096e

Please sign in to comment.