mirror of
https://github.com/friendica/friendica
synced 2025-02-15 10:14:01 +00:00
Create AddonInfo class
This commit is contained in:
parent
61fa36b227
commit
a8249be928
4 changed files with 203 additions and 0 deletions
|
@ -44,6 +44,11 @@ interface AddonHelper
|
||||||
*/
|
*/
|
||||||
public function reloadAddons(): void;
|
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
|
* Checks if the provided addon is enabled
|
||||||
*/
|
*/
|
||||||
|
|
130
src/Core/Addon/AddonInfo.php
Normal file
130
src/Core/Addon/AddonInfo.php
Normal file
|
@ -0,0 +1,130 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
// Copyright (C) 2010-2024, the Friendica project
|
||||||
|
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Friendica\Core\Addon;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Information about an addon
|
||||||
|
*/
|
||||||
|
final class AddonInfo
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @internal Never create this object by yourself, use `Friendica\Core\Addon\AddonHelper::getAddonInfo()` instead.
|
||||||
|
*
|
||||||
|
* @see Friendica\Core\Addon\AddonHelper::getAddonInfo()
|
||||||
|
*/
|
||||||
|
public static function fromArray(array $info): self
|
||||||
|
{
|
||||||
|
$id = array_key_exists('id', $info) ? (string) $info['id'] : '';
|
||||||
|
$name = array_key_exists('name', $info) ? (string) $info['name'] : '';
|
||||||
|
$description = array_key_exists('description', $info) ? (string) $info['description'] : '';
|
||||||
|
$author = array_key_exists('author', $info) ? self::parseContributor($info['author']) : [];
|
||||||
|
$maintainer = array_key_exists('maintainer', $info) ? self::parseContributor($info['maintainer']) : [];
|
||||||
|
$version = array_key_exists('version', $info) ? (string) $info['version'] : '';
|
||||||
|
$status = array_key_exists('status', $info) ? (string) $info['status'] : '';
|
||||||
|
|
||||||
|
return new self(
|
||||||
|
$id,
|
||||||
|
$name,
|
||||||
|
$description,
|
||||||
|
$author,
|
||||||
|
$maintainer,
|
||||||
|
$version,
|
||||||
|
$status
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function parseContributor($entry): array
|
||||||
|
{
|
||||||
|
if (!is_array($entry)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!array_key_exists('name', $entry)) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$contributor = [
|
||||||
|
'name' => (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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -60,6 +60,19 @@ final class AddonProxy implements AddonHelper
|
||||||
Addon::reload();
|
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
|
* Checks if the provided addon is enabled
|
||||||
*/
|
*/
|
||||||
|
|
55
tests/Unit/Core/Addon/AddonInfoTest.php
Normal file
55
tests/Unit/Core/Addon/AddonInfoTest.php
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
// Copyright (C) 2010-2024, the Friendica project
|
||||||
|
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Friendica\Test\Unit\Core;
|
||||||
|
|
||||||
|
use Friendica\Core\Addon\AddonInfo;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class AddonInfoTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testFromArrayCreatesObject(): void
|
||||||
|
{
|
||||||
|
$data = [
|
||||||
|
'id' => '',
|
||||||
|
'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());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue