From a8249be9288fd3e05be1b398c07b02c44b781dd8 Mon Sep 17 00:00:00 2001 From: Art4 Date: Tue, 4 Feb 2025 09:41:52 +0000 Subject: [PATCH] Create AddonInfo class --- src/Core/Addon/AddonHelper.php | 5 + src/Core/Addon/AddonInfo.php | 130 ++++++++++++++++++++++++ src/Core/Addon/AddonProxy.php | 13 +++ tests/Unit/Core/Addon/AddonInfoTest.php | 55 ++++++++++ 4 files changed, 203 insertions(+) create mode 100644 src/Core/Addon/AddonInfo.php create mode 100644 tests/Unit/Core/Addon/AddonInfoTest.php diff --git a/src/Core/Addon/AddonHelper.php b/src/Core/Addon/AddonHelper.php index 1dd8dc93f5..63f32a3ae7 100644 --- a/src/Core/Addon/AddonHelper.php +++ b/src/Core/Addon/AddonHelper.php @@ -44,6 +44,11 @@ interface AddonHelper */ public function reloadAddons(): void; + /** + * Get the comment block of an addon as value object. + */ + public function getAddonInfo(string $addonId): AddonInfo; + /** * Checks if the provided addon is enabled */ diff --git a/src/Core/Addon/AddonInfo.php b/src/Core/Addon/AddonInfo.php new file mode 100644 index 0000000000..38f7dd6cd6 --- /dev/null +++ b/src/Core/Addon/AddonInfo.php @@ -0,0 +1,130 @@ + (string) $entry['name'], + ]; + + if (array_key_exists('link', $entry)) { + $contributor['link'] = (string) $entry['link']; + } + + return $contributor; + } + + private string $id = ''; + + private string $name = ''; + + private string $description = ''; + + private array $author = []; + + private array $maintainer = []; + + private string $version = ''; + + private string $status = ''; + + private function __construct( + string $id, + string $name, + string $description, + array $author, + array $maintainer, + string $version, + string $status, + ) { + $this->id = $id; + $this->name = $name; + $this->description = $description; + $this->author = $author; + $this->maintainer = $maintainer; + $this->version = $version; + $this->status = $status; + } + + public function getId(): string + { + return $this->id; + } + + public function getName(): string + { + return $this->name; + } + + public function getDescription(): string + { + return $this->description; + } + + public function getAuthor(): array + { + return $this->author; + } + + public function getMaintainer(): array + { + return $this->maintainer; + } + + public function getVersion(): string + { + return $this->version; + } + + public function getStatus(): string + { + return $this->status; + } +} diff --git a/src/Core/Addon/AddonProxy.php b/src/Core/Addon/AddonProxy.php index dead7cbb3e..ded52e8c29 100644 --- a/src/Core/Addon/AddonProxy.php +++ b/src/Core/Addon/AddonProxy.php @@ -60,6 +60,19 @@ final class AddonProxy implements AddonHelper Addon::reload(); } + /** + * Get the comment block of an addon as value object. + */ + public function getAddonInfo(string $addonId): AddonInfo + { + $data = Addon::getInfo($addonId); + + // add addon ID + $data['id'] = $addonId; + + return AddonInfo::fromArray($data); + } + /** * Checks if the provided addon is enabled */ diff --git a/tests/Unit/Core/Addon/AddonInfoTest.php b/tests/Unit/Core/Addon/AddonInfoTest.php new file mode 100644 index 0000000000..12dc474794 --- /dev/null +++ b/tests/Unit/Core/Addon/AddonInfoTest.php @@ -0,0 +1,55 @@ + '', + 'name' => '', + 'description' => '', + 'author' => [], + 'maintainer' => [], + 'version' => '', + 'status' => '', + ]; + + $this->assertInstanceOf(AddonInfo::class, AddonInfo::fromArray($data)); + } + + public function testGetterReturningCorrectValues(): void + { + $data = [ + 'id' => 'test', + 'name' => 'Test-Addon', + 'description' => 'This is an addon for tests', + 'author' => ['name' => 'Sam'], + 'maintainer' => ['name' => 'Sam', 'link' => 'https://example.com'], + 'version' => '0.1', + 'status' => 'In Development', + ]; + + $info = AddonInfo::fromArray($data); + + $this->assertSame($data['id'], $info->getId()); + $this->assertSame($data['name'], $info->getName()); + $this->assertSame($data['description'], $info->getDescription()); + $this->assertSame($data['description'], $info->getDescription()); + $this->assertSame($data['author'], $info->getAuthor()); + $this->assertSame($data['maintainer'], $info->getMaintainer()); + $this->assertSame($data['version'], $info->getVersion()); + $this->assertSame($data['status'], $info->getStatus()); + } +}