diff --git a/system/src/Grav/Common/Config/Config.php b/system/src/Grav/Common/Config/Config.php index f2c6abcbc..adea8c72c 100644 --- a/system/src/Grav/Common/Config/Config.php +++ b/system/src/Grav/Common/Config/Config.php @@ -60,6 +60,12 @@ class Config extends Data '' => ['user://themes'], ] ], + 'languages' => [ + 'type' => 'ReadOnlyStream', + 'prefixes' => [ + '' => ['user://languages', 'system://languages'], + ] + ], 'cache' => [ 'type' => 'Stream', 'prefixes' => [ @@ -85,12 +91,14 @@ class Config extends Data protected $blueprintFiles = []; protected $configFiles = []; + protected $languageFiles = []; protected $checksum; protected $timestamp; protected $configLookup; protected $blueprintLookup; protected $pluginLookup; + protected $languagesLookup; protected $finder; protected $environment; @@ -163,9 +171,11 @@ class Config extends Data $this->configLookup = $locator->findResources('config://'); $this->blueprintLookup = $locator->findResources('blueprints://config'); $this->pluginLookup = $locator->findResources('plugins://'); + $this->languagesLookup = $locator->findResources('languages://'); $this->loadCompiledBlueprints($this->blueprintLookup, $this->pluginLookup, 'master'); $this->loadCompiledConfig($this->configLookup, $this->pluginLookup, 'master'); + $this->loadCompiledLanguages($this->languagesLookup, $this->pluginLookup, 'master'); $this->initializeLocator($locator); } @@ -312,6 +322,58 @@ class Config extends Data $this->items = $cache['data']; } + protected function loadCompiledLanguages($languages, $plugins, $filename = null) + { + $filename = $filename + ? CACHE_DIR . 'compiled/languages/' . $filename . '-' . $this->environment . '.php' + : CACHE_DIR . 'compiled/languages/' . $checksum . '-' . $this->environment . '.php'; + $file = PhpFile::instance($filename); + $cache = $file->exists() ? $file->content() : null; + $class = get_class($this); + $checksum = $this->checksum(); + + if ( + !is_array($cache) + || !isset($cache['checksum']) + || !isset($cache['@class']) + || $cache['@class'] != $class + ) { + $this->messages[] = 'No cached languages, compiling new configuration..'; + } else if ($cache['checksum'] !== $checksum) { + $this->messages[] = 'Languages checksum mismatch, reloading languages..'; + } else { + $this->messages[] = 'Languages checksum matches, using cached version.'; + + $this->items = $cache['data']; + return; + } + + $languageFiles = $this->finder->locateLanguageFiles($languages, $plugins); + + // Attempt to lock the file for writing. + $file->lock(false); + + // Load languages. + foreach ($languageFiles as $files) { + $this->loadLanguagesFiles($files); + } + $cache = [ + '@class' => $class, + 'timestamp' => time(), + 'checksum' => $checksum, + 'data' => $this->toArray() + ]; + + // If compiled file wasn't already locked by another process, save it. + if ($file->locked() !== false) { + $this->messages[] = 'Saving compiled configuration.'; + $file->save($cache); + $file->unlock(); + } + + $this->items = $cache['data']; + } + /** * Load blueprints. * @@ -338,6 +400,14 @@ class Config extends Data } } + public function loadLanguagesFiles(array $files) + { + foreach ($files as $name => $item) { + $file = CompiledYamlFile::instance($item['file']); + $this->set($name, $file->content(), '/'); + } + } + /** * Initialize resource locator by using the configuration. * diff --git a/system/src/Grav/Common/Config/ConfigFinder.php b/system/src/Grav/Common/Config/ConfigFinder.php index 243ddc5aa..f13a339f1 100644 --- a/system/src/Grav/Common/Config/ConfigFinder.php +++ b/system/src/Grav/Common/Config/ConfigFinder.php @@ -49,6 +49,18 @@ class ConfigFinder return $list; } + public function locateLanguageFiles(array $languages, array $plugins) + { + $list = []; + foreach (array_reverse($plugins) as $folder) { + $list += $this->detectInFolder($folder, 'languages'); + } + foreach (array_reverse($languages) as $folder) { + $list += $this->detectRecursive($folder); + } + return $list; + } + /** * Get all locations for a single configuration file. *