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

index 84b0c5f89dba6a7dde4783ead4e459f566317f35..03a21232e5c1a95e02c021d4b3fa2b31c0535abd 100644 (file)
@@ -55,7 +55,7 @@ interface AddonHelper
        public function loadAddons(): void;
 
        /**
-        * Reload (uninstall and install) all updated addons.
+        * Reload (uninstall and install) all installed and modified addons.
         */
        public function reloadAddons(): void;
 
index d8ff19af93577199713c1f5ea3c513fc5b3df561..f2ff78d896b22dc1efa030f9c1b0c312cc664247 100644 (file)
@@ -201,11 +201,31 @@ final class AddonManagerHelper implements AddonHelper
        }
 
        /**
-        * Reload (uninstall and install) all updated addons.
+        * Reload (uninstall and install) all installed and modified addons.
         */
        public function reloadAddons(): void
        {
-               $this->proxy->reloadAddons();
+               $addons = array_filter($this->config->get('addons') ?? []);
+
+               foreach ($addons as $addonName => $data) {
+                       $addonId = Strings::sanitizeFilePathItem(trim($addonName));
+
+                       $addon_file_path = $this->getAddonPath() . '/' . $addonId . '/' . $addonId . '.php';
+
+                       if (!file_exists($addon_file_path)) {
+                               continue;
+                       }
+
+                       if (array_key_exists('last_update', $data) && intval($data['last_update']) === filemtime($addon_file_path)) {
+                               // Addon unmodified, skipping
+                               continue;
+                       }
+
+                       $this->logger->debug("Addon {addon}: {action}", ['action' => 'reload', 'addon' => $addonId]);
+
+                       $this->uninstallAddon($addonId);
+                       $this->installAddon($addonId);
+               }
        }
 
        /**
index 951f9655ac9fe00b7cbe3e5b86cedad4ea1b72c8..41320361ddd4e5affd653a43e791db94a4abe071 100644 (file)
@@ -162,12 +162,12 @@ class AddonManagerHelperTest extends TestCase
                $root = vfsStream::setup(__FUNCTION__ . '_addons', 0777, [
                        $addonName => [
                                $addonName . '.php' => <<<PHP
-                                                                       <?php
-                                                                       function {$addonName}_install()
-                                                                       {
-                                                                               throw new \Exception("Addon installed");
-                                                                       }
-                                                                       PHP,
+                                       <?php
+                                       function {$addonName}_install()
+                                       {
+                                               throw new \Exception("Addon installed");
+                                       }
+                                       PHP,
                        ]
                ]);
 
@@ -270,12 +270,12 @@ class AddonManagerHelperTest extends TestCase
                $root = vfsStream::setup(__FUNCTION__ . '_addons', 0777, [
                        $addonName => [
                                $addonName . '.php' => <<<PHP
-                                                                       <?php
-                                                                       function {$addonName}_uninstall()
-                                                                       {
-                                                                               throw new \Exception("Addon uninstalled");
-                                                                       }
-                                                                       PHP,
+                                       <?php
+                                       function {$addonName}_uninstall()
+                                       {
+                                               throw new \Exception("Addon uninstalled");
+                                       }
+                                       PHP,
                        ]
                ]);
 
@@ -355,4 +355,51 @@ class AddonManagerHelperTest extends TestCase
 
                $this->assertSame([], $addonManagerHelper->getEnabledAddons());
        }
+
+       public function testReloadAddonsInstallsAddon(): void
+       {
+               // We need a unique name for the addon to avoid conflicts
+               // with other tests that may define the same install function.
+               $addonName = __FUNCTION__;
+
+               $root = vfsStream::setup(__FUNCTION__ . '_addons', 0777, [
+                       $addonName => [
+                               $addonName . '.php' => <<<PHP
+                                       <?php
+                                       function {$addonName}_install()
+                                       {
+                                               throw new \Exception("Addon reinstalled");
+                                       }
+                                       PHP,
+                       ]
+               ]);
+
+               $root->getChild($addonName . '/' . $addonName . '.php')->lastModified(1234567890);
+
+               $config = $this->createStub(IManageConfigValues::class);
+               $config->method('get')->willReturn([
+                       $addonName => [
+                               'last_update' => 0,
+                               'admin' => false,
+                       ],
+               ]);
+
+               $addonManagerHelper = new AddonManagerHelper(
+                       $root->url(),
+                       $this->createStub(Database::class),
+                       $config,
+                       $this->createStub(ICanCache::class),
+                       $this->createStub(LoggerInterface::class),
+                       $this->createStub(Profiler::class)
+               );
+
+               $addonManagerHelper->loadAddons();
+
+               $this->assertSame([$addonName], $addonManagerHelper->getEnabledAddons());
+
+               $this->expectException(Exception::class);
+               $this->expectExceptionMessage('Addon reinstalled');
+
+               $addonManagerHelper->reloadAddons();
+       }
 }