Skip to content
This repository has been archived by the owner on Jan 10, 2022. It is now read-only.

Possibility to set context for SocketClient #31

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Connection/Socket.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function __construct(SocketClient $socket)
*/
public static function factory(Options $options)
{
$socket = new SocketClient($options->getAddress());
$socket = new SocketClient($options->getAddress(), $options->getContextOptions());
$object = new static($socket);
$object->setOptions($options);
return $object;
Expand Down
31 changes: 31 additions & 0 deletions src/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,15 @@ class Options
'plain' => '\\Fabiang\\Xmpp\\EventListener\\Stream\\Authentication\\Plain'
);


/**
* Options used to create a stream context
*
* @var array
*/
protected $contextOptions = array();


/**
* Constructor.
*
Expand Down Expand Up @@ -413,4 +422,26 @@ public function setTimeout($timeout)
$this->timeout = (int) $timeout;
return $this;
}

/**
* Get context options for connection
*
* @return array
*/
public function getContextOptions()
{
return $this->contextOptions;
}

/**
* Set context options for connection
*
* @param array $contextOptions
* @return \Fabiang\Xmpp\Options
*/
public function setContextOptions($contextOptions)
{
$this->contextOptions = (array) $contextOptions;
return $this;
}
}
27 changes: 19 additions & 8 deletions src/Stream/SocketClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,30 @@ class SocketClient
*/
protected $address;


/**
* Options used to create a stream context
* @see http://php.net/manual/en/function.stream-context-create.php
*
* @var array
*/
protected $options;

/**
* Constructor takes address as argument.
*
* @param string $address
*/
public function __construct($address)
public function __construct($address, $options = null)
{
$this->address = $address;
$this->options = $options;
}

/**
* Connect.
*
* @param integer $timeout Timeout for connection
* @param integer $timeout Timeout for connection
* @param boolean $persistent Persitent connection
* @return void
*/
Expand All @@ -91,7 +101,8 @@ public function connect($timeout = 30, $persistent = false)
// call stream_socket_client with custom error handler enabled
$handler = new ErrorHandler(
function ($address, $timeout, $flags) {
return stream_socket_client($address, $errno, $errstr, $timeout, $flags);
$context = (null != $this->options) ? stream_context_create($this->options) : null;
return stream_socket_client($address, $errno, $errstr, $timeout, $flags, $context);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be inside an if-statement like this:

    if (null === $context) {
        return stream_socket_client($address, $errno, $errstr, $timeout, $flags);
    }
    return stream_socket_client($address, $errno, $errstr, $timeout, $flags, $context);

},
$this->address,
$timeout,
Expand All @@ -106,9 +117,9 @@ function ($address, $timeout, $flags) {
/**
* Reconnect and optionally use different address.
*
* @param string $address
* @param string $address
* @param integer $timeout
* @param bool $persistent
* @param bool $persistent
*/
public function reconnect($address = null, $timeout = 30, $persistent = false)
{
Expand Down Expand Up @@ -139,7 +150,7 @@ public function close()
*/
public function setBlocking($flag = true)
{
stream_set_blocking($this->resource, (int) $flag);
stream_set_blocking($this->resource, (int)$flag);
return $this;
}

Expand All @@ -157,7 +168,7 @@ public function read($length = self::BUFFER_LENGTH)
/**
* Write to stream.
*
* @param string $string String
* @param string $string String
* @param integer $length Limit
* @return void
*/
Expand All @@ -173,7 +184,7 @@ public function write($string, $length = null)
/**
* Enable/disable cryptography on stream.
*
* @param boolean $enable Flag
* @param boolean $enable Flag
* @param integer $cryptoType One of the STREAM_CRYPTO_METHOD_* constants.
* @return void
* @throws InvalidArgumentException
Expand Down