Explorar o código

Add support for PHP8 CurlHandler/Socket instead of resources

Jordi Boggiano %!s(int64=5) %!d(string=hai) anos
pai
achega
fd305da67b

+ 6 - 4
src/Monolog/Handler/Curl/Util.php

@@ -11,6 +11,8 @@
 
 
 namespace Monolog\Handler\Curl;
 namespace Monolog\Handler\Curl;
 
 
+use CurlHandle;
+
 /**
 /**
  * This class is marked as internal and it is not under the BC promise of the package.
  * 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
      * 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)
     public static function execute($ch, int $retries = 5, bool $closeAfterDone = true)
     {
     {

+ 4 - 3
src/Monolog/Handler/LogglyHandler.php

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

+ 5 - 4
src/Monolog/Handler/SyslogUdp/UdpSocket.php

@@ -12,6 +12,7 @@
 namespace Monolog\Handler\SyslogUdp;
 namespace Monolog\Handler\SyslogUdp;
 
 
 use Monolog\Utils;
 use Monolog\Utils;
+use Socket;
 
 
 class UdpSocket
 class UdpSocket
 {
 {
@@ -21,7 +22,7 @@ class UdpSocket
     protected $ip;
     protected $ip;
     /** @var int */
     /** @var int */
     protected $port;
     protected $port;
-    /** @var resource|null */
+    /** @var resource|Socket|null */
     protected $socket;
     protected $socket;
 
 
     public function __construct(string $ip, int $port = 514)
     public function __construct(string $ip, int $port = 514)
@@ -35,7 +36,7 @@ class UdpSocket
           $domain = AF_UNIX;
           $domain = AF_UNIX;
           $protocol = IPPROTO_IP;
           $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 = "")
     public function write($line, $header = "")
@@ -45,7 +46,7 @@ class UdpSocket
 
 
     public function close(): void
     public function close(): void
     {
     {
-        if (is_resource($this->socket)) {
+        if (is_resource($this->socket) || $this->socket instanceof Socket) {
             socket_close($this->socket);
             socket_close($this->socket);
             $this->socket = null;
             $this->socket = null;
         }
         }
@@ -53,7 +54,7 @@ class UdpSocket
 
 
     protected function send(string $chunk): void
     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');
             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);
         socket_sendto($this->socket, $chunk, strlen($chunk), $flags = 0, $this->ip, $this->port);