Skip to content

Commit

Permalink
Introduced app enable/disable/update typed events
Browse files Browse the repository at this point in the history
OCP\App\ManagerEvent is depreciated since 22 without a replacement

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
  • Loading branch information
tcitworld committed Dec 8, 2022
1 parent 11d7cd0 commit 2e1b276
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 12 deletions.
3 changes: 3 additions & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@
'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',
Expand Down
3 changes: 3 additions & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,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',
Expand Down
20 changes: 15 additions & 5 deletions lib/private/App/AppManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@

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;
Expand Down Expand Up @@ -81,7 +84,9 @@ class AppManager implements IAppManager {
private $memCacheFactory;

/** @var EventDispatcherInterface */
private $dispatcher;
private $legacyDispatcher;

private IEventDispatcher $dispatcher;

/** @var LoggerInterface */
private $logger;
Expand Down Expand Up @@ -109,13 +114,15 @@ public function __construct(IUserSession $userSession,
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;
}
Expand Down Expand Up @@ -321,7 +328,8 @@ public function enableApp(string $appId, bool $forceEnable = false): void {

$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();
Expand Down Expand Up @@ -373,7 +381,8 @@ public function enableAppForGroups(string $appId, array $groups, bool $forceEnab

$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, $groups));
$this->legacyDispatcher->dispatch(ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, new ManagerEvent(
ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, $appId, $groups
));
$this->clearAppsCache();
Expand Down Expand Up @@ -408,7 +417,8 @@ public function disableApp($appId, $automaticDisabled = false) {
\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();
Expand Down
1 change: 1 addition & 0 deletions lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,7 @@ public function __construct($webRoot, \OC\Config $config) {
$c->get(IGroupManager::class),
$c->get(ICacheFactory::class),
$c->get(SymfonyAdapter::class),
$c->get(IEventDispatcher::class),
$c->get(LoggerInterface::class)
);
});
Expand Down
6 changes: 5 additions & 1 deletion lib/private/legacy/OC_App.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,12 @@
* 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;
Expand Down Expand Up @@ -1029,6 +1032,7 @@ public static function updateApp(string $appId): bool {
$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
));
Expand All @@ -1048,7 +1052,7 @@ public static function executeRepairSteps(string $appId, array $steps) {
// 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));
Expand Down
45 changes: 45 additions & 0 deletions lib/public/App/Events/AppDisableEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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 26.0.0
*/
class AppDisableEvent extends Event {
private string $appId;

public function __construct(string $appId) {
parent::__construct();

$this->appId = $appId;
}

public function getAppId(): string {
return $this->appId;
}
}
59 changes: 59 additions & 0 deletions lib/public/App/Events/AppEnableEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?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;
use OCP\IGroup;

/**
* @since 26.0.0
*/
class AppEnableEvent extends Event {
private string $appId;
/** @var IGroup[] */
private array $groups;

public function __construct(string $appId, array $groups = []) {
parent::__construct();

$this->appId = $appId;
$this->groups = $groups;
}

public function getAppId(): string {
return $this->appId;
}

/**
* returns the group Ids
* @return string[]
*/
public function getGroups(): array {
return array_map(function ($group) {
return $group->getGID();
}, $this->groups);
}
}
45 changes: 45 additions & 0 deletions lib/public/App/Events/AppUpdateEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?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 26.0.0
*/
class AppUpdateEvent extends Event {
private string $appId;

public function __construct(string $appId) {
parent::__construct();

$this->appId = $appId;
}

public function getAppId(): string {
return $this->appId;
}
}
Loading

0 comments on commit 2e1b276

Please sign in to comment.