Implement getAddonDependencyConfig

This commit is contained in:
Art4 2025-05-15 12:37:57 +00:00
parent 638496e553
commit 694893e2bb
3 changed files with 113 additions and 0 deletions

View file

@ -9,6 +9,8 @@ declare(strict_types=1);
namespace Friendica\Core\Addon;
use Friendica\Core\Addon\Exception\AddonInvalidConfigFileException;
/**
* Some functions to handle addons
*/
@ -64,6 +66,17 @@ interface AddonHelper
*/
public function getAddonInfo(string $addonId): AddonInfo;
/**
* Returns a dependency config array for a given addon
*
* This will load a potential config-file from the static directory, like `addon/{addonId}/static/dependencies.config.php`
*
* @throws AddonInvalidConfigFileException If the config file doesn't return an array
*
* @return array the config as array or empty array if no config file was found
*/
public function getAddonDependencyConfig(string $addonId): array;
/**
* Checks if the provided addon is enabled
*/

View file

@ -9,6 +9,7 @@ declare(strict_types=1);
namespace Friendica\Core\Addon;
use Friendica\Core\Addon\Exception\AddonInvalidConfigFileException;
use Friendica\Core\Cache\Capability\ICanCache;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Database\Database;
@ -246,6 +247,34 @@ final class AddonManagerHelper implements AddonHelper
return AddonInfo::fromString($addonId, $raw);
}
/**
* Returns a dependency config array for a given addon
*
* This will load a potential config-file from the static directory, like `addon/{addonId}/static/dependencies.config.php`
*
* @throws AddonInvalidConfigFileException If the config file doesn't return an array
*
* @return array the config as array or empty array if no config file was found
*/
public function getAddonDependencyConfig(string $addonId): array
{
$addonId = Strings::sanitizeFilePathItem(trim($addonId));
$configFile = $this->getAddonPath() . '/' . $addonId . '/static/dependencies.config.php';
if (!file_exists($configFile)) {
return [];
}
$config = include($configFile);
if (!is_array($config)) {
throw new AddonInvalidConfigFileException('Error loading config file ' . $configFile);
}
return $config;
}
/**
* Checks if the provided addon is enabled
*/

View file

@ -12,6 +12,7 @@ namespace Friendica\Test\Unit\Core\Addon;
use Exception;
use Friendica\Core\Addon\AddonInfo;
use Friendica\Core\Addon\AddonManagerHelper;
use Friendica\Core\Addon\Exception\AddonInvalidConfigFileException;
use Friendica\Core\Cache\Capability\ICanCache;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Database\Database;
@ -54,6 +55,76 @@ class AddonManagerHelperTest extends TestCase
$this->assertEquals('Hello Addon', $info->getName());
}
public function testGetAddonDependencyConfigReturnsArray(): void
{
$root = vfsStream::setup(__FUNCTION__ . '_addons', 0777, [
'helloaddon' => [
'static' => [
'dependencies.config.php' => <<<PHP
<?php
return [
'foo' => 'bar',
];
PHP,
],
]
]);
$addonManagerHelper = new AddonManagerHelper(
$root->url(),
$this->createStub(Database::class),
$this->createStub(IManageConfigValues::class),
$this->createStub(ICanCache::class),
$this->createStub(LoggerInterface::class),
$this->createStub(Profiler::class)
);
$this->assertSame(['foo' => 'bar'], $addonManagerHelper->getAddonDependencyConfig('helloaddon'));
}
public function testGetAddonDependencyConfigWithoutConfigFileReturnsEmptyArray(): void
{
$root = vfsStream::setup(__FUNCTION__ . '_addons', 0777, [
'helloaddon' => []
]);
$addonManagerHelper = new AddonManagerHelper(
$root->url(),
$this->createStub(Database::class),
$this->createStub(IManageConfigValues::class),
$this->createStub(ICanCache::class),
$this->createStub(LoggerInterface::class),
$this->createStub(Profiler::class)
);
$this->assertSame([], $addonManagerHelper->getAddonDependencyConfig('helloaddon'));
}
public function testGetAddonDependencyConfigWithoutReturningAnArrayThrowsException(): void
{
$root = vfsStream::setup(__FUNCTION__ . '_addons', 0777, [
'helloaddon' => [
'static' => [
'dependencies.config.php' => '<?php return null;',
],
]
]);
$addonManagerHelper = new AddonManagerHelper(
$root->url(),
$this->createStub(Database::class),
$this->createStub(IManageConfigValues::class),
$this->createStub(ICanCache::class),
$this->createStub(LoggerInterface::class),
$this->createStub(Profiler::class)
);
$this->expectException(AddonInvalidConfigFileException::class);
$this->expectExceptionMessageMatches('^#Error loading config file .+/helloaddon/static/dependencies\.config\.php#$');
$addonManagerHelper->getAddonDependencyConfig('helloaddon');
}
public function testEnabledAddons(): void
{
$config = $this->createStub(IManageConfigValues::class);