From e84504c4d6a079d55d2f78ad21b1ad20d53035bb Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Wed, 22 Jul 2020 22:11:01 +0200 Subject: [PATCH] Use IBootstrap for the files_trashbin app Signed-off-by: Morris Jobke --- .../files_sharing/tests/SharedStorageTest.php | 6 +- apps/files_sharing/tests/UpdaterTest.php | 7 +- apps/files_trashbin/appinfo/app.php | 41 --------- .../lib/AppInfo/Application.php | 84 ++++++++++--------- apps/files_trashbin/lib/Trashbin.php | 14 ---- apps/files_trashbin/tests/StorageTest.php | 6 +- apps/files_trashbin/tests/TrashbinTest.php | 8 +- 7 files changed, 64 insertions(+), 102 deletions(-) delete mode 100644 apps/files_trashbin/appinfo/app.php diff --git a/apps/files_sharing/tests/SharedStorageTest.php b/apps/files_sharing/tests/SharedStorageTest.php index fb8207a3c1..e3e57cc73a 100644 --- a/apps/files_sharing/tests/SharedStorageTest.php +++ b/apps/files_sharing/tests/SharedStorageTest.php @@ -31,6 +31,8 @@ namespace OCA\Files_Sharing\Tests; use OC\Files\View; use OCA\Files_Sharing\SharedStorage; +use OCA\Files_Trashbin\AppInfo\Application; +use OCP\AppFramework\Bootstrap\IBootContext; use OCP\Files\NotFoundException; use OCP\Share\IShare; @@ -42,7 +44,9 @@ use OCP\Share\IShare; class SharedStorageTest extends TestCase { protected function setUp(): void { parent::setUp(); - \OCA\Files_Trashbin\Trashbin::registerHooks(); + // register trashbin hooks + $trashbinApp = new Application(); + $trashbinApp->boot($this->createMock(IBootContext::class)); $this->folder = '/folder_share_storage_test'; $this->filename = '/share-api-storage.txt'; diff --git a/apps/files_sharing/tests/UpdaterTest.php b/apps/files_sharing/tests/UpdaterTest.php index 30efbac533..344c08bea4 100644 --- a/apps/files_sharing/tests/UpdaterTest.php +++ b/apps/files_sharing/tests/UpdaterTest.php @@ -31,6 +31,8 @@ namespace OCA\Files_Sharing\Tests; +use OCA\Files_Trashbin\AppInfo\Application; +use OCP\AppFramework\Bootstrap\IBootContext; use OCP\Share\IShare; /** @@ -77,8 +79,9 @@ class UpdaterTest extends TestCase { $status = \OC::$server->getAppManager()->isEnabledForUser('files_trashbin'); (new \OC_App())->enable('files_trashbin'); - - \OCA\Files_Trashbin\Trashbin::registerHooks(); + // register trashbin hooks + $trashbinApp = new Application(); + $trashbinApp->boot($this->createMock(IBootContext::class)); $fileinfo = \OC\Files\Filesystem::getFileInfo($this->folder); $this->assertTrue($fileinfo instanceof \OC\Files\FileInfo); diff --git a/apps/files_trashbin/appinfo/app.php b/apps/files_trashbin/appinfo/app.php deleted file mode 100644 index 83f8defccb..0000000000 --- a/apps/files_trashbin/appinfo/app.php +++ /dev/null @@ -1,41 +0,0 @@ - - * @author Florin Peter - * @author John Molakvoæ (skjnldsv) - * @author Jörn Friedrich Dreyer - * @author Victor Dubiniuk - * @author Vincent Petry - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * 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, version 3, - * along with this program. If not, see - * - */ - -// register hooks -\OCA\Files_Trashbin\Trashbin::registerHooks(); - -\OCA\Files\App::getNavigationManager()->add(function () { - $l = \OC::$server->getL10N('files_trashbin'); - return [ - 'id' => 'trashbin', - 'appname' => 'files_trashbin', - 'script' => 'list.php', - 'order' => 50, - 'name' => $l->t('Deleted files'), - 'classes' => 'pinned', - ]; -}); diff --git a/apps/files_trashbin/lib/AppInfo/Application.php b/apps/files_trashbin/lib/AppInfo/Application.php index d9f52d21a6..ed78709a5d 100644 --- a/apps/files_trashbin/lib/AppInfo/Application.php +++ b/apps/files_trashbin/lib/AppInfo/Application.php @@ -27,57 +27,59 @@ namespace OCA\Files_Trashbin\AppInfo; -use OCA\DAV\CalDAV\Proxy\ProxyMapper; use OCA\DAV\Connector\Sabre\Principal; use OCA\Files_Trashbin\Capabilities; use OCA\Files_Trashbin\Expiration; use OCA\Files_Trashbin\Trash\ITrashManager; use OCA\Files_Trashbin\Trash\TrashManager; +use OCP\App\IAppManager; use OCP\AppFramework\App; +use OCP\AppFramework\Bootstrap\IBootContext; +use OCP\AppFramework\Bootstrap\IBootstrap; +use OCP\AppFramework\Bootstrap\IRegistrationContext; +use OCP\ILogger; +use OCP\IServerContainer; -class Application extends App { +class Application extends App implements IBootstrap { public function __construct(array $urlParams = []) { parent::__construct('files_trashbin', $urlParams); - - $container = $this->getContainer(); - /* - * Register capabilities - */ - $container->registerCapability(Capabilities::class); - - /* - * Register expiration - */ - $container->registerAlias('Expiration', Expiration::class); - - /* - * Register $principalBackend for the DAV collection - */ - $container->registerService('principalBackend', function () { - return new Principal( - \OC::$server->getUserManager(), - \OC::$server->getGroupManager(), - \OC::$server->getShareManager(), - \OC::$server->getUserSession(), - \OC::$server->getAppManager(), - \OC::$server->query(ProxyMapper::class), - \OC::$server->getConfig() - ); - }); - - $container->registerService(ITrashManager::class, function () { - return new TrashManager(); - }); - - $this->registerTrashBackends(); } - public function registerTrashBackends() { - $server = $this->getContainer()->getServer(); - $logger = $server->getLogger(); - $appManager = $server->getAppManager(); - /** @var ITrashManager $trashManager */ - $trashManager = $this->getContainer()->getServer()->query(ITrashManager::class); + public function register(IRegistrationContext $context): void { + $context->registerCapability(Capabilities::class); + + $context->registerServiceAlias('Expiration', Expiration::class); + $context->registerServiceAlias(ITrashManager::class, TrashManager::class); + /** Register $principalBackend for the DAV collection */ + $context->registerServiceAlias('principalBackend', Principal::class); + } + + public function boot(IBootContext $context): void { + $context->injectFn([$this, 'registerTrashBackends']); + + // create storage wrapper on setup + \OCP\Util::connectHook('OC_Filesystem', 'preSetup', 'OCA\Files_Trashbin\Storage', 'setupStorage'); + //Listen to delete user signal + \OCP\Util::connectHook('OC_User', 'pre_deleteUser', 'OCA\Files_Trashbin\Hooks', 'deleteUser_hook'); + //Listen to post write hook + \OCP\Util::connectHook('OC_Filesystem', 'post_write', 'OCA\Files_Trashbin\Hooks', 'post_write_hook'); + // pre and post-rename, disable trash logic for the copy+unlink case + \OCP\Util::connectHook('OC_Filesystem', 'delete', 'OCA\Files_Trashbin\Trashbin', 'ensureFileScannedHook'); + + \OCA\Files\App::getNavigationManager()->add(function () { + $l = \OC::$server->getL10N('files_trashbin'); + return [ + 'id' => 'trashbin', + 'appname' => 'files_trashbin', + 'script' => 'list.php', + 'order' => 50, + 'name' => $l->t('Deleted files'), + 'classes' => 'pinned', + ]; + }); + } + + public function registerTrashBackends(IServerContainer $serverContainer, ILogger $logger, IAppManager $appManager, ITrashManager $trashManager) { foreach ($appManager->getInstalledApps() as $app) { $appInfo = $appManager->getAppInfo($app); if (isset($appInfo['trash'])) { @@ -87,7 +89,7 @@ class Application extends App { $for = $backend['@attributes']['for']; try { - $backendObject = $server->query($class); + $backendObject = $serverContainer->query($class); $trashManager->registerBackend($for, $backendObject); } catch (\Exception $e) { $logger->logException($e); diff --git a/apps/files_trashbin/lib/Trashbin.php b/apps/files_trashbin/lib/Trashbin.php index f73cc1f4aa..46bdc08ce3 100644 --- a/apps/files_trashbin/lib/Trashbin.php +++ b/apps/files_trashbin/lib/Trashbin.php @@ -1008,20 +1008,6 @@ class Trashbin { return isset($fileInfo['size']) ? $fileInfo['size'] : 0; } - /** - * register hooks - */ - public static function registerHooks() { - // create storage wrapper on setup - \OCP\Util::connectHook('OC_Filesystem', 'preSetup', 'OCA\Files_Trashbin\Storage', 'setupStorage'); - //Listen to delete user signal - \OCP\Util::connectHook('OC_User', 'pre_deleteUser', 'OCA\Files_Trashbin\Hooks', 'deleteUser_hook'); - //Listen to post write hook - \OCP\Util::connectHook('OC_Filesystem', 'post_write', 'OCA\Files_Trashbin\Hooks', 'post_write_hook'); - // pre and post-rename, disable trash logic for the copy+unlink case - \OCP\Util::connectHook('OC_Filesystem', 'delete', 'OCA\Files_Trashbin\Trashbin', 'ensureFileScannedHook'); - } - /** * check if trash bin is empty for a given user * diff --git a/apps/files_trashbin/tests/StorageTest.php b/apps/files_trashbin/tests/StorageTest.php index 2efdf1b895..36628cb08b 100644 --- a/apps/files_trashbin/tests/StorageTest.php +++ b/apps/files_trashbin/tests/StorageTest.php @@ -34,9 +34,11 @@ namespace OCA\Files_Trashbin\Tests; use OC\Files\Filesystem; use OC\Files\Storage\Temporary; +use OCA\Files_Trashbin\AppInfo\Application; use OCA\Files_Trashbin\Events\MoveToTrashEvent; use OCA\Files_Trashbin\Storage; use OCA\Files_Trashbin\Trash\ITrashManager; +use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Utility\ITimeFactory; use OCP\Files\Cache\ICache; use OCP\Files\Folder; @@ -75,7 +77,9 @@ class StorageTest extends \Test\TestCase { parent::setUp(); \OC_Hook::clear(); - \OCA\Files_Trashbin\Trashbin::registerHooks(); + // register trashbin hooks + $trashbinApp = new Application(); + $trashbinApp->boot($this->createMock(IBootContext::class)); $this->user = $this->getUniqueId('user'); \OC::$server->getUserManager()->createUser($this->user, $this->user); diff --git a/apps/files_trashbin/tests/TrashbinTest.php b/apps/files_trashbin/tests/TrashbinTest.php index 7dfa93bb04..96a76e1e2f 100644 --- a/apps/files_trashbin/tests/TrashbinTest.php +++ b/apps/files_trashbin/tests/TrashbinTest.php @@ -30,6 +30,9 @@ */ use OCA\Files_Sharing\AppInfo\Application; +use OCA\Files_Trashbin\AppInfo\Application as TrashbinApplication; +use OC\AppFramework\Bootstrap\BootContext; +use OC\AppFramework\DependencyInjection\DIContainer; use OCP\Share\IShare; /** @@ -83,8 +86,9 @@ class TrashbinTest extends \Test\TestCase { $expiration = \OC::$server->query(\OCA\Files_Trashbin\Expiration::class); $expiration->setRetentionObligation('auto, 2'); - // register hooks - \OCA\Files_Trashbin\Trashbin::registerHooks(); + // register trashbin hooks + $trashbinApp = new TrashbinApplication(); + $trashbinApp->boot(new BootContext(new DIContainer('', [], \OC::$server))); // create test user self::loginHelper(self::TEST_TRASHBIN_USER2, true);