Skip to content

Commit

Permalink
Remove Socket, PeristentSocket and MockSocket classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablo Belloc committed Feb 26, 2012
1 parent 423cfeb commit d752f43
Show file tree
Hide file tree
Showing 6 changed files with 407 additions and 450 deletions.
199 changes: 179 additions & 20 deletions src/Monolog/Handler/SocketHandler.php
Expand Up @@ -5,24 +5,24 @@

namespace Monolog\Handler;

use Monolog\Handler\SocketHandler\Socket;
use Monolog\Handler\SocketHandler\PersistentSocket;
use Monolog\Logger;
use Monolog\Handler\SocketHandler\Exception\ConnectionException;
use Monolog\Handler\SocketHandler\Exception\WriteToSocketException;


/**
* Stores to any socket - uses fsockopen() or pfsockopen().
*
* @see Monolog\Handler\SocketHandler\Socket
* @see Monolog\Handler\SocketHandler\PersistentSocket
* @see http://php.net/manual/en/function.fsockopen.php
*/
class SocketHandler extends AbstractProcessingHandler
{
/**
* @var Socket
*/
private $socket;

private $connectionString;
private $connectionTimeout;
private $resource;
private $timeout = 0;
private $persistent = false;

/**
* @param string $connectionString
* @param integer $level The minimum logging level at which this handler will be triggered
Expand All @@ -31,35 +31,194 @@ class SocketHandler extends AbstractProcessingHandler
public function __construct($connectionString, $level = Logger::DEBUG, $bubble = true)
{
parent::__construct($level, $bubble);
$this->socket = new Socket($connectionString);
$this->connectionString = $connectionString;
$this->connectionTimeout = (float)ini_get('default_socket_timeout');
}

/**
* Inject socket - allows you to configure timeouts.
* Connect (if necessary) and write to the socket
*
* @param Socket $socket
* @throws Monolog\Handler\SocketHandler\Exception\ConnectionException
* @throws Monolog\Handler\SocketHandler\Exception\WriteToSocketException
* @param string $string
*/
public function setSocket(Socket $socket)
public function write(array $record)
{
$this->socket = $socket;
$this->connectIfNotConnected();
$this->writeToSocket((string) $record['formatted']);
}

/**
* We will not close a PersistentSocket instance so it can be reused in other requests.
*/
public function close()
{
if ($this->socket instanceof PersistentSocket) {
if ($this->isPersistent()) {
return;
}
$this->socket->close();
$this->closeSocket();
}

public function closeSocket()
{
if (is_resource($this->resource)) {
fclose($this->resource);
$this->resource = null;
}
}

public function setPersistent($boolean)
{
$this->persistent = (boolean)$boolean;
}

/**
* Set connection timeout. Only has effect before we connect.
*
* @see http://php.net/manual/en/function.fsockopen.php
* @param integer $seconds
*/
public function setConnectionTimeout($seconds)
{
$this->validateTimeout($seconds);
$this->connectionTimeout = (float)$seconds;
}

/**
* Set write timeout. Only has effect before we connect.
*
* @see http://php.net/manual/en/function.stream-set-timeout.php
* @param type $seconds
*/
public function setTimeout($seconds)
{
$this->validateTimeout($seconds);
$this->timeout = (int)$seconds;
}

private function validateTimeout($value)
{
$ok = filter_var($value, FILTER_VALIDATE_INT, array('options' => array(
'min_range' => 0,
)));
if ($ok === false) {
throw new \InvalidArgumentException("Timeout must be 0 or a positive integer (got $value)");
}
}

public function getConnectionString()
{
return $this->connectionString;
}

public function isPersistent()
{
return $this->persistent;
}

public function getConnectionTimeout() {
return $this->connectionTimeout;
}

public function getTimeout() {
return $this->timeout;
}

/**
* Allow injecting a resource opened somewhere else. Used in tests.
*
* @throws \InvalidArgumentException
* @param resource $resource
*/
public function setResource($resource)
{
if (is_resource($resource)) {
$this->resource = $resource;
} else {
throw new \InvalidArgumentException("Expected a resource");
}
}

private function connectIfNotConnected()
{
if ($this->isConnected()) {
return;
}
$this->connect();
}

/**
* Check to see if the socket is currently available.
*
* UDP might appear to be connected but might fail when writing. See http://php.net/fsockopen for details.
*
* @return boolean
*/
public function isConnected()
{
return is_resource($this->resource)
&& !feof($this->resource); // on TCP - other party can close connection.
}

private function connect()
{
$this->createSocketResource();
$this->setSocketTimeout();
}

protected function createSocketResource()
{
if ($this->persistent) {
@$resource = pfsockopen($this->connectionString, -1, $errno, $errstr, $this->connectionTimeout);
} else {
@$resource = fsockopen($this->connectionString, -1, $errno, $errstr, $this->connectionTimeout);
}
if (!$resource) {
throw new ConnectionException("Failed connecting to $this->connectionString ($errno: $errstr)");
}
$this->resource = $resource;
}

private function setSocketTimeout()
{
if (!stream_set_timeout($this->resource, $this->timeout)) {
throw new ConnectionException("Failed setting timeout with stream_set_timeout()");
}
}

protected function writeToSocket($data)
{
$length = strlen($data);
$sent = 0;
while ($this->isConnected() && $sent < $length) {
$chunk = $this->fwrite(substr($data, $sent));
if ($chunk === false) {
throw new WriteToSocketException("Could not write to socket");
}
$sent += $chunk;
$socketInfo = $this->stream_get_meta_data();
if ($socketInfo['timed_out']) {
throw new WriteToSocketException("Write timed-out");
}
}
if (!$this->isConnected() && $sent < $length) {
throw new WriteToSocketException("End-of-file reached, probably we got disconnected (sent $sent of $length)");
}
}

/**
* Allow mock
*/
protected function fwrite($data)
{
return @fwrite($this->resource, $data);
}

/**
* {@inheritdoc}
* Allow mock
*/
protected function write(array $record)
protected function stream_get_meta_data()
{
$this->socket->write((string) $record['formatted']);
return stream_get_meta_data($this->resource);
}
}
65 changes: 0 additions & 65 deletions src/Monolog/Handler/SocketHandler/MockSocket.php

This file was deleted.

32 changes: 0 additions & 32 deletions src/Monolog/Handler/SocketHandler/PersistentSocket.php

This file was deleted.

0 comments on commit d752f43

Please sign in to comment.