]> git.mxchange.org Git - friendica.git/commitdiff
Implement getAddonDependencyConfig
authorArt4 <art4@wlabs.de>
Thu, 15 May 2025 12:37:57 +0000 (12:37 +0000)
committerArt4 <art4@wlabs.de>
Thu, 15 May 2025 12:37:57 +0000 (12:37 +0000)
src/Core/Addon/AddonHelper.php
src/Core/Addon/AddonManagerHelper.php
tests/Unit/Core/Addon/AddonManagerHelperTest.php

index 03a21232e5c1a95e02c021d4b3fa2b31c0535abd..94dd721fa38db54230f73c257d1674d96f1b80f8 100644 (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
         */
index 21573ab89c4d4b71c81729b5474a6be495a7ea29..a5b8ccc96ee085eceadfb53680b052c944b8a402 100644 (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
         */
index fae0502474d3c90c90598ccdcbb0ad220ab7e8a4..0008f806928ca241dbee8958b3719fa43855f2d8 100644 (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);