]> _ Git - cubist_locale.git/commitdiff
wip #5851
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 11 Apr 2023 15:58:45 +0000 (17:58 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 11 Apr 2023 15:58:45 +0000 (17:58 +0200)
composer.json
src/Translate.php

index ee50a122ac3e7ff2227813a9e9d4c3bcf93b5729..13040636f07db6de6bc21c68383514349dd31095 100644 (file)
@@ -20,7 +20,7 @@
         "php": ">=7.1.3",
         "umpirsky/locale-list": "^1.0",
         "umpirsky/country-list": "^2.0",
-        "laravel/framework": "~5.8|^6.0|^7.0|^8.0|^9.0|^10.0",
+        "laravel/framework": "^8.0|^9.0|^10.0",
         "cubist/util": "dev-master",
         "barryvdh/laravel-debugbar": "*"
     },
index 725a7aa7769c9fe95fe8e2b6f6c5e939eaa6074d..7faa156db85157a113caca40685bfff944ef6f8c 100644 (file)
@@ -3,12 +3,12 @@
 namespace Cubist\Locale;
 
 use Cubist\Util\Files\Files;
+use Illuminate\Support\Facades\Cache;
 use \RecursiveIteratorIterator;
 use \RecursiveDirectoryIterator;
 use \SplFileInfo;
 
-class Translate
-{
+class Translate {
 
     protected $_paths = [];
     protected $_extensions = ['php', 'phtml'];
@@ -16,13 +16,11 @@ class Translate
     protected $_toTranslate = [];
     protected static $_parsed = [];
 
-    public function __construct()
-    {
+    public function __construct() {
 
     }
 
-    public function addPath($path, $sectionLabel = null)
-    {
+    public function addPath($path, $sectionLabel = null) {
         if (null === $sectionLabel) {
             $this->_paths[] = $path;
         } else {
@@ -30,78 +28,78 @@ class Translate
         }
     }
 
-    public function clearPaths()
-    {
+    public function clearPaths() {
         $this->_paths = [];
         $this->_toTranslate = [];
     }
 
-    public function setExtensions(array $extensions)
-    {
+    public function setExtensions(array $extensions) {
         $this->_extensions = $extensions;
     }
 
-    public function addExtension($extension)
-    {
+    public function addExtension($extension) {
         $this->_extensions[] = $extension;
     }
 
-    public function parseFiles()
-    {
+    public function parseFiles() {
         $this->_extensions = array_unique($this->_extensions);
         $this->_paths = array_unique($this->_paths);
 
         foreach ($this->_paths as $sectionLabel => $path) {
-            if (isset(self::$_parsed[$path])) {
-                continue;
-            }
+
             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));
+            if (isset(static::$_parsed[$path])) {
+                $found = static::$_parsed[$path];
+            } else {
+
+                start_measure('Get translate hash ' . $path);
+                $cacheKey = 'translate_path_' . sha1($path . '//' . Files::dirmtime($path, true, $this->_extensions));
+                stop_measure('Get translate hash ' . $path);
+                $found = 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, static::extractTextsToTranslate($p));
+                        }
                     }
-                }
-                stop_measure('Parse path for translations ' . $path);
-                return $res;
-            }));
-            self::$_parsed[$path] = true;
+                    stop_measure('Parse path for translations ' . $path);
+                    return $res;
+                });
+                static::$_parsed[$path] = $found;
+            }
+
+            $this->_toTranslate[$sectionLabel] = array_merge($this->_toTranslate[$sectionLabel], $found);
         }
     }
 
 
-    public static function extractTextsToTranslate(SplFileInfo $file)
-    {
+    public static function extractTextsToTranslate(SplFileInfo $file) {
         if (!$file->isFile() || !$file->isReadable()) {
             return [];
         }
-
-        $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;
+        $cacheKey = 'totranslate_' . Files::hashFileAttributes($file->getPathname());
+        return Cache::rememberForever($cacheKey, function () use ($file) {
+            $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);
                 }
-                $m .= $matches[4][$k];
-                $res[] = stripcslashes($m);
             }
-        }
-        return $res;
+            return $res;
+        });
     }
 
-    public function hasSomethingToTranslate()
-    {
+    public function hasSomethingToTranslate() {
         return count($this->_toTranslate) > 0;
     }
 
@@ -109,13 +107,11 @@ class Translate
      *
      * @return array
      */
-    public function getStringToTranslate()
-    {
+    public function getStringToTranslate() {
         return $this->_toTranslate;
     }
 
-    public static function saveTranslations($data, $path)
-    {
+    public static function saveTranslations($data, $path) {
         file_put_contents($path, '<?php return ' . var_export($data, true) . ';');
     }