Skip to content

Commit

Permalink
Modified the streams driver to support context options, added a relat…
Browse files Browse the repository at this point in the history
…ed setting to force use of IPv4 to the sample configuration file
  • Loading branch information
elazar committed May 25, 2012
1 parent 694ecac commit 2b9096e
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 16 deletions.
29 changes: 29 additions & 0 deletions Phergie/Connection.php
Expand Up @@ -96,6 +96,13 @@ class Phergie_Connection
*/ */
protected $hostmask; protected $hostmask;


/**
* Stream context options
*
* @var array
*/
protected $context = array();

/** /**
* Constructor to initialize instance properties. * Constructor to initialize instance properties.
* *
Expand Down Expand Up @@ -385,6 +392,28 @@ public function getPassword()
return $this->password; return $this->password;
} }


/**
* 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. * Sets multiple connection settings using an array.
* *
Expand Down
15 changes: 12 additions & 3 deletions Phergie/Driver/Streams.php
Expand Up @@ -342,13 +342,21 @@ public function getEvent()
* @param string $remote Address to connect the socket to * @param string $remote Address to connect the socket to
* @param int &$errno System level error number if connection fails * @param int &$errno System level error number if connection fails
* @param string &$errstr System level error message if connection fails * @param string &$errstr System level error message if connection fails
* @param array $context Optional socket context options
* *
* @return resource Established socket * @return resource Established socket
*/ */
protected function connect($remote, &$errno, &$errstr) protected function connect($remote, &$errno, &$errstr, array $context = array())
{ {
// @codeCoverageIgnoreStart // @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 // @codeCoverageIgnoreEnd
} }


Expand All @@ -368,11 +376,12 @@ public function doConnect()
$nick = $connection->getNick(); $nick = $connection->getNick();
$realname = $connection->getRealname(); $realname = $connection->getRealname();
$transport = $connection->getTransport(); $transport = $connection->getTransport();
$context = $connection->getContext();


// Establish and configure the socket connection // Establish and configure the socket connection
$remote = $transport . '://' . $hostname . ':' . $port; $remote = $transport . '://' . $hostname . ':' . $port;
$errno = $errstr = null; $errno = $errstr = null;
$this->socket = $this->connect($remote, $errno, $errstr); $this->socket = $this->connect($remote, $errno, $errstr, $context);
if (!$this->socket) { if (!$this->socket) {
throw new Phergie_Driver_Exception( throw new Phergie_Driver_Exception(
'Unable to connect: socket error ' . $errno . ' ' . $errstr, 'Unable to connect: socket error ' . $errno . ' ' . $errstr,
Expand Down
1 change: 1 addition & 0 deletions Settings.php.dist
Expand Up @@ -14,6 +14,7 @@ return array(
// 'password' => 'password goes here if needed', // 'password' => 'password goes here if needed',
// 'transport' => 'ssl', // uncomment to connect using SSL // 'transport' => 'ssl', // uncomment to connect using SSL
// 'encoding' => 'UTF-8', // uncomment if using UTF-8 // 'encoding' => 'UTF-8', // uncomment if using UTF-8
// 'context' => array('socket' => array('bindto' => '0.0.0.0:0')), // uncomment to force use of IPv4
) )
), ),


Expand Down
15 changes: 3 additions & 12 deletions Tests/Phergie/ConnectionTest.php
Expand Up @@ -44,6 +44,7 @@ class Phergie_ConnectionTest extends PHPUnit_Framework_TestCase
'username' => 'MyUsername', 'username' => 'MyUsername',
'realname' => 'MyRealName', 'realname' => 'MyRealName',
'password' => 'MyPassword', 'password' => 'MyPassword',
'context' => array('socket' => array('bindto' => '0.0.0.0:0')),
); );


/** /**
Expand All @@ -60,11 +61,12 @@ public function dataProviderTestGetOptionReturnsDefault()
array('encoding', 'ISO-8859-1'), array('encoding', 'ISO-8859-1'),
array('port', 6667), array('port', 6667),
array('password', null), 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 string $option Name of the option with a default value
* @param mixed $value Default value of the option * @param mixed $value Default value of the option
Expand All @@ -78,17 +80,6 @@ public function testGetOptionReturnsDefault($option, $value)
$this->assertEquals($value, $connection->{'get' . ucfirst($option)}()); $this->assertEquals($value, $connection->{'get' . ucfirst($option)}());
} }


/**
* 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. * Tests that options can be set via the constructor.
* *
Expand Down
3 changes: 2 additions & 1 deletion Tests/Phergie/Driver/StreamsTest.php
Expand Up @@ -101,7 +101,8 @@ protected function getMockConnection()
'port' => 6667, 'port' => 6667,
'username' => 'username', 'username' => 'username',
'realname' => 'realname', 'realname' => 'realname',
'transport' => 'tcp' 'transport' => 'tcp',
'context' => array(),
); );
$connection = parent::getMockConnection(); $connection = parent::getMockConnection();
foreach ($options as $key => $value) { foreach ($options as $key => $value) {
Expand Down

0 comments on commit 2b9096e

Please sign in to comment.