]> _ Git - cubist_locale.git/commitdiff
wait #4209
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 1 Sep 2022 16:15:28 +0000 (18:15 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 1 Sep 2022 16:15:28 +0000 (18:15 +0200)
composer.json
src/Translate.php

index 4e446823d11bd7e13e4311509f1ff956a4722160..154343d21a44d0c8de91c7e223a6971e43d234ce 100644 (file)
@@ -20,6 +20,8 @@
         "php": ">=7.1.3",
         "umpirsky/locale-list": "^1.0",
         "umpirsky/country-list": "^2.0",
+        "laravel/framework": "~5.8|^6.0|^7.0|^8.0",
+        "cubist/util": "dev-master",
         "barryvdh/laravel-debugbar": "*"
     },
     "extra": {
index d24ed51cbe47ceb2e0c6c8a4a9261672566abf15..725a7aa7769c9fe95fe8e2b6f6c5e939eaa6074d 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace Cubist\Locale;
 
+use Cubist\Util\Files\Files;
 use \RecursiveIteratorIterator;
 use \RecursiveDirectoryIterator;
 use \SplFileInfo;
@@ -11,6 +12,7 @@ class Translate
 
     protected $_paths = [];
     protected $_extensions = ['php', 'phtml'];
+    protected $_exclude = ['^\.git'];
     protected $_toTranslate = [];
     protected static $_parsed = [];
 
@@ -53,51 +55,49 @@ class Translate
             if (isset(self::$_parsed[$path])) {
                 continue;
             }
-            start_measure('Parse path for translations ' . $path);
-            $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
-            foreach ($iterator as $p) {
-                if ($p->isFile()) {
-                    $this->_parseFile($p, $sectionLabel);
-                }
+            if (!isset($this->_toTranslate[$sectionLabel])) {
+                $this->_toTranslate[$sectionLabel] = [];
             }
+
+            start_measure('Get translate hash ' . $path);
+            $cacheKey = 'translate_path_' . sha1($path . '//' . Files::dirmtime($path, true, $this->_extensions));
+            stop_measure('Get translate hash ' . $path);
+            $this->_toTranslate[$sectionLabel] = array_merge($this->_toTranslate[$sectionLabel], cache()->remember($cacheKey, 3600, function () use ($path) {
+                start_measure('Parse path for translations ' . $path);
+                $res = [];
+                $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
+                foreach ($iterator as $p) {
+                    if ($p->isFile() && in_array($p->getExtension(), $this->_extensions)) {
+                        $res = array_merge($res, self::extractTextsToTranslate($p));
+                    }
+                }
+                stop_measure('Parse path for translations ' . $path);
+                return $res;
+            }));
             self::$_parsed[$path] = true;
-            stop_measure('Parse path for translations ' . $path);
         }
     }
 
-    protected function _parseFile(SplFileInfo $file, $sectionLabel)
-    {
-        if (!in_array($file->getExtension(), $this->_extensions)) {
-            return;
-        }
-        if (!isset($this->_toTranslate[$sectionLabel])) {
-            $this->_toTranslate[$sectionLabel] = [];
-        }
-        $this->_toTranslate[$sectionLabel] = array_merge($this->_toTranslate[$sectionLabel], self::extractTextsToTranslate($file));
-    }
 
     public static function extractTextsToTranslate(SplFileInfo $file)
     {
-        $toTranslate = [];
-
         if (!$file->isFile() || !$file->isReadable()) {
-            $toTranslate = [];
-        } else {
-            $c = file_get_contents($file);
+            return [];
+        }
 
-            // @link https://regex101.com/r/uD0eT1/4
-            if (preg_match_all('/__\(([\'\"]{1})(.*)(([^\\\\]{1})(\1))(.*)\)/Us', $c, $matches)) {
-                foreach ($matches[2] as $k => $m) {
-                    if ($m == '') {
-                        continue;
-                    }
-                    $m .= $matches[4][$k];
-                    $toTranslate[] = stripcslashes($m);
+        $c = file_get_contents($file);
+        $res = [];
+        // @link https://regex101.com/r/uD0eT1/4
+        if (preg_match_all('/__\(([\'\"]{1})(.*)(([^\\\\]{1})(\1))(.*)\)/Us', $c, $matches)) {
+            foreach ($matches[2] as $k => $m) {
+                if ($m == '') {
+                    continue;
                 }
+                $m .= $matches[4][$k];
+                $res[] = stripcslashes($m);
             }
         }
-
-        return $toTranslate;
+        return $res;
     }
 
     public function hasSomethingToTranslate()