From: Vincent Vanwaelscappel Date: Thu, 1 Sep 2022 16:15:28 +0000 (+0200) Subject: wait #4209 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=02a7de778c0c2919005d71bb01c423e69e7f6885;p=cubist_locale.git wait #4209 --- diff --git a/composer.json b/composer.json index 4e44682..154343d 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/src/Translate.php b/src/Translate.php index d24ed51..725a7aa 100644 --- a/src/Translate.php +++ b/src/Translate.php @@ -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()