1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-05 12:47:39 +02:00

Add support for PHP8 CurlHandler/Socket instead of resources

This commit is contained in:
Jordi Boggiano
2020-12-09 16:39:57 +01:00
parent c5853b9b0f
commit fd305da67b
3 changed files with 15 additions and 11 deletions

View File

@@ -11,6 +11,8 @@
namespace Monolog\Handler\Curl;
use CurlHandle;
/**
* This class is marked as internal and it is not under the BC promise of the package.
*
@@ -31,10 +33,10 @@ final class Util
/**
* Executes a CURL request with optional retries and exception on failure
*
* @param resource $ch curl handler
* @param int $retries
* @param bool $closeAfterDone
* @return bool|string @see curl_exec
* @param resource|CurlHandle $ch curl handler
* @param int $retries
* @param bool $closeAfterDone
* @return bool|string @see curl_exec
*/
public static function execute($ch, int $retries = 5, bool $closeAfterDone = true)
{

View File

@@ -15,6 +15,7 @@ use Monolog\Logger;
use Monolog\Formatter\FormatterInterface;
use Monolog\Formatter\LogglyFormatter;
use function array_key_exists;
use CurlHandle;
/**
* Sends errors to Loggly.
@@ -32,7 +33,7 @@ class LogglyHandler extends AbstractProcessingHandler
/**
* Caches the curl handlers for every given endpoint.
*
* @var array
* @var resource[]|CurlHandle[]
*/
protected $curlHandlers = [];
@@ -63,7 +64,7 @@ class LogglyHandler extends AbstractProcessingHandler
*
* @param string $endpoint
*
* @return resource
* @return resource|CurlHandle
*/
protected function getCurlHandler(string $endpoint)
{
@@ -79,7 +80,7 @@ class LogglyHandler extends AbstractProcessingHandler
*
* @param string $endpoint
*
* @return resource|\CurlHandle
* @return resource|CurlHandle
*/
private function loadCurlHandle(string $endpoint)
{

View File

@@ -12,6 +12,7 @@
namespace Monolog\Handler\SyslogUdp;
use Monolog\Utils;
use Socket;
class UdpSocket
{
@@ -21,7 +22,7 @@ class UdpSocket
protected $ip;
/** @var int */
protected $port;
/** @var resource|null */
/** @var resource|Socket|null */
protected $socket;
public function __construct(string $ip, int $port = 514)
@@ -35,7 +36,7 @@ class UdpSocket
$domain = AF_UNIX;
$protocol = IPPROTO_IP;
}
$this->socket = socket_create($domain, SOCK_DGRAM, $protocol);
$this->socket = socket_create($domain, SOCK_DGRAM, $protocol) ?: null;
}
public function write($line, $header = "")
@@ -45,7 +46,7 @@ class UdpSocket
public function close(): void
{
if (is_resource($this->socket)) {
if (is_resource($this->socket) || $this->socket instanceof Socket) {
socket_close($this->socket);
$this->socket = null;
}
@@ -53,7 +54,7 @@ class UdpSocket
protected function send(string $chunk): void
{
if (!is_resource($this->socket)) {
if (!is_resource($this->socket) && !$this->socket instanceof Socket) {
throw new \RuntimeException('The UdpSocket to '.$this->ip.':'.$this->port.' has been closed and can not be written to anymore');
}
socket_sendto($this->socket, $chunk, strlen($chunk), $flags = 0, $this->ip, $this->port);