mirror of
https://github.com/Seldaek/monolog.git
synced 2025-10-23 17:46:09 +02:00
Remove Socket, PeristentSocket and MockSocket classes
This commit is contained in:
@@ -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
|
||||
@@ -31,17 +31,21 @@ 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']);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,17 +53,172 @@ class SocketHandler extends AbstractProcessingHandler
|
||||
*/
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function write(array $record)
|
||||
private function connectIfNotConnected()
|
||||
{
|
||||
$this->socket->write((string) $record['formatted']);
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Allow mock
|
||||
*/
|
||||
protected function stream_get_meta_data()
|
||||
{
|
||||
return stream_get_meta_data($this->resource);
|
||||
}
|
||||
}
|
||||
|
@@ -1,65 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Monolog package.
|
||||
*
|
||||
* (c) Jordi Boggiano <j.boggiano@seld.be>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Monolog\Handler\SocketHandler;
|
||||
|
||||
use Monolog\Handler\SocketHandler\Exception\ConnectionException;
|
||||
use Monolog\Handler\SocketHandler\Exception\WriteToSocketException;
|
||||
|
||||
class MockSocket extends Socket
|
||||
{
|
||||
private $connectTimeoutMock = 0;
|
||||
private $timeoutMock = 0;
|
||||
|
||||
|
||||
public function __construct($connectionString)
|
||||
{
|
||||
if (is_resource($connectionString)) {
|
||||
$this->resource = $connectionString;
|
||||
} else {
|
||||
$this->connectionString = $connectionString;
|
||||
}
|
||||
}
|
||||
|
||||
public function setFailConnectionTimeout($seconds)
|
||||
{
|
||||
$this->connectTimeoutMock = (int)$seconds;
|
||||
}
|
||||
|
||||
public function setFailTimeout($seconds)
|
||||
{
|
||||
$this->timeoutMock = (int)$seconds;
|
||||
}
|
||||
|
||||
protected function createSocketResource()
|
||||
{
|
||||
if ($this->connectTimeoutMock > 0) {
|
||||
throw new ConnectionException("Mocked connection timeout");
|
||||
}
|
||||
$this->resource = fopen('php://memory', '+a');
|
||||
}
|
||||
|
||||
protected function writeToSocket($data) {
|
||||
if ($this->timeoutMock > 0) {
|
||||
throw new WriteToSocketException("Mocked write timeout");
|
||||
}
|
||||
return parent::writeToSocket($data);
|
||||
}
|
||||
|
||||
protected function setSocketTimeout()
|
||||
{
|
||||
// php://memory does not support this
|
||||
}
|
||||
|
||||
public function getResource() {
|
||||
return $this->resource;
|
||||
}
|
||||
}
|
@@ -1,32 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Monolog package.
|
||||
*
|
||||
* (c) Jordi Boggiano <j.boggiano@seld.be>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Monolog\Handler\SocketHandler;
|
||||
|
||||
use Monolog\Handler\SocketHandler\Exception\ConnectionException;
|
||||
|
||||
/**
|
||||
* Same as Socket but uses pfsockopen() instead allowing the connection to be reused in other requests.
|
||||
*
|
||||
* @see http://php.net/manual/en/function.pfsockopen.php
|
||||
* @author Pablo de Leon Belloc <pablolb@gmail.com>
|
||||
*/
|
||||
class PersistentSocket extends Socket
|
||||
{
|
||||
protected function createSocketResource()
|
||||
{
|
||||
@$resource = pfsockopen($this->connectionString, -1, $errno, $errstr, $this->connectionTimeout);
|
||||
if (!$resource) {
|
||||
throw new ConnectionException("Failed connecting to $this->connectionString ($errno: $errstr)");
|
||||
}
|
||||
$this->resource = $resource;
|
||||
}
|
||||
}
|
@@ -1,185 +0,0 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Monolog package.
|
||||
*
|
||||
* (c) Jordi Boggiano <j.boggiano@seld.be>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Monolog\Handler\SocketHandler;
|
||||
|
||||
use Monolog\Handler\SocketHandler\Exception\ConnectionException;
|
||||
use Monolog\Handler\SocketHandler\Exception\WriteToSocketException;
|
||||
|
||||
/**
|
||||
* Small class which writes to a socket.
|
||||
* Timeout settings must be set before first write to have any effect.
|
||||
*
|
||||
* @see http://php.net/manual/en/function.fsockopen.php
|
||||
* @author Pablo de Leon Belloc <pablolb@gmail.com>
|
||||
*/
|
||||
class Socket
|
||||
{
|
||||
protected $connectionString;
|
||||
protected $connectionTimeout;
|
||||
protected $resource;
|
||||
private $timeout = 0;
|
||||
|
||||
/**
|
||||
* @param string $connectionString As interpreted by fsockopen()
|
||||
*/
|
||||
public function __construct($connectionString)
|
||||
{
|
||||
$this->connectionString = $connectionString;
|
||||
$this->connectionTimeout = (float)ini_get('default_socket_timeout');
|
||||
}
|
||||
|
||||
public function getConnectionString()
|
||||
{
|
||||
return $this->connectionString;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 getConnectionTimeout() {
|
||||
return $this->connectionTimeout;
|
||||
}
|
||||
|
||||
public function getTimeout() {
|
||||
return $this->timeout;
|
||||
}
|
||||
|
||||
public function close()
|
||||
{
|
||||
if (is_resource($this->resource)) {
|
||||
fclose($this->resource);
|
||||
$this->resource = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect (if necessary) and write to the socket
|
||||
*
|
||||
* @throws Monolog\Handler\SocketHandler\Exception\ConnectionException
|
||||
* @throws Monolog\Handler\SocketHandler\Exception\WriteToSocketException
|
||||
* @param string $string
|
||||
*/
|
||||
public function write($string)
|
||||
{
|
||||
$this->connectIfNotConnected();
|
||||
$this->writeToSocket($string);
|
||||
}
|
||||
|
||||
protected 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.
|
||||
}
|
||||
|
||||
protected function connect()
|
||||
{
|
||||
$this->createSocketResource();
|
||||
$this->setSocketTimeout();
|
||||
}
|
||||
|
||||
protected function createSocketResource()
|
||||
{
|
||||
@$resource = fsockopen($this->connectionString, -1, $errno, $errstr, $this->connectionTimeout);
|
||||
if (!$resource) {
|
||||
throw new ConnectionException("Failed connecting to $this->connectionString ($errno: $errstr)");
|
||||
}
|
||||
$this->resource = $resource;
|
||||
}
|
||||
|
||||
protected 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 = fwrite($this->resource, substr($data, $sent));
|
||||
if ($chunk === false) {
|
||||
throw new WriteToSocketException("Could not write to socket");
|
||||
}
|
||||
$sent += $chunk;
|
||||
$socketInfo = stream_get_meta_data($this->resource);
|
||||
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)");
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user