Add `PhrasePreloadCache` to retrieve and implicitly create the preload cache
authorAlexander Ebert <ebert@woltlab.com>
Thu, 10 Nov 2022 15:36:33 +0000 (16:36 +0100)
committerAlexander Ebert <ebert@woltlab.com>
Sun, 13 Nov 2022 14:28:21 +0000 (15:28 +0100)
wcfsetup/install/files/lib/system/language/preload/LanguagePreloader.class.php [deleted file]
wcfsetup/install/files/lib/system/language/preload/PhrasePreloadCache.class.php [new file with mode: 0644]

diff --git a/wcfsetup/install/files/lib/system/language/preload/LanguagePreloader.class.php b/wcfsetup/install/files/lib/system/language/preload/LanguagePreloader.class.php
deleted file mode 100644 (file)
index 66171d8..0000000
+++ /dev/null
@@ -1,62 +0,0 @@
-<?php
-
-namespace wcf\system\language\preload;
-
-use wcf\data\language\Language;
-use wcf\system\event\EventHandler;
-use wcf\system\Regex;
-use wcf\util\DirectoryUtil;
-use wcf\util\FileUtil;
-
-final class LanguagePreloader
-{
-    private readonly EventHandler $eventHandler;
-    private readonly Language $language;
-
-    private const STORAGE_PATH = \WCF_DIR . 'js/preload/';
-
-    public function __construct(Language $language)
-    {
-        $this->eventHandler = EventHandler::getInstance();
-        $this->language = $language;
-    }
-
-    public function rebuild(): void
-    {
-        $registerPreloadPhrases = new RegisterPreloadPhrases($this->language);
-        $this->eventHandler->fire($registerPreloadPhrases);
-
-        $payload = $this->generatePreloadPayload($registerPreloadPhrases->getPreloadPhrases());
-
-        $filename = $this->getPreloadFileName();
-        \file_put_contents($filename, $payload);
-        FileUtil::makeWritable($filename);
-    }
-
-    public function mustBeRebuild(): bool
-    {
-        return \file_exists($this->getPreloadFileName());
-    }
-
-    private function getPreloadFileName(): string
-    {
-        return \sprintf(
-            '%s%s.preload.js',
-            self::STORAGE_PATH,
-            $this->language->getLocale(),
-        );
-    }
-
-    /**
-     * @param PreloadPhrase[] $preloadPhrases
-     */
-    private  function generatePreloadPayload(array $preloadPhrases): string
-    {
-        return '';
-    }
-
-    public static function reset(): void
-    {
-        DirectoryUtil::getInstance(self::STORAGE_PATH)->removePattern(new Regex('.*\.preload\.js$'));
-    }
-}
diff --git a/wcfsetup/install/files/lib/system/language/preload/PhrasePreloadCache.class.php b/wcfsetup/install/files/lib/system/language/preload/PhrasePreloadCache.class.php
new file mode 100644 (file)
index 0000000..ba9d3d6
--- /dev/null
@@ -0,0 +1,44 @@
+<?php
+
+namespace wcf\system\language\preload;
+
+use wcf\data\language\Language;
+use wcf\system\language\preload\command\CachePreloadPhrases;
+use wcf\system\WCF;
+
+/**
+ * Provides the URL to the preload cache for
+ * phrases and creates it if it is missing.
+ *
+ * @author Alexander Ebert
+ * @copyright 2001-2022 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package WoltLabSuite\Core\System\Language\Preload\Command
+ * @since 6.0
+ */
+final class PhrasePreloadCache
+{
+    /**
+     * Returns the URL to the preload cache. Will implicitly
+     * create the cache if it does not exist.
+     */
+    public static function getUrl(Language $language): string
+    {
+        if (self::needsRebuild($language)) {
+            self::rebuild($language);
+        }
+
+        return WCF::getPath() . $language->getPreloadCacheFilename();
+    }
+
+    private static function needsRebuild(Language $language): bool
+    {
+        return \file_exists(\WCF_DIR . $language->getPreloadCacheFilename());
+    }
+
+    private static function rebuild(Language $language): void
+    {
+        $command = new CachePreloadPhrases($language);
+        $command();
+    }
+}