Browse Source

Core\Files: Optimize the filterName() method

Visman 1 year ago
parent
commit
5a3ad9d33e
1 changed files with 13 additions and 4 deletions
  1. 13 4
      app/Core/Files.php

+ 13 - 4
app/Core/Files.php

@@ -15,6 +15,7 @@ use ForkBB\Core\File;
 use ForkBB\Core\Image;
 use ForkBB\Core\Image;
 use ForkBB\Core\Image\DefaultDriver;
 use ForkBB\Core\Image\DefaultDriver;
 use ForkBB\Core\Exceptions\FileException;
 use ForkBB\Core\Exceptions\FileException;
+use Transliterator;
 use InvalidArgumentException;
 use InvalidArgumentException;
 use RuntimeException;
 use RuntimeException;
 
 
@@ -55,6 +56,11 @@ class Files
      */
      */
     protected array $tmpFiles = [];
     protected array $tmpFiles = [];
 
 
+    /**
+     * Для кэширования транслитератора
+     */
+    protected Transliterator|false|null $transl = null;
+
     /**
     /**
      * Список mime типов считающихся картинками
      * Список mime типов считающихся картинками
      */
      */
@@ -971,10 +977,13 @@ class Files
      */
      */
     public function filterName(string $name): string
     public function filterName(string $name): string
     {
     {
-        $name = \transliterator_transliterate(
-            'Any-Latin; Latin-ASCII; Lower()', // "Any-Latin; NFD; [:Nonspacing Mark:] Remove; NFC; Lower();",
-            $name
-        );
+        if (null === $this->transl) {
+            $this->transl = Transliterator::create('Any-Latin;Latin-ASCII;Lower();') ?? false;
+        }
+
+        if ($this->transl instanceof Transliterator) {
+            $name = $this->transl->transliterate($name);
+        }
 
 
         $name = \trim(\preg_replace(['%[^\w]+%', '%_+%'], ['-', '_'], $name), '-_');
         $name = \trim(\preg_replace(['%[^\w]+%', '%_+%'], ['-', '_'], $name), '-_');