diff --git a/src/Core/Addon/AddonHelper.php b/src/Core/Addon/AddonHelper.php index 03a21232e5..94dd721fa3 100644 --- a/src/Core/Addon/AddonHelper.php +++ b/src/Core/Addon/AddonHelper.php @@ -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 */ diff --git a/src/Core/Addon/AddonManagerHelper.php b/src/Core/Addon/AddonManagerHelper.php index 21573ab89c..a5b8ccc96e 100644 --- a/src/Core/Addon/AddonManagerHelper.php +++ b/src/Core/Addon/AddonManagerHelper.php @@ -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 */ diff --git a/tests/Unit/Core/Addon/AddonManagerHelperTest.php b/tests/Unit/Core/Addon/AddonManagerHelperTest.php index fae0502474..0008f80692 100644 --- a/tests/Unit/Core/Addon/AddonManagerHelperTest.php +++ b/tests/Unit/Core/Addon/AddonManagerHelperTest.php @@ -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' => << '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' => '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);