From: Art4 Date: Wed, 4 Jun 2025 10:55:51 +0000 (+0000) Subject: Merge branch 'rework-addon-class' into deprecate-addonloader X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=4bac217806b34ff6316365823cc0b33e13320e4f;p=friendica.git Merge branch 'rework-addon-class' into deprecate-addonloader --- 4bac217806b34ff6316365823cc0b33e13320e4f diff --cc src/Core/Addon/AddonManagerHelper.php index 04ed918b3e,9c888f55da..0825e67b13 --- a/src/Core/Addon/AddonManagerHelper.php +++ b/src/Core/Addon/AddonManagerHelper.php @@@ -9,7 -9,7 +9,8 @@@ declare(strict_types=1) namespace Friendica\Core\Addon; +use Friendica\Core\Addon\Exception\AddonInvalidConfigFileException; + use Friendica\Core\Addon\Exception\InvalidAddonException; use Friendica\Core\Cache\Capability\ICanCache; use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Database\Database; @@@ -246,37 -257,19 +258,47 @@@ final class AddonManagerHelper implemen $this->profiler->stopRecording(); - return AddonInfo::fromString($addonId, $raw); + if ($raw === false) { + throw new InvalidAddonException('Could not read addon file: ' . $addonFile); + } + + $result = preg_match("|/\*.*\*/|msU", $raw, $matches); + + if ($result === false || $result === 0 || !is_array($matches) || count($matches) < 1) { + throw new InvalidAddonException('Could not find valid comment block in addon file: ' . $addonFile); + } + + return AddonInfo::fromString($addonId, $matches[0]); } + /** + * 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 --cc tests/Unit/Core/Addon/AddonManagerHelperTest.php index e3e340dd39,882082ceb0..a63c605538 --- a/tests/Unit/Core/Addon/AddonManagerHelperTest.php +++ b/tests/Unit/Core/Addon/AddonManagerHelperTest.php @@@ -12,7 -12,7 +12,8 @@@ namespace Friendica\Test\Unit\Core\Addo use Exception; use Friendica\Core\Addon\AddonInfo; use Friendica\Core\Addon\AddonManagerHelper; +use Friendica\Core\Addon\Exception\AddonInvalidConfigFileException; + use Friendica\Core\Addon\Exception\InvalidAddonException; use Friendica\Core\Cache\Capability\ICanCache; use Friendica\Core\Config\Capability\IManageConfigValues; use Friendica\Database\Database; @@@ -55,76 -55,32 +56,102 @@@ class AddonManagerHelperTest extends Te $this->assertEquals('Hello Addon', $info->getName()); } + public function testGetAddonInfoThrowsInvalidAddonException(): void + { + $root = vfsStream::setup(__FUNCTION__ . '_addons', 0777, [ + 'helloaddon' => [ + 'helloaddon.php' => <<url(), + $this->createStub(Database::class), + $this->createStub(IManageConfigValues::class), + $this->createStub(ICanCache::class), + $this->createStub(LoggerInterface::class), + $this->createStub(Profiler::class) + ); + + $this->expectException(InvalidAddonException::class); + $this->expectExceptionMessage('Could not find valid comment block in addon file:'); + + $addonManagerHelper->getAddonInfo('helloaddon'); + } + + 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);