]> _ Git - cubist_locale.git/commitdiff
wip #6386 @0.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 10 Oct 2023 15:13:57 +0000 (17:13 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 10 Oct 2023 15:13:57 +0000 (17:13 +0200)
src/Translate.php

index 7faa156db85157a113caca40685bfff944ef6f8c..df50dac5079c860811dcaf8c53ad8bbbd5736b83 100644 (file)
@@ -8,7 +8,8 @@ use \RecursiveIteratorIterator;
 use \RecursiveDirectoryIterator;
 use \SplFileInfo;
 
-class Translate {
+class Translate
+{
 
     protected $_paths = [];
     protected $_extensions = ['php', 'phtml'];
@@ -16,11 +17,13 @@ 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 {
@@ -28,56 +31,59 @@ 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($this->_toTranslate[$sectionLabel])) {
                 $this->_toTranslate[$sectionLabel] = [];
             }
-            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));
-                        }
+
+            start_measure('Get translate hash ' . $path);
+            $cacheKey = 'translate_path_' . sha1($path . '--' . print_r($this->_extensions, true) . '//' . 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;
-                });
-                static::$_parsed[$path] = $found;
-            }
+                }
+                stop_measure('Parse path for translations ' . $path);
+                return $res;
+            });
+
 
             $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 [];
         }
@@ -99,7 +105,8 @@ class Translate {
         });
     }
 
-    public function hasSomethingToTranslate() {
+    public function hasSomethingToTranslate()
+    {
         return count($this->_toTranslate) > 0;
     }
 
@@ -107,11 +114,13 @@ 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) . ';');
     }