]> source.dussan.org Git - nextcloud-server.git/commitdiff
Introduced app enable/disable/update typed events 35677/head
authorThomas Citharel <tcit@tcit.fr>
Thu, 8 Dec 2022 09:55:19 +0000 (10:55 +0100)
committerThomas Citharel <tcit@tcit.fr>
Mon, 13 Mar 2023 10:52:57 +0000 (11:52 +0100)
OCP\App\ManagerEvent is depreciated since 22 without a replacement

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
lib/composer/composer/autoload_classmap.php
lib/composer/composer/autoload_static.php
lib/private/App/AppManager.php
lib/private/Server.php
lib/private/legacy/OC_App.php
lib/public/App/Events/AppDisableEvent.php [new file with mode: 0644]
lib/public/App/Events/AppEnableEvent.php [new file with mode: 0644]
lib/public/App/Events/AppUpdateEvent.php [new file with mode: 0644]
tests/lib/App/AppManagerTest.php
tests/lib/AppTest.php

index ee6117f9b7344fbb03fcb4e53002ab76263b63af..1ecd8152c0f37780631ec885d1b6df5ca051ddf6 100644 (file)
@@ -82,6 +82,9 @@ return array(
     'OCP\\AppFramework\\Utility\\IControllerMethodReflector' => $baseDir . '/lib/public/AppFramework/Utility/IControllerMethodReflector.php',
     'OCP\\AppFramework\\Utility\\ITimeFactory' => $baseDir . '/lib/public/AppFramework/Utility/ITimeFactory.php',
     'OCP\\App\\AppPathNotFoundException' => $baseDir . '/lib/public/App/AppPathNotFoundException.php',
+    'OCP\\App\\Events\\AppDisableEvent' => $baseDir . '/lib/public/App/Events/AppDisableEvent.php',
+    'OCP\\App\\Events\\AppEnableEvent' => $baseDir . '/lib/public/App/Events/AppEnableEvent.php',
+    'OCP\\App\\Events\\AppUpdateEvent' => $baseDir . '/lib/public/App/Events/AppUpdateEvent.php',
     'OCP\\App\\IAppManager' => $baseDir . '/lib/public/App/IAppManager.php',
     'OCP\\App\\ManagerEvent' => $baseDir . '/lib/public/App/ManagerEvent.php',
     'OCP\\Authentication\\Events\\AnyLoginFailedEvent' => $baseDir . '/lib/public/Authentication/Events/AnyLoginFailedEvent.php',
index 253b9ddbadadc8e8b3f24c6a74a36afb8a4c80a6..2127835e5148337a801a3790fa70a9338bdd61e2 100644 (file)
@@ -115,6 +115,9 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
         'OCP\\AppFramework\\Utility\\IControllerMethodReflector' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Utility/IControllerMethodReflector.php',
         'OCP\\AppFramework\\Utility\\ITimeFactory' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Utility/ITimeFactory.php',
         'OCP\\App\\AppPathNotFoundException' => __DIR__ . '/../../..' . '/lib/public/App/AppPathNotFoundException.php',
+        'OCP\\App\\Events\\AppDisableEvent' => __DIR__ . '/../../..' . '/lib/public/App/Events/AppDisableEvent.php',
+        'OCP\\App\\Events\\AppEnableEvent' => __DIR__ . '/../../..' . '/lib/public/App/Events/AppEnableEvent.php',
+        'OCP\\App\\Events\\AppUpdateEvent' => __DIR__ . '/../../..' . '/lib/public/App/Events/AppUpdateEvent.php',
         'OCP\\App\\IAppManager' => __DIR__ . '/../../..' . '/lib/public/App/IAppManager.php',
         'OCP\\App\\ManagerEvent' => __DIR__ . '/../../..' . '/lib/public/App/ManagerEvent.php',
         'OCP\\Authentication\\Events\\AnyLoginFailedEvent' => __DIR__ . '/../../..' . '/lib/public/Authentication/Events/AnyLoginFailedEvent.php',
index d14f0a2644e03323af4b85296a2bdff7c0257d56..0a89711f1789835cfb27110dea64df5dca24d746 100644 (file)
@@ -40,8 +40,11 @@ namespace OC\App;
 
 use OC\AppConfig;
 use OCP\App\AppPathNotFoundException;
+use OCP\App\Events\AppDisableEvent;
+use OCP\App\Events\AppEnableEvent;
 use OCP\App\IAppManager;
 use OCP\App\ManagerEvent;
+use OCP\EventDispatcher\IEventDispatcher;
 use OCP\ICacheFactory;
 use OCP\IConfig;
 use OCP\IGroup;
@@ -80,7 +83,9 @@ class AppManager implements IAppManager {
        private $memCacheFactory;
 
        /** @var EventDispatcherInterface */
-       private $dispatcher;
+       private $legacyDispatcher;
+
+       private IEventDispatcher $dispatcher;
 
        /** @var LoggerInterface */
        private $logger;
@@ -108,13 +113,15 @@ class AppManager implements IAppManager {
                                                                AppConfig $appConfig,
                                                                IGroupManager $groupManager,
                                                                ICacheFactory $memCacheFactory,
-                                                               EventDispatcherInterface $dispatcher,
+                                                               EventDispatcherInterface $legacyDispatcher,
+                                                               IEventDispatcher $dispatcher,
                                                                LoggerInterface $logger) {
                $this->userSession = $userSession;
                $this->config = $config;
                $this->appConfig = $appConfig;
                $this->groupManager = $groupManager;
                $this->memCacheFactory = $memCacheFactory;
+               $this->legacyDispatcher = $legacyDispatcher;
                $this->dispatcher = $dispatcher;
                $this->logger = $logger;
        }
@@ -163,7 +170,7 @@ class AppManager implements IAppManager {
        }
 
        /**
-        * @param \OCP\IGroup $group
+        * @param IGroup $group
         * @return array
         */
        public function getEnabledAppsForGroup(IGroup $group): array {
@@ -287,7 +294,7 @@ class AppManager implements IAppManager {
         * Notice: This actually checks if the app is enabled and not only if it is installed.
         *
         * @param string $appId
-        * @param \OCP\IGroup[]|String[] $groups
+        * @param IGroup[]|String[] $groups
         * @return bool
         */
        public function isInstalled($appId) {
@@ -320,7 +327,8 @@ class AppManager implements IAppManager {
 
                $this->installedAppsCache[$appId] = 'yes';
                $this->appConfig->setValue($appId, 'enabled', 'yes');
-               $this->dispatcher->dispatch(ManagerEvent::EVENT_APP_ENABLE, new ManagerEvent(
+               $this->dispatcher->dispatchTyped(new AppEnableEvent($appId));
+               $this->legacyDispatcher->dispatch(ManagerEvent::EVENT_APP_ENABLE, new ManagerEvent(
                        ManagerEvent::EVENT_APP_ENABLE, $appId
                ));
                $this->clearAppsCache();
@@ -345,7 +353,7 @@ class AppManager implements IAppManager {
         * Enable an app only for specific groups
         *
         * @param string $appId
-        * @param \OCP\IGroup[] $groups
+        * @param IGroup[] $groups
         * @param bool $forceEnable
         * @throws \InvalidArgumentException if app can't be enabled for groups
         * @throws AppPathNotFoundException
@@ -363,8 +371,9 @@ class AppManager implements IAppManager {
                        $this->ignoreNextcloudRequirementForApp($appId);
                }
 
+               /** @var string[] $groupIds */
                $groupIds = array_map(function ($group) {
-                       /** @var \OCP\IGroup $group */
+                       /** @var IGroup $group */
                        return ($group instanceof IGroup)
                                ? $group->getGID()
                                : $group;
@@ -372,7 +381,8 @@ class AppManager implements IAppManager {
 
                $this->installedAppsCache[$appId] = json_encode($groupIds);
                $this->appConfig->setValue($appId, 'enabled', json_encode($groupIds));
-               $this->dispatcher->dispatch(ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, new ManagerEvent(
+               $this->dispatcher->dispatchTyped(new AppEnableEvent($appId, $groupIds));
+               $this->legacyDispatcher->dispatch(ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, new ManagerEvent(
                        ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, $appId, $groups
                ));
                $this->clearAppsCache();
@@ -407,7 +417,8 @@ class AppManager implements IAppManager {
                        \OC_App::executeRepairSteps($appId, $appData['repair-steps']['uninstall']);
                }
 
-               $this->dispatcher->dispatch(ManagerEvent::EVENT_APP_DISABLE, new ManagerEvent(
+               $this->dispatcher->dispatchTyped(new AppDisableEvent($appId));
+               $this->legacyDispatcher->dispatch(ManagerEvent::EVENT_APP_DISABLE, new ManagerEvent(
                        ManagerEvent::EVENT_APP_DISABLE, $appId
                ));
                $this->clearAppsCache();
index f9fc585e74d76b5aa8c77ba408c7e655f759d51d..9a4ee0da198823ca21e0d8b8862ce560fefb3a33 100644 (file)
@@ -928,6 +928,7 @@ class Server extends ServerContainer implements IServerContainer {
                                $c->get(IGroupManager::class),
                                $c->get(ICacheFactory::class),
                                $c->get(SymfonyAdapter::class),
+                               $c->get(IEventDispatcher::class),
                                $c->get(LoggerInterface::class)
                        );
                });
index 7f51d81d21bbaa5ff418e7f08f82762504de5549..a7887d2bed7f48ec517c88e5ae46574fee06792a 100644 (file)
@@ -50,9 +50,12 @@ declare(strict_types=1);
  * along with this program. If not, see <http://www.gnu.org/licenses/>
  *
  */
+
+use OCP\App\Events\AppUpdateEvent;
 use OCP\AppFramework\QueryException;
 use OCP\App\ManagerEvent;
 use OCP\Authentication\IAlternativeLogin;
+use OCP\EventDispatcher\IEventDispatcher;
 use OCP\ILogger;
 use OCP\Settings\IManager as ISettingsManager;
 use OC\AppFramework\Bootstrap\Coordinator;
@@ -1042,6 +1045,7 @@ class OC_App {
                $version = \OC_App::getAppVersion($appId);
                \OC::$server->getConfig()->setAppValue($appId, 'installed_version', $version);
 
+               \OC::$server->get(IEventDispatcher::class)->dispatchTyped(new AppUpdateEvent($appId));
                \OC::$server->getEventDispatcher()->dispatch(ManagerEvent::EVENT_APP_UPDATE, new ManagerEvent(
                        ManagerEvent::EVENT_APP_UPDATE, $appId
                ));
@@ -1061,7 +1065,7 @@ class OC_App {
                // load the app
                self::loadApp($appId);
 
-               $dispatcher = \OC::$server->get(\OCP\EventDispatcher\IEventDispatcher::class);
+               $dispatcher = \OC::$server->get(IEventDispatcher::class);
 
                // load the steps
                $r = new Repair([], $dispatcher, \OC::$server->get(LoggerInterface::class));
diff --git a/lib/public/App/Events/AppDisableEvent.php b/lib/public/App/Events/AppDisableEvent.php
new file mode 100644 (file)
index 0000000..5a50587
--- /dev/null
@@ -0,0 +1,51 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @copyright Copyright (c) 2022, Thomas Citharel <nextcloud@tcit.fr>
+ *
+ * @author Thomas Citharel <nextcloud@tcit.fr>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+namespace OCP\App\Events;
+
+use OCP\EventDispatcher\Event;
+
+/**
+ * @since 27.0.0
+ */
+class AppDisableEvent extends Event {
+       private string $appId;
+
+       /**
+        * @since 27.0.0
+        */
+       public function __construct(string $appId) {
+               parent::__construct();
+
+               $this->appId = $appId;
+       }
+
+       /**
+        * @since 27.0.0
+        */
+       public function getAppId(): string {
+               return $this->appId;
+       }
+}
diff --git a/lib/public/App/Events/AppEnableEvent.php b/lib/public/App/Events/AppEnableEvent.php
new file mode 100644 (file)
index 0000000..1aff363
--- /dev/null
@@ -0,0 +1,62 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @copyright Copyright (c) 2022, Thomas Citharel <nextcloud@tcit.fr>
+ *
+ * @author Thomas Citharel <nextcloud@tcit.fr>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+namespace OCP\App\Events;
+
+use OCP\EventDispatcher\Event;
+
+/**
+ * @since 27.0.0
+ */
+class AppEnableEvent extends Event {
+       private string $appId;
+       /** @var string[] */
+       private array $groupIds;
+
+       /**
+        * @param string[] $groupIds
+        * @since 27.0.0
+        */
+       public function __construct(string $appId, array $groupIds = []) {
+               parent::__construct();
+
+               $this->appId = $appId;
+               $this->groupIds = $groupIds;
+       }
+
+       /**
+        * @since 27.0.0
+        */
+       public function getAppId(): string {
+               return $this->appId;
+       }
+
+       /**
+        * @since 27.0.0
+        */
+       public function getGroupIds(): array {
+               return $this->groupIds;
+       }
+}
diff --git a/lib/public/App/Events/AppUpdateEvent.php b/lib/public/App/Events/AppUpdateEvent.php
new file mode 100644 (file)
index 0000000..92f1f8f
--- /dev/null
@@ -0,0 +1,51 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * @copyright Copyright (c) 2022, Thomas Citharel <nextcloud@tcit.fr>
+ *
+ * @author Thomas Citharel <nextcloud@tcit.fr>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+namespace OCP\App\Events;
+
+use OCP\EventDispatcher\Event;
+
+/**
+ * @since 27.0.0
+ */
+class AppUpdateEvent extends Event {
+       private string $appId;
+
+       /**
+        * @since 27.0.0
+        */
+       public function __construct(string $appId) {
+               parent::__construct();
+
+               $this->appId = $appId;
+       }
+
+       /**
+        * @since 27.0.0
+        */
+       public function getAppId(): string {
+               return $this->appId;
+       }
+}
index de515837406ffa6d54b3a63d013e33c23b1c09cb..bf9592ac6a685a1654f5e7cc1ff626d815af51df 100644 (file)
@@ -14,7 +14,10 @@ namespace Test\App;
 use OC\App\AppManager;
 use OC\AppConfig;
 use OCP\App\AppPathNotFoundException;
+use OCP\App\Events\AppDisableEvent;
+use OCP\App\Events\AppEnableEvent;
 use OCP\App\IAppManager;
+use OCP\EventDispatcher\IEventDispatcher;
 use OCP\ICache;
 use OCP\ICacheFactory;
 use OCP\IConfig;
@@ -91,6 +94,9 @@ class AppManagerTest extends TestCase {
        protected $cacheFactory;
 
        /** @var EventDispatcherInterface|MockObject */
+       protected $legacyEventDispatcher;
+
+       /** @var IEventDispatcher|MockObject */
        protected $eventDispatcher;
 
        /** @var LoggerInterface|MockObject */
@@ -108,7 +114,8 @@ class AppManagerTest extends TestCase {
                $this->appConfig = $this->getAppConfig();
                $this->cacheFactory = $this->createMock(ICacheFactory::class);
                $this->cache = $this->createMock(ICache::class);
-               $this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
+               $this->legacyEventDispatcher = $this->createMock(EventDispatcherInterface::class);
+               $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
                $this->logger = $this->createMock(LoggerInterface::class);
                $this->cacheFactory->expects($this->any())
                        ->method('createDistributed')
@@ -120,6 +127,7 @@ class AppManagerTest extends TestCase {
                        $this->appConfig,
                        $this->groupManager,
                        $this->cacheFactory,
+                       $this->legacyEventDispatcher,
                        $this->eventDispatcher,
                        $this->logger
                );
@@ -137,12 +145,14 @@ class AppManagerTest extends TestCase {
                if ($this->manager->isEnabledForUser('files_trashbin')) {
                        $this->manager->disableApp('files_trashbin');
                }
+               $this->eventDispatcher->expects($this->once())->method('dispatchTyped')->with(new AppEnableEvent('files_trashbin'));
                $this->manager->enableApp('files_trashbin');
                $this->assertEquals('yes', $this->appConfig->getValue('files_trashbin', 'enabled', 'no'));
        }
 
        public function testDisableApp() {
                $this->expectClearCache();
+               $this->eventDispatcher->expects($this->once())->method('dispatchTyped')->with(new AppDisableEvent('files_trashbin'));
                $this->manager->disableApp('files_trashbin');
                $this->assertEquals('no', $this->appConfig->getValue('files_trashbin', 'enabled', 'no'));
        }
@@ -175,7 +185,7 @@ class AppManagerTest extends TestCase {
                /** @var AppManager|MockObject $manager */
                $manager = $this->getMockBuilder(AppManager::class)
                        ->setConstructorArgs([
-                               $this->userSession, $this->config, $this->appConfig, $this->groupManager, $this->cacheFactory, $this->eventDispatcher, $this->logger
+                               $this->userSession, $this->config, $this->appConfig, $this->groupManager, $this->cacheFactory, $this->legacyEventDispatcher, $this->eventDispatcher, $this->logger
                        ])
                        ->setMethods([
                                'getAppPath',
@@ -187,6 +197,8 @@ class AppManagerTest extends TestCase {
                        ->with('test')
                        ->willReturn('apps/test');
 
+               $this->eventDispatcher->expects($this->once())->method('dispatchTyped')->with(new AppEnableEvent('test', ['group1', 'group2']));
+
                $manager->enableAppForGroups('test', $groups);
                $this->assertEquals('["group1","group2"]', $this->appConfig->getValue('test', 'enabled', 'no'));
        }
@@ -222,7 +234,7 @@ class AppManagerTest extends TestCase {
                /** @var AppManager|MockObject $manager */
                $manager = $this->getMockBuilder(AppManager::class)
                        ->setConstructorArgs([
-                               $this->userSession, $this->config, $this->appConfig, $this->groupManager, $this->cacheFactory, $this->eventDispatcher, $this->logger
+                               $this->userSession, $this->config, $this->appConfig, $this->groupManager, $this->cacheFactory, $this->legacyEventDispatcher, $this->eventDispatcher, $this->logger
                        ])
                        ->setMethods([
                                'getAppPath',
@@ -240,6 +252,8 @@ class AppManagerTest extends TestCase {
                        ->with('test')
                        ->willReturn($appInfo);
 
+               $this->eventDispatcher->expects($this->once())->method('dispatchTyped')->with(new AppEnableEvent('test', ['group1', 'group2']));
+
                $manager->enableAppForGroups('test', $groups);
                $this->assertEquals('["group1","group2"]', $this->appConfig->getValue('test', 'enabled', 'no'));
        }
@@ -276,7 +290,7 @@ class AppManagerTest extends TestCase {
                /** @var AppManager|MockObject $manager */
                $manager = $this->getMockBuilder(AppManager::class)
                        ->setConstructorArgs([
-                               $this->userSession, $this->config, $this->appConfig, $this->groupManager, $this->cacheFactory, $this->eventDispatcher, $this->logger
+                               $this->userSession, $this->config, $this->appConfig, $this->groupManager, $this->cacheFactory, $this->legacyEventDispatcher, $this->eventDispatcher, $this->logger
                        ])
                        ->setMethods([
                                'getAppPath',
@@ -296,6 +310,8 @@ class AppManagerTest extends TestCase {
                                'types' => [$type],
                        ]);
 
+               $this->eventDispatcher->expects($this->never())->method('dispatchTyped')->with(new AppEnableEvent('test', ['group1', 'group2']));
+
                $manager->enableAppForGroups('test', $groups);
        }
 
@@ -470,7 +486,7 @@ class AppManagerTest extends TestCase {
        public function testGetAppsNeedingUpgrade() {
                /** @var AppManager|MockObject $manager */
                $manager = $this->getMockBuilder(AppManager::class)
-                       ->setConstructorArgs([$this->userSession, $this->config, $this->appConfig, $this->groupManager, $this->cacheFactory, $this->eventDispatcher, $this->logger])
+                       ->setConstructorArgs([$this->userSession, $this->config, $this->appConfig, $this->groupManager, $this->cacheFactory, $this->legacyEventDispatcher, $this->eventDispatcher, $this->logger])
                        ->setMethods(['getAppInfo'])
                        ->getMock();
 
@@ -521,7 +537,7 @@ class AppManagerTest extends TestCase {
        public function testGetIncompatibleApps() {
                /** @var AppManager|MockObject $manager */
                $manager = $this->getMockBuilder(AppManager::class)
-                       ->setConstructorArgs([$this->userSession, $this->config, $this->appConfig, $this->groupManager, $this->cacheFactory, $this->eventDispatcher, $this->logger])
+                       ->setConstructorArgs([$this->userSession, $this->config, $this->appConfig, $this->groupManager, $this->cacheFactory, $this->legacyEventDispatcher, $this->eventDispatcher, $this->logger])
                        ->setMethods(['getAppInfo'])
                        ->getMock();
 
index 4b2619a3761ca13173a62a03a26989e1ec035b73..5cdee5e1200682b80b736b9a36a27c60d70d325c 100644 (file)
@@ -12,6 +12,7 @@ namespace Test;
 use OC\App\AppManager;
 use OC\App\InfoParser;
 use OC\AppConfig;
+use OCP\EventDispatcher\IEventDispatcher;
 use OCP\IAppConfig;
 use Psr\Log\LoggerInterface;
 
@@ -553,13 +554,14 @@ class AppTest extends \Test\TestCase {
         */
        private function registerAppConfig(AppConfig $appConfig) {
                $this->overwriteService(AppConfig::class, $appConfig);
-               $this->overwriteService(AppManager::class, new \OC\App\AppManager(
+               $this->overwriteService(AppManager::class, new AppManager(
                        \OC::$server->getUserSession(),
                        \OC::$server->getConfig(),
                        $appConfig,
                        \OC::$server->getGroupManager(),
                        \OC::$server->getMemCacheFactory(),
                        \OC::$server->getEventDispatcher(),
+                       \OC::$server->get(IEventDispatcher::class),
                        \OC::$server->get(LoggerInterface::class)
                ));
        }