Create AddonManagerHelper
This commit is contained in:
parent
6a058793f0
commit
e2a2cf5d6e
3 changed files with 221 additions and 0 deletions
160
src/Core/Addon/AddonManagerHelper.php
Normal file
160
src/Core/Addon/AddonManagerHelper.php
Normal file
|
@ -0,0 +1,160 @@
|
|||
<?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;
|
||||
|
||||
use Friendica\Util\Profiler;
|
||||
|
||||
/**
|
||||
* helper functions to handle addons
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final class AddonManagerHelper implements AddonHelper
|
||||
{
|
||||
private string $addonPath;
|
||||
|
||||
private Profiler $profiler;
|
||||
|
||||
/** @deprecated */
|
||||
private AddonHelper $proxy;
|
||||
|
||||
public function __construct(
|
||||
string $addonPath,
|
||||
Profiler $profiler
|
||||
) {
|
||||
$this->addonPath = $addonPath;
|
||||
$this->profiler = $profiler;
|
||||
|
||||
$this->proxy = new AddonProxy($addonPath);
|
||||
}
|
||||
/**
|
||||
* Returns the absolute path to the addon folder
|
||||
*
|
||||
* e.g. `/var/www/html/addon`
|
||||
*/
|
||||
public function getAddonPath(): string
|
||||
{
|
||||
return $this->addonPath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the list of available addons.
|
||||
*
|
||||
* This list is made from scanning the addon/ folder.
|
||||
* Unsupported addons are excluded unless they already are enabled or system.show_unsupported_addon is set.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function getAvailableAddons(): array
|
||||
{
|
||||
return $this->proxy->getAvailableAddons();
|
||||
}
|
||||
|
||||
/**
|
||||
* Installs an addon.
|
||||
*
|
||||
* @param string $addonId name of the addon
|
||||
*
|
||||
* @return bool true on success or false on failure
|
||||
*/
|
||||
public function installAddon(string $addonId): bool
|
||||
{
|
||||
return $this->proxy->installAddon($addonId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Uninstalls an addon.
|
||||
*
|
||||
* @param string $addonId name of the addon
|
||||
*/
|
||||
public function uninstallAddon(string $addonId): void
|
||||
{
|
||||
$this->proxy->uninstallAddon($addonId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load addons.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
public function loadAddons(): void
|
||||
{
|
||||
$this->proxy->loadAddons();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reload (uninstall and install) all updated addons.
|
||||
*/
|
||||
public function reloadAddons(): void
|
||||
{
|
||||
$this->proxy->reloadAddons();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the comment block of an addon as value object.
|
||||
*/
|
||||
public function getAddonInfo(string $addonId): AddonInfo
|
||||
{
|
||||
$default = [
|
||||
'id' => $addonId,
|
||||
'name' => $addonId,
|
||||
];
|
||||
|
||||
if (!is_file($this->getAddonPath() . "/$addonId/$addonId.php")) {
|
||||
return AddonInfo::fromArray($default);
|
||||
}
|
||||
|
||||
$this->profiler->startRecording('file');
|
||||
|
||||
$raw = file_get_contents($this->getAddonPath() . "/$addonId/$addonId.php");
|
||||
|
||||
$this->profiler->stopRecording();
|
||||
|
||||
return AddonInfo::fromString($addonId, $raw);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the provided addon is enabled
|
||||
*/
|
||||
public function isAddonEnabled(string $addonId): bool
|
||||
{
|
||||
return $this->proxy->isAddonEnabled($addonId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list with the IDs of the enabled addons
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function getEnabledAddons(): array
|
||||
{
|
||||
return $this->proxy->getEnabledAddons();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list with the IDs of the non-hidden enabled addons
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function getVisibleEnabledAddons(): array
|
||||
{
|
||||
return $this->proxy->getVisibleEnabledAddons();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list with the IDs of the enabled addons that provides admin settings.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function getEnabledAddonsWithAdminSettings(): array
|
||||
{
|
||||
return $this->proxy->getEnabledAddonsWithAdminSettings();
|
||||
}
|
||||
}
|
32
tests/Unit/Core/Addon/AddonManagerHelperTest.php
Normal file
32
tests/Unit/Core/Addon/AddonManagerHelperTest.php
Normal file
|
@ -0,0 +1,32 @@
|
|||
<?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\Addon;
|
||||
|
||||
use Friendica\Core\Addon\AddonInfo;
|
||||
use Friendica\Core\Addon\AddonManagerHelper;
|
||||
use Friendica\Util\Profiler;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class AddonManagerHelperTest extends TestCase
|
||||
{
|
||||
public function testGetAddonInfoReturnsAddonInfo(): void
|
||||
{
|
||||
$addonManagerHelper = new AddonManagerHelper(
|
||||
__DIR__ . '/../../../Util/addons',
|
||||
$this->createStub(Profiler::class)
|
||||
);
|
||||
|
||||
$info = $addonManagerHelper->getAddonInfo('helloaddon');
|
||||
|
||||
$this->assertInstanceOf(AddonInfo::class, $info);
|
||||
|
||||
$this->assertEquals('Hello Addon', $info->getName());
|
||||
}
|
||||
}
|
29
tests/Util/addons/helloaddon/helloaddon.php
Normal file
29
tests/Util/addons/helloaddon/helloaddon.php
Normal file
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
// SPDX-FileCopyrightText: 2010-2024 the Friendica project
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
/**
|
||||
* Name: Hello Addon
|
||||
* Description: For testing purpose only
|
||||
* Version: 1.0
|
||||
* Author: Artur Weigandt <dont-mail-me@example.com>
|
||||
*/
|
||||
|
||||
use Friendica\Core\Hook;
|
||||
|
||||
function helloaddon_install()
|
||||
{
|
||||
Hook::register('page_end', 'addon/helloaddon/helloaddon.php', 'helloaddon_page_end');
|
||||
}
|
||||
|
||||
function helloaddon_uninstall()
|
||||
{
|
||||
Hook::unregister('page_end', 'addon/helloaddon/helloaddon.php', 'helloaddon_page_end');
|
||||
}
|
||||
|
||||
function helloaddon_page_end(&$html)
|
||||
{
|
||||
$html .= '<p>Hello, World!</p>';
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue