Implement parsing of addon files to AddonInfo

This commit is contained in:
Art4 2025-04-29 13:59:58 +00:00
parent baaec75bfc
commit 4b9a674659
2 changed files with 168 additions and 52 deletions

View file

@ -14,6 +14,76 @@ namespace Friendica\Core\Addon;
*/
final class AddonInfo
{
/**
* Parse addon comment in search of addon infos.
*
* like
* \code
* * Name: addon
* * Description: An addon which plugs in
* . * Version: 1.2.3
* * Author: John <profile url>
* * Author: Jane <email>
* * Maintainer: Jess without link
* * Maintainer: Robin <email>
* * Status: in development
* \endcode
*
* @internal Never create this object by yourself, use `Friendica\Core\Addon\AddonHelper::getAddonInfo()` instead.
* @see Friendica\Core\Addon\AddonHelper::getAddonInfo()
*
* @param string $addonId the name of the addon
* @param string $raw The raw file content
*/
public static function fromString(string $addonId, string $raw): self
{
$data = [
'id' => $addonId,
];
$result = preg_match("|/\*.*\*/|msU", $raw, $m);
if ($result === false || $result === 0) {
return self::fromArray($data);
}
$ll = explode("\n", $m[0]);
foreach ($ll as $l) {
$l = trim($l, "\t\n\r */");
if ($l !== '') {
$addon_info = array_map('trim', explode(":", $l, 2));
if (count($addon_info) < 2) {
continue;
}
list($type, $v) = $addon_info;
$type = strtolower($type);
if ($type === 'author' || $type === 'maintainer') {
$r = preg_match("|([^<]+)<([^>]+)>|", $v, $m);
if ($r === false || $r === 0) {
$data[$type][] = ['name' => trim($v)];
} else {
$data[$type][] = ['name' => trim($m[1]), 'link' => $m[2]];
}
} else {
$data[$type] = $v;
}
}
}
// rename author to authors
$data['authors'] = $data['author'];
unset($data['author']);
// rename maintainer to maintainers
$data['maintainers'] = $data['maintainer'];
unset($data['maintainer']);
return self::fromArray($data);
}
/**
* @internal Never create this object by yourself, use `Friendica\Core\Addon\AddonHelper::getAddonInfo()` instead.
*
@ -21,25 +91,21 @@ final class AddonInfo
*/
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'] : '';
$authors = array_key_exists('authors', $info) ? self::parseContributors($info['authors']) : [];
$maintainers = array_key_exists('maintainers', $info) ? self::parseContributors($info['maintainers']) : [];
$version = array_key_exists('version', $info) ? (string) $info['version'] : '';
$status = array_key_exists('status', $info) ? (string) $info['status'] : '';
$addonInfo = new self();
$addonInfo->id = array_key_exists('id', $info) ? (string) $info['id'] : '';
$addonInfo->name = array_key_exists('name', $info) ? (string) $info['name'] : '';
$addonInfo->description = array_key_exists('description', $info) ? (string) $info['description'] : '';
$addonInfo->authors = array_key_exists('authors', $info) ? self::parseContributors($info['authors']) : [];
$addonInfo->maintainers = array_key_exists('maintainers', $info) ? self::parseContributors($info['maintainers']) : [];
$addonInfo->version = array_key_exists('version', $info) ? (string) $info['version'] : '';
$addonInfo->status = array_key_exists('status', $info) ? (string) $info['status'] : '';
return new self(
$id,
$name,
$description,
$authors,
$maintainers,
$version,
$status
);
return $addonInfo;
}
/**
* @param mixed $entries
*/
private static function parseContributors($entries): array
{
if (!is_array($entries)) {
@ -85,22 +151,7 @@ final class AddonInfo
private string $status = '';
private function __construct(
string $id,
string $name,
string $description,
array $authors,
array $maintainers,
string $version,
string $status
) {
$this->id = $id;
$this->name = $name;
$this->description = $description;
$this->authors = $authors;
$this->maintainers = $maintainers;
$this->version = $version;
$this->status = $status;
private function __construct() {
}
public function getId(): string