From 974f03632655753bfe954ecefd81913f52a67fc5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Wed, 27 Sep 2023 15:03:21 +0200 Subject: [PATCH] Map the license data into an object --- .../install/files/acp/templates/license.tpl | 6 ++-- .../form/FirstTimeSetupLicenseForm.class.php | 4 +-- .../lib/acp/form/LicenseEditForm.class.php | 2 +- .../files/lib/acp/page/LicensePage.class.php | 22 ++++++++----- .../files/lib/bootstrap/com.woltlab.wcf.php | 4 +-- .../lib/system/bbcode/BBCodeHandler.class.php | 2 +- .../package/license/LicenseApi.class.php | 26 +++------------ .../package/license/LicenseData.class.php | 33 +++++++++++++++++++ 8 files changed, 60 insertions(+), 39 deletions(-) create mode 100644 wcfsetup/install/files/lib/system/package/license/LicenseData.class.php diff --git a/wcfsetup/install/files/acp/templates/license.tpl b/wcfsetup/install/files/acp/templates/license.tpl index 4ba0759db5..a1f1306596 100644 --- a/wcfsetup/install/files/acp/templates/license.tpl +++ b/wcfsetup/install/files/acp/templates/license.tpl @@ -59,7 +59,7 @@ {/hascontent} -{if $licenseData[license][type] === 'developer'} +{if $licenseData->license[type] === 'developer'}

{lang}wcf.acp.license.developerLicense{/lang}

{/if} @@ -76,7 +76,7 @@ {content} - {foreach from=$licenseData[woltlab] key=package item=majorVersion} + {foreach from=$availablePackages[woltlab] key=package item=majorVersion} {if $installedPackages[$package]|isset} @@ -135,7 +135,7 @@ {content} - {foreach from=$licenseData[pluginstore] key=package item=majorVersion} + {foreach from=$availablePackages[pluginstore] key=package item=majorVersion} {if $installedPackages[$package]|isset} diff --git a/wcfsetup/install/files/lib/acp/form/FirstTimeSetupLicenseForm.class.php b/wcfsetup/install/files/lib/acp/form/FirstTimeSetupLicenseForm.class.php index a0e791341b..41f60590dc 100644 --- a/wcfsetup/install/files/lib/acp/form/FirstTimeSetupLicenseForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/FirstTimeSetupLicenseForm.class.php @@ -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']; } } diff --git a/wcfsetup/install/files/lib/acp/form/LicenseEditForm.class.php b/wcfsetup/install/files/lib/acp/form/LicenseEditForm.class.php index df8f4883eb..7a932838a9 100644 --- a/wcfsetup/install/files/lib/acp/form/LicenseEditForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/LicenseEditForm.class.php @@ -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(); } diff --git a/wcfsetup/install/files/lib/acp/page/LicensePage.class.php b/wcfsetup/install/files/lib/acp/page/LicensePage.class.php index ed489c9d54..baec0f028a 100644 --- a/wcfsetup/install/files/lib/acp/page/LicensePage.class.php +++ b/wcfsetup/install/files/lib/acp/page/LicensePage.class.php @@ -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, diff --git a/wcfsetup/install/files/lib/bootstrap/com.woltlab.wcf.php b/wcfsetup/install/files/lib/bootstrap/com.woltlab.wcf.php index c6a3ea339d..fb03c436f7 100644 --- a/wcfsetup/install/files/lib/bootstrap/com.woltlab.wcf.php +++ b/wcfsetup/install/files/lib/bootstrap/com.woltlab.wcf.php @@ -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); } diff --git a/wcfsetup/install/files/lib/system/bbcode/BBCodeHandler.class.php b/wcfsetup/install/files/lib/system/bbcode/BBCodeHandler.class.php index b7b80806cb..f4a7c11e23 100644 --- a/wcfsetup/install/files/lib/system/bbcode/BBCodeHandler.class.php +++ b/wcfsetup/install/files/lib/system/bbcode/BBCodeHandler.class.php @@ -333,6 +333,6 @@ class BBCodeHandler extends SingletonFactory return ''; } - return $licenseApi->getData()['license']['ckeditorLicenseKey'] ?? ''; + return $licenseApi->getData()->license['ckeditorLicenseKey'] ?? ''; } } diff --git a/wcfsetup/install/files/lib/system/package/license/LicenseApi.class.php b/wcfsetup/install/files/lib/system/package/license/LicenseApi.class.php index 74a15b4147..bd669d3cd1 100644 --- a/wcfsetup/install/files/lib/system/package/license/LicenseApi.class.php +++ b/wcfsetup/install/files/lib/system/package/license/LicenseApi.class.php @@ -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, - ckeditorLicenseKey?: string, - }, - pluginstore: array, - woltlab: array, - } - 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 index 0000000000..bfdc351f9b --- /dev/null +++ b/wcfsetup/install/files/lib/system/package/license/LicenseData.class.php @@ -0,0 +1,33 @@ + + * @since 6.0 + */ +final class LicenseData +{ + /** + * @param array{ + * authCode?: string, + * licenseID?: int, + * type: string, + * expiryDates?: array, + * ckeditorLicenseKey?: string, + * } $license + * @param array $pluginstore + * @param array $woltlab + */ + public function __construct( + public readonly array $license, + public readonly array $pluginstore, + public readonly array $woltlab, + ) + { + } +} -- 2.20.1