Map the license data into an object
authorTim Düsterhus <duesterhus@woltlab.com>
Wed, 27 Sep 2023 13:03:21 +0000 (15:03 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Wed, 27 Sep 2023 13:03:21 +0000 (15:03 +0200)
wcfsetup/install/files/acp/templates/license.tpl
wcfsetup/install/files/lib/acp/form/FirstTimeSetupLicenseForm.class.php
wcfsetup/install/files/lib/acp/form/LicenseEditForm.class.php
wcfsetup/install/files/lib/acp/page/LicensePage.class.php
wcfsetup/install/files/lib/bootstrap/com.woltlab.wcf.php
wcfsetup/install/files/lib/system/bbcode/BBCodeHandler.class.php
wcfsetup/install/files/lib/system/package/license/LicenseApi.class.php
wcfsetup/install/files/lib/system/package/license/LicenseData.class.php [new file with mode: 0644]

index 4ba0759db5533f71a267162a3129a646b9b3dd17..a1f1306596adf4a260104a029cf315ead4c32b50 100644 (file)
@@ -59,7 +59,7 @@
     {/hascontent}
 </header>
 
-{if $licenseData[license][type] === 'developer'}
+{if $licenseData->license[type] === 'developer'}
     <p class="warning">{lang}wcf.acp.license.developerLicense{/lang}</p>
 {/if}
 
@@ -76,7 +76,7 @@
             </thead>
             <tbody>
                 {content}
-                {foreach from=$licenseData[woltlab] key=package item=majorVersion}
+                {foreach from=$availablePackages[woltlab] key=package item=majorVersion}
                     <tr class="licensed_packages__package" data-package="{$package}">
                         {if $installedPackages[$package]|isset}
                             <td class="columnText">
             </thead>
             <tbody>
                 {content}
-                {foreach from=$licenseData[pluginstore] key=package item=majorVersion}
+                {foreach from=$availablePackages[pluginstore] key=package item=majorVersion}
                     <tr class="licensed_packages__package" data-package="{$package}">
                         {if $installedPackages[$package]|isset}
                             <td class="columnText">
index a0e791341b74698755a1c5f67401d8904102894a..41f60590dc06871c6ca209721702d7127b9e63e8 100644 (file)
@@ -155,8 +155,8 @@ final class FirstTimeSetupLicenseForm extends AbstractFormBuilderForm
         if (isset($this->licenseApi)) {
             $this->licenseApi->updateLicenseFile();
 
-            if (isset($this->licenseApi->getData()['license']['authCode'])) {
-                $optionData[Option::getOptionByName('package_server_auth_code')->optionID] = $this->licenseApi->getData()['license']['authCode'];
+            if (isset($this->licenseApi->getData()->license['authCode'])) {
+                $optionData[Option::getOptionByName('package_server_auth_code')->optionID] = $this->licenseApi->getData()->license['authCode'];
             }
         }
 
index df8f4883ebac82c752c6aa783dd105bc118242b1..7a932838a97bf6720ea7057254e8bd6bddd815f3 100644 (file)
@@ -204,7 +204,7 @@ final class LicenseEditForm extends AbstractFormBuilderForm
         if (isset($this->licenseApi)) {
             $this->licenseApi->updateLicenseFile();
 
-            $authCode = $this->licenseApi->getData()['license']['authCode'] ?? '';
+            $authCode = $this->licenseApi->getData()->license['authCode'] ?? '';
         } else {
             LicenseApi::removeLicenseFile();
         }
index ed489c9d54cc29ece37feb552aa8ac2d81c3933e..baec0f028a6cece319e90bd1ff01fa07e3403f65 100644 (file)
@@ -11,6 +11,7 @@ use wcf\data\package\update\server\PackageUpdateServer;
 use wcf\page\AbstractPage;
 use wcf\system\database\util\PreparedStatementConditionBuilder;
 use wcf\system\package\license\LicenseApi;
+use wcf\system\package\license\LicenseData;
 use wcf\system\request\LinkHandler;
 use wcf\system\WCF;
 
@@ -28,7 +29,9 @@ final class LicensePage extends AbstractPage
 
     public $neededPermissions = ['admin.configuration.package.canInstallPackage'];
 
-    private array $licenseData;
+    private LicenseData $licenseData;
+
+    private array $availablePackages = [];
 
     private int $licenseNumber;
 
@@ -63,13 +66,13 @@ final class LicensePage extends AbstractPage
         $licenseApi->updateLicenseFile();
 
         $this->licenseData = $licenseApi->getData();
-        if (isset($this->licenseData['license']['licenseID'])) {
-            $this->licenseNumber = $this->licenseData['license']['licenseID'];
+        if (isset($this->licenseData->license['licenseID'])) {
+            $this->licenseNumber = $this->licenseData->license['licenseID'];
         }
 
         $identifiers = \array_merge(
-            \array_keys($this->licenseData['woltlab']),
-            \array_keys($this->licenseData['pluginstore'])
+            \array_keys($this->licenseData->woltlab),
+            \array_keys($this->licenseData->pluginstore)
         );
         $this->installedPackages = $this->getInstalledPackages($identifiers);
 
@@ -82,8 +85,8 @@ final class LicensePage extends AbstractPage
         }
 
         foreach (['woltlab', 'pluginstore'] as $type) {
-            $this->licenseData[$type] = \array_filter(
-                $this->licenseData[$type],
+            $this->availablePackages[$type] = \array_filter(
+                $this->licenseData->{$type},
                 function (string $package) {
                     if (isset($this->installedPackages[$package])) {
                         return true;
@@ -94,7 +97,7 @@ final class LicensePage extends AbstractPage
                 \ARRAY_FILTER_USE_KEY
             );
 
-            \uksort($this->licenseData[$type], function ($packageA, $packageB) {
+            \uksort($this->availablePackages[$type], function ($packageA, $packageB) {
                 $a = $this->installedPackages[$packageA] ?? $this->packageUpdates[$packageA];
                 $b = $this->installedPackages[$packageB] ?? $this->packageUpdates[$packageB];
 
@@ -105,7 +108,7 @@ final class LicensePage extends AbstractPage
             });
         }
 
-        foreach ($this->licenseData['woltlab'] as $identifier => $accessibleVersion) {
+        foreach ($this->availablePackages['woltlab'] as $identifier => $accessibleVersion) {
             if ($accessibleVersion === '*') {
                 continue;
             }
@@ -124,6 +127,7 @@ final class LicensePage extends AbstractPage
 
         WCF::getTPL()->assign([
             'licenseData' => $this->licenseData,
+            'availablePackages' => $this->availablePackages,
             'licenseNumber' => $this->licenseNumber,
             'installedPackages' => $this->installedPackages,
             'installablePackages' => $this->installablePackages,
index c6a3ea339d23694e1f1668ec969d83af9181ac2d..fb03c436f7c71ffacf8de0ae498e3ec15518bc3a 100644 (file)
@@ -68,8 +68,8 @@ return static function (): void {
         $licenseApi = LicenseApi::readFromFile();
         if ($licenseApi !== null) {
             $licenseData = $licenseApi->getData();
-            $brandingFree = $licenseData['woltlab']['com.woltlab.brandingFree'] ?? '0.0';
-            $expiresAt = $licenseData['expiryDates']['com.woltlab.brandingFree'] ?? \TIME_NOW;
+            $brandingFree = $licenseData->woltlab['com.woltlab.brandingFree'] ?? '0.0';
+            $expiresAt = $licenseData->license['expiryDates']['com.woltlab.brandingFree'] ?? \TIME_NOW;
             if ($brandingFree !== '0.0' && $expiresAt >= \TIME_NOW) {
                 define('WOLTLAB_BRANDING', false);
             }
index b7b80806cb8ef8ef583d6d46d498279baf92442e..f4a7c11e2308edf50081790b4440e08d476a4473 100644 (file)
@@ -333,6 +333,6 @@ class BBCodeHandler extends SingletonFactory
             return '';
         }
 
-        return $licenseApi->getData()['license']['ckeditorLicenseKey'] ?? '';
+        return $licenseApi->getData()->license['ckeditorLicenseKey'] ?? '';
     }
 }
index 74a15b41475a45535fa3c8ac40649d3466410260..bd669d3cd15b99260d43d72e86b7dab9738f379c 100644 (file)
@@ -21,7 +21,7 @@ use wcf\system\package\license\exception\ParsingFailed;
  */
 final class LicenseApi
 {
-    private readonly array $data;
+    private readonly LicenseData $data;
     private readonly string $json;
 
     private const LICENSE_FILE = \WCF_DIR . 'license.php';
@@ -32,7 +32,7 @@ final class LicenseApi
         $this->data = $this->parseLicenseData($this->json);
     }
 
-    public function getData(): array
+    public function getData(): LicenseData
     {
         return $this->data;
     }
@@ -55,35 +55,19 @@ final class LicenseApi
         );
     }
 
-    private function parseLicenseData(string $json): array
+    private function parseLicenseData(string $json): LicenseData
     {
         try {
-            /** @var array $result */
-            $result = (new MapperBuilder())
+            return (new MapperBuilder())
                 ->allowSuperfluousKeys()
                 ->mapper()
                 ->map(
-                    <<<'EOT'
-                    array {
-                        status: 200,
-                        license: array {
-                            authCode?: string,
-                            licenseID?: int,
-                            type: string,
-                            expiryDates?: array<string, int>,
-                            ckeditorLicenseKey?: string,
-                        },
-                        pluginstore: array<string, string>,
-                        woltlab: array<string, string>,
-                    }
-                    EOT,
+                   LicenseData::class,
                     Source::json($json)
                 );
         } catch (MappingError $e) {
             throw new ParsingFailed($e);
         }
-
-        return $result;
     }
 
     public static function fetchFromRemote(array $authData = []): LicenseApi
diff --git a/wcfsetup/install/files/lib/system/package/license/LicenseData.class.php b/wcfsetup/install/files/lib/system/package/license/LicenseData.class.php
new file mode 100644 (file)
index 0000000..bfdc351
--- /dev/null
@@ -0,0 +1,33 @@
+<?php
+
+namespace wcf\system\package\license;
+
+/**
+ * Provides structured access to the license data.
+ *
+ * @author Tim Duesterhus
+ * @copyright 2001-2023 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @since 6.0
+ */
+final class LicenseData
+{
+    /**
+     * @param   array{
+     *              authCode?: string,
+     *              licenseID?: int,
+     *              type: string,
+     *              expiryDates?: array<string, int>,
+     *              ckeditorLicenseKey?: string,
+     *          } $license
+     * @param   array<string,string> $pluginstore 
+     * @param   array<string,string> $woltlab 
+     */
+    public function __construct(
+        public readonly array $license,
+        public readonly array $pluginstore,
+        public readonly array $woltlab,
+    )
+    {
+    }
+}