Forráskód Böngészése

use mb_* when needed

vershinin_so 6 éve
szülő
commit
f8de7cf628

+ 15 - 5
src/Monolog/Formatter/FlowdockFormatter.php

@@ -11,8 +11,6 @@
 
 
 namespace Monolog\Formatter;
 namespace Monolog\Formatter;
 
 
-use Monolog\Utils;
-
 /**
 /**
  * formats the record to be used in the FlowdockHandler
  * formats the record to be used in the FlowdockHandler
  *
  *
@@ -84,12 +82,24 @@ class FlowdockFormatter implements FormatterInterface
         return $formatted;
         return $formatted;
     }
     }
 
 
-    public function getShortMessage(string $message): string
+    public function getShortMessage($message)
     {
     {
+        static $hasMbString;
+
+        if (null === $hasMbString) {
+            $hasMbString = function_exists('mb_strlen');
+        }
+
         $maxLength = 45;
         $maxLength = 45;
 
 
-        if (Utils::strlen($message) > $maxLength) {
-            $message = Utils::substr($message, 0, $maxLength - 4) . ' ...';
+        if ($hasMbString) {
+            if (mb_strlen($message, 'UTF-8') > $maxLength) {
+                $message = mb_substr($message, 0, $maxLength - 4, 'UTF-8') . ' ...';
+            }
+        } else {
+            if (strlen($message) > $maxLength) {
+                $message = substr($message, 0, $maxLength - 4) . ' ...';
+            }
         }
         }
 
 
         return $message;
         return $message;

+ 3 - 3
src/Monolog/Formatter/GelfMessageFormatter.php

@@ -94,7 +94,7 @@ class GelfMessageFormatter extends NormalizerFormatter
             ->setLevel($this->logLevels[$record['level']]);
             ->setLevel($this->logLevels[$record['level']]);
 
 
         // message length + system name length + 200 for padding / metadata
         // message length + system name length + 200 for padding / metadata
-        $len = 200 + Utils::strlen((string) $record['message']) + Utils::strlen($this->systemName);
+        $len = 200 + strlen((string) $record['message']) + strlen($this->systemName);
 
 
         if ($len > $this->maxLength) {
         if ($len > $this->maxLength) {
             $message->setShortMessage(Utils::substr($record['message'], 0, $this->maxLength));
             $message->setShortMessage(Utils::substr($record['message'], 0, $this->maxLength));
@@ -114,7 +114,7 @@ class GelfMessageFormatter extends NormalizerFormatter
 
 
         foreach ($record['extra'] as $key => $val) {
         foreach ($record['extra'] as $key => $val) {
             $val = is_scalar($val) || null === $val ? $val : $this->toJson($val);
             $val = is_scalar($val) || null === $val ? $val : $this->toJson($val);
-            $len = Utils::strlen($this->extraPrefix . $key . $val);
+            $len = strlen($this->extraPrefix . $key . $val);
             if ($len > $this->maxLength) {
             if ($len > $this->maxLength) {
                 $message->setAdditional($this->extraPrefix . $key, Utils::substr($val, 0, $this->maxLength));
                 $message->setAdditional($this->extraPrefix . $key, Utils::substr($val, 0, $this->maxLength));
 
 
@@ -125,7 +125,7 @@ class GelfMessageFormatter extends NormalizerFormatter
 
 
         foreach ($record['context'] as $key => $val) {
         foreach ($record['context'] as $key => $val) {
             $val = is_scalar($val) || null === $val ? $val : $this->toJson($val);
             $val = is_scalar($val) || null === $val ? $val : $this->toJson($val);
-            $len = Utils::strlen($this->contextPrefix . $key . $val);
+            $len = strlen($this->contextPrefix . $key . $val);
             if ($len > $this->maxLength) {
             if ($len > $this->maxLength) {
                 $message->setAdditional($this->contextPrefix . $key, Utils::substr($val, 0, $this->maxLength));
                 $message->setAdditional($this->contextPrefix . $key, Utils::substr($val, 0, $this->maxLength));
 
 

+ 1 - 2
src/Monolog/Formatter/WildfireFormatter.php

@@ -12,7 +12,6 @@
 namespace Monolog\Formatter;
 namespace Monolog\Formatter;
 
 
 use Monolog\Logger;
 use Monolog\Logger;
-use Monolog\Utils;
 
 
 /**
 /**
  * Serializes a log message according to Wildfire's header requirements
  * Serializes a log message according to Wildfire's header requirements
@@ -91,7 +90,7 @@ class WildfireFormatter extends NormalizerFormatter
         // The message itself is a serialization of the above JSON object + it's length
         // The message itself is a serialization of the above JSON object + it's length
         return sprintf(
         return sprintf(
             '%d|%s|',
             '%d|%s|',
-            Utils::strlen($json),
+            strlen($json),
             $json
             $json
         );
         );
     }
     }

+ 1 - 1
src/Monolog/Handler/BrowserConsoleHandler.php

@@ -176,7 +176,7 @@ class BrowserConsoleHandler extends AbstractProcessingHandler
             $args[] = '"font-weight: normal"';
             $args[] = '"font-weight: normal"';
 
 
             $pos = $match[0][1];
             $pos = $match[0][1];
-            $format = Utils::substr($format, 0, $pos) . '%c' . $match[1][0] . '%c' . Utils::substr($format, $pos + Utils::strlen($match[0][0]));
+            $format = Utils::substr($format, 0, $pos) . '%c' . $match[1][0] . '%c' . Utils::substr($format, $pos + strlen($match[0][0]));
         }
         }
 
 
         array_unshift($args, static::quote($format));
         array_unshift($args, static::quote($format));

+ 1 - 2
src/Monolog/Handler/ChromePHPHandler.php

@@ -14,7 +14,6 @@ namespace Monolog\Handler;
 use Monolog\Formatter\ChromePHPFormatter;
 use Monolog\Formatter\ChromePHPFormatter;
 use Monolog\Formatter\FormatterInterface;
 use Monolog\Formatter\FormatterInterface;
 use Monolog\Logger;
 use Monolog\Logger;
-use Monolog\Utils;
 
 
 /**
 /**
  * Handler sending logs to the ChromePHP extension (http://www.chromephp.com/)
  * Handler sending logs to the ChromePHP extension (http://www.chromephp.com/)
@@ -147,7 +146,7 @@ class ChromePHPHandler extends AbstractProcessingHandler
 
 
         $json = @json_encode(self::$json);
         $json = @json_encode(self::$json);
         $data = base64_encode(utf8_encode($json));
         $data = base64_encode(utf8_encode($json));
-        if (Utils::strlen($data) > 240 * 1024) {
+        if (strlen($data) > 240 * 1024) {
             self::$overflowed = true;
             self::$overflowed = true;
 
 
             $record = [
             $record = [

+ 2 - 3
src/Monolog/Handler/CubeHandler.php

@@ -12,7 +12,6 @@
 namespace Monolog\Handler;
 namespace Monolog\Handler;
 
 
 use Monolog\Logger;
 use Monolog\Logger;
-use Monolog\Utils;
 
 
 /**
 /**
  * Logs to Cube.
  * Logs to Cube.
@@ -135,7 +134,7 @@ class CubeHandler extends AbstractProcessingHandler
             $this->connectUdp();
             $this->connectUdp();
         }
         }
 
 
-        socket_send($this->udpConnection, $data, Utils::strlen($data), 0);
+        socket_send($this->udpConnection, $data, strlen($data), 0);
     }
     }
 
 
     private function writeHttp(string $data): void
     private function writeHttp(string $data): void
@@ -147,7 +146,7 @@ class CubeHandler extends AbstractProcessingHandler
         curl_setopt($this->httpConnection, CURLOPT_POSTFIELDS, '['.$data.']');
         curl_setopt($this->httpConnection, CURLOPT_POSTFIELDS, '['.$data.']');
         curl_setopt($this->httpConnection, CURLOPT_HTTPHEADER, [
         curl_setopt($this->httpConnection, CURLOPT_HTTPHEADER, [
             'Content-Type: application/json',
             'Content-Type: application/json',
-            'Content-Length: ' . Utils::strlen('['.$data.']'),
+            'Content-Length: ' . strlen('['.$data.']'),
         ]);
         ]);
 
 
         Curl\Util::execute($this->httpConnection, 5, false);
         Curl\Util::execute($this->httpConnection, 5, false);

+ 2 - 3
src/Monolog/Handler/DeduplicationHandler.php

@@ -12,7 +12,6 @@
 namespace Monolog\Handler;
 namespace Monolog\Handler;
 
 
 use Monolog\Logger;
 use Monolog\Logger;
-use Monolog\Utils;
 
 
 /**
 /**
  * Simple handler wrapper that deduplicates log records across multiple requests
  * Simple handler wrapper that deduplicates log records across multiple requests
@@ -67,7 +66,7 @@ class DeduplicationHandler extends BufferHandler
     {
     {
         parent::__construct($handler, 0, Logger::DEBUG, $bubble, false);
         parent::__construct($handler, 0, Logger::DEBUG, $bubble, false);
 
 
-        $this->deduplicationStore = $deduplicationStore === null ? sys_get_temp_dir() . '/monolog-dedup-' . Utils::substr(md5(__FILE__), 0, 20) .'.log' : $deduplicationStore;
+        $this->deduplicationStore = $deduplicationStore === null ? sys_get_temp_dir() . '/monolog-dedup-' . substr(md5(__FILE__), 0, 20) .'.log' : $deduplicationStore;
         $this->deduplicationLevel = Logger::toMonologLevel($deduplicationLevel);
         $this->deduplicationLevel = Logger::toMonologLevel($deduplicationLevel);
         $this->time = $time;
         $this->time = $time;
     }
     }
@@ -150,7 +149,7 @@ class DeduplicationHandler extends BufferHandler
 
 
         while (!feof($handle)) {
         while (!feof($handle)) {
             $log = fgets($handle);
             $log = fgets($handle);
-            if ($log && Utils::substr($log, 0, 10) >= $timestampValidity) {
+            if ($log && substr($log, 0, 10) >= $timestampValidity) {
                 $validLogs[] = $log;
                 $validLogs[] = $log;
             }
             }
         }
         }

+ 1 - 2
src/Monolog/Handler/FleepHookHandler.php

@@ -14,7 +14,6 @@ namespace Monolog\Handler;
 use Monolog\Formatter\FormatterInterface;
 use Monolog\Formatter\FormatterInterface;
 use Monolog\Formatter\LineFormatter;
 use Monolog\Formatter\LineFormatter;
 use Monolog\Logger;
 use Monolog\Logger;
-use Monolog\Utils;
 
 
 /**
 /**
  * Sends logs to Fleep.io using Webhook integrations
  * Sends logs to Fleep.io using Webhook integrations
@@ -97,7 +96,7 @@ class FleepHookHandler extends SocketHandler
         $header = "POST " . static::FLEEP_HOOK_URI . $this->token . " HTTP/1.1\r\n";
         $header = "POST " . static::FLEEP_HOOK_URI . $this->token . " HTTP/1.1\r\n";
         $header .= "Host: " . static::FLEEP_HOST . "\r\n";
         $header .= "Host: " . static::FLEEP_HOST . "\r\n";
         $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
         $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
-        $header .= "Content-Length: " . Utils::strlen($content) . "\r\n";
+        $header .= "Content-Length: " . strlen($content) . "\r\n";
         $header .= "\r\n";
         $header .= "\r\n";
 
 
         return $header;
         return $header;

+ 1 - 2
src/Monolog/Handler/FlowdockHandler.php

@@ -14,7 +14,6 @@ namespace Monolog\Handler;
 use Monolog\Logger;
 use Monolog\Logger;
 use Monolog\Formatter\FlowdockFormatter;
 use Monolog\Formatter\FlowdockFormatter;
 use Monolog\Formatter\FormatterInterface;
 use Monolog\Formatter\FormatterInterface;
-use Monolog\Utils;
 
 
 /**
 /**
  * Sends notifications through the Flowdock push API
  * Sends notifications through the Flowdock push API
@@ -108,7 +107,7 @@ class FlowdockHandler extends SocketHandler
         $header = "POST /v1/messages/team_inbox/" . $this->apiToken . " HTTP/1.1\r\n";
         $header = "POST /v1/messages/team_inbox/" . $this->apiToken . " HTTP/1.1\r\n";
         $header .= "Host: api.flowdock.com\r\n";
         $header .= "Host: api.flowdock.com\r\n";
         $header .= "Content-Type: application/json\r\n";
         $header .= "Content-Type: application/json\r\n";
-        $header .= "Content-Length: " . Utils::strlen($content) . "\r\n";
+        $header .= "Content-Length: " . strlen($content) . "\r\n";
         $header .= "\r\n";
         $header .= "\r\n";
 
 
         return $header;
         return $header;

+ 1 - 2
src/Monolog/Handler/MailHandler.php

@@ -13,7 +13,6 @@ namespace Monolog\Handler;
 
 
 use Monolog\Formatter\FormatterInterface;
 use Monolog\Formatter\FormatterInterface;
 use Monolog\Formatter\HtmlFormatter;
 use Monolog\Formatter\HtmlFormatter;
-use Monolog\Utils;
 
 
 /**
 /**
  * Base class for all mail handlers
  * Base class for all mail handlers
@@ -71,7 +70,7 @@ abstract class MailHandler extends AbstractProcessingHandler
 
 
     protected function isHtmlBody(string $body): bool
     protected function isHtmlBody(string $body): bool
     {
     {
-        return Utils::substr($body, 0, 1) === '<';
+        return substr($body, 0, 1) === '<';
     }
     }
 
 
     /**
     /**

+ 2 - 2
src/Monolog/Handler/PushoverHandler.php

@@ -116,7 +116,7 @@ class PushoverHandler extends SocketHandler
     private function buildContent(array $record): string
     private function buildContent(array $record): string
     {
     {
         // Pushover has a limit of 512 characters on title and message combined.
         // Pushover has a limit of 512 characters on title and message combined.
-        $maxMessageLength = 512 - Utils::strlen($this->title);
+        $maxMessageLength = 512 - strlen($this->title);
 
 
         $message = ($this->useFormattedMessage) ? $record['formatted'] : $record['message'];
         $message = ($this->useFormattedMessage) ? $record['formatted'] : $record['message'];
         $message = Utils::substr($message, 0, $maxMessageLength);
         $message = Utils::substr($message, 0, $maxMessageLength);
@@ -159,7 +159,7 @@ class PushoverHandler extends SocketHandler
         $header = "POST /1/messages.json HTTP/1.1\r\n";
         $header = "POST /1/messages.json HTTP/1.1\r\n";
         $header .= "Host: api.pushover.net\r\n";
         $header .= "Host: api.pushover.net\r\n";
         $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
         $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
-        $header .= "Content-Length: " . Utils::strlen($content) . "\r\n";
+        $header .= "Content-Length: " . strlen($content) . "\r\n";
         $header .= "\r\n";
         $header .= "\r\n";
 
 
         return $header;
         return $header;

+ 1 - 2
src/Monolog/Handler/Slack/SlackRecord.php

@@ -14,7 +14,6 @@ namespace Monolog\Handler\Slack;
 use Monolog\Logger;
 use Monolog\Logger;
 use Monolog\Formatter\NormalizerFormatter;
 use Monolog\Formatter\NormalizerFormatter;
 use Monolog\Formatter\FormatterInterface;
 use Monolog\Formatter\FormatterInterface;
-use Monolog\Utils;
 
 
 /**
 /**
  * Slack record utility helping to log to Slack webhooks or API.
  * Slack record utility helping to log to Slack webhooks or API.
@@ -308,7 +307,7 @@ class SlackRecord
     private function generateAttachmentField(string $title, $value): array
     private function generateAttachmentField(string $title, $value): array
     {
     {
         $value = is_array($value)
         $value = is_array($value)
-            ? sprintf('```%s```', Utils::substr($this->stringify($value), 0, 1990))
+            ? sprintf('```%s```', substr($this->stringify($value), 0, 1990))
             : $value;
             : $value;
 
 
         return array(
         return array(

+ 1 - 2
src/Monolog/Handler/SlackHandler.php

@@ -14,7 +14,6 @@ namespace Monolog\Handler;
 use Monolog\Formatter\FormatterInterface;
 use Monolog\Formatter\FormatterInterface;
 use Monolog\Logger;
 use Monolog\Logger;
 use Monolog\Handler\Slack\SlackRecord;
 use Monolog\Handler\Slack\SlackRecord;
-use Monolog\Utils;
 
 
 /**
 /**
  * Sends notifications through Slack API
  * Sends notifications through Slack API
@@ -130,7 +129,7 @@ class SlackHandler extends SocketHandler
         $header = "POST /api/chat.postMessage HTTP/1.1\r\n";
         $header = "POST /api/chat.postMessage HTTP/1.1\r\n";
         $header .= "Host: slack.com\r\n";
         $header .= "Host: slack.com\r\n";
         $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
         $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
-        $header .= "Content-Length: " . Utils::strlen($content) . "\r\n";
+        $header .= "Content-Length: " . strlen($content) . "\r\n";
         $header .= "\r\n";
         $header .= "\r\n";
 
 
         return $header;
         return $header;

+ 1 - 2
src/Monolog/Handler/SocketHandler.php

@@ -12,7 +12,6 @@
 namespace Monolog\Handler;
 namespace Monolog\Handler;
 
 
 use Monolog\Logger;
 use Monolog\Logger;
-use Monolog\Utils;
 
 
 /**
 /**
  * Stores to any socket - uses fsockopen() or pfsockopen().
  * Stores to any socket - uses fsockopen() or pfsockopen().
@@ -326,7 +325,7 @@ class SocketHandler extends AbstractProcessingHandler
 
 
     private function writeToSocket(string $data): void
     private function writeToSocket(string $data): void
     {
     {
-        $length = Utils::strlen($data);
+        $length = strlen($data);
         $sent = 0;
         $sent = 0;
         $this->lastSentBytes = $sent;
         $this->lastSentBytes = $sent;
         while ($this->isConnected() && $sent < $length) {
         while ($this->isConnected() && $sent < $length) {

+ 1 - 1
src/Monolog/Handler/SqsHandler.php

@@ -52,7 +52,7 @@ class SqsHandler extends AbstractProcessingHandler
         }
         }
 
 
         $messageBody = $record['formatted'];
         $messageBody = $record['formatted'];
-        if (Utils::strlen($messageBody) >= static::MAX_MESSAGE_SIZE) {
+        if (strlen($messageBody) >= static::MAX_MESSAGE_SIZE) {
             $messageBody = Utils::substr($messageBody, 0, static::HEAD_MESSAGE_SIZE);
             $messageBody = Utils::substr($messageBody, 0, static::HEAD_MESSAGE_SIZE);
         }
         }
 
 

+ 2 - 3
src/Monolog/Handler/StreamHandler.php

@@ -12,7 +12,6 @@
 namespace Monolog\Handler;
 namespace Monolog\Handler;
 
 
 use Monolog\Logger;
 use Monolog\Logger;
-use Monolog\Utils;
 
 
 /**
 /**
  * Stores to any stream resource
  * Stores to any stream resource
@@ -147,8 +146,8 @@ class StreamHandler extends AbstractProcessingHandler
             return dirname($stream);
             return dirname($stream);
         }
         }
 
 
-        if ('file://' === Utils::substr($stream, 0, 7)) {
-            return dirname(Utils::substr($stream, 7));
+        if ('file://' === substr($stream, 0, 7)) {
+            return dirname(substr($stream, 7));
         }
         }
 
 
         return null;
         return null;

+ 2 - 2
src/Monolog/Handler/SyslogUdp/UdpSocket.php

@@ -49,12 +49,12 @@ class UdpSocket
         if (!is_resource($this->socket)) {
         if (!is_resource($this->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, Utils::strlen($chunk), $flags = 0, $this->ip, $this->port);
+        socket_sendto($this->socket, $chunk, strlen($chunk), $flags = 0, $this->ip, $this->port);
     }
     }
 
 
     protected function assembleMessage(string $line, string $header): string
     protected function assembleMessage(string $line, string $header): string
     {
     {
-        $chunkSize = static::DATAGRAM_MAX_LENGTH - Utils::strlen($header);
+        $chunkSize = static::DATAGRAM_MAX_LENGTH - strlen($header);
 
 
         return $header . Utils::substr($line, 0, $chunkSize);
         return $header . Utils::substr($line, 0, $chunkSize);
     }
     }

+ 2 - 3
src/Monolog/Processor/UidProcessor.php

@@ -12,7 +12,6 @@
 namespace Monolog\Processor;
 namespace Monolog\Processor;
 
 
 use Monolog\ResettableInterface;
 use Monolog\ResettableInterface;
-use Monolog\Utils;
 
 
 /**
 /**
  * Adds a unique identifier into records
  * Adds a unique identifier into records
@@ -46,11 +45,11 @@ class UidProcessor implements ProcessorInterface, ResettableInterface
 
 
     public function reset()
     public function reset()
     {
     {
-        $this->uid = $this->generateUid(Utils::strlen($this->uid));
+        $this->uid = $this->generateUid(strlen($this->uid));
     }
     }
 
 
     private function generateUid(int $length): string
     private function generateUid(int $length): string
     {
     {
-        return Utils::substr(bin2hex(random_bytes((int) ceil($length / 2))), 0, $length);
+        return substr(bin2hex(random_bytes((int) ceil($length / 2))), 0, $length);
     }
     }
 }
 }

+ 2 - 22
src/Monolog/Utils.php

@@ -23,30 +23,10 @@ final class Utils
         return 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).'@anonymous' : $class;
         return 'c' === $class[0] && 0 === strpos($class, "class@anonymous\0") ? get_parent_class($class).'@anonymous' : $class;
     }
     }
 
 
-    private static function hasMbString(): bool
-    {
-        static $hasMbString;
-
-        if (null === $hasMbString) {
-            $hasMbString = extension_loaded('mbstring');
-        }
-
-        return $hasMbString;
-    }
-
-    public static function strlen(string $string, ?string $encoding = null): int
-    {
-        if (self::hasMbString()) {
-            return $encoding ? mb_strlen($string, $encoding) : mb_strlen($string);
-        }
-
-        return strlen($string);
-    }
-
     public static function substr(string $string, int $start, ?int $length = null)
     public static function substr(string $string, int $start, ?int $length = null)
     {
     {
-        if (self::hasMbString()) {
-            return mb_substr($string, $start, $length);
+        if (extension_loaded('mbstring')) {
+            return mb_strcut($string, $start, $length);
         }
         }
 
 
         return substr($string, $start, $length);
         return substr($string, $start, $length);