]> git.mxchange.org Git - friendica.git/commitdiff
Merge branch 'rework-addon-class' into deprecate-addonloader
authorArt4 <art4@wlabs.de>
Wed, 4 Jun 2025 10:55:51 +0000 (10:55 +0000)
committerArt4 <art4@wlabs.de>
Wed, 4 Jun 2025 10:55:51 +0000 (10:55 +0000)
1  2 
src/Core/Addon/AddonHelper.php
src/Core/Addon/AddonManagerHelper.php
tests/Unit/Core/Addon/AddonManagerHelperTest.php

Simple merge
index 04ed918b3e35060b36c3967154e8c75626d3783b,9c888f55dab34e315b25e6c294a33323e01307e7..0825e67b1319ee6b57afca3138809f6fd7799478
@@@ -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
         */
index e3e340dd39dfd824b108c0f06f038d53ff494995,882082ceb00982e6753924454a6ead7fb980193e..a63c6055389377dfcd635062462fa2239a99ec26
@@@ -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' => <<<PHP
+                                       <?php
+                                       // This is not a valid addon comment section
+                                       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->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' => <<<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);