From 9980fd09cbc8e5968f660be755ea6bfdc7906509 Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Tue, 14 Jul 2020 03:43:58 +0200 Subject: Use IBootstrap for the app Signed-off-by: Morris Jobke --- lib/AppInfo/Application.php | 74 ++++++--------------- lib/Listener/LoadAdditionalScriptsListener.php | 77 ++++++++++++++++++++++ .../LoadAdditionalScriptsLoggedInListener.php | 41 ++++++++++++ 3 files changed, 138 insertions(+), 54 deletions(-) create mode 100644 lib/Listener/LoadAdditionalScriptsListener.php create mode 100644 lib/Listener/LoadAdditionalScriptsLoggedInListener.php (limited to 'lib') diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 4379d00e..0ea0a526 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -24,20 +24,17 @@ namespace OCA\FirstRunWizard\AppInfo; use OCA\Files\Event\LoadAdditionalScriptsEvent; +use OCA\FirstRunWizard\Listener\LoadAdditionalScriptsListener; +use OCA\FirstRunWizard\Listener\LoadAdditionalScriptsLoggedInListener; use OCA\FirstRunWizard\Notification\AppHint; use OCA\FirstRunWizard\Notification\Notifier; use OCP\AppFramework\App; -use OCP\AppFramework\Http\TemplateResponse; -use OCP\EventDispatcher\IEventDispatcher; -use OCP\IConfig; -use OCP\IInitialStateService; -use OCP\IL10N; -use OCP\IServerContainer; -use OCP\IUser; -use OCP\IUserSession; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use OCP\AppFramework\Bootstrap\IBootContext; +use OCP\AppFramework\Bootstrap\IBootstrap; +use OCP\AppFramework\Bootstrap\IRegistrationContext; +use OCP\AppFramework\Http\Events\LoadAdditionalScriptsLoggedInEvent; -class Application extends App { +class Application extends App implements IBootstrap { /** @var bool */ protected $isCLI; @@ -47,52 +44,21 @@ class Application extends App { $this->isCLI = \OC::$CLI; } - public function register() { - if (!$this->isCLI) { - $this->registerScripts(); - $this->registerNotificationNotifier(); - } - } - - protected function registerScripts() { - /** @var IServerContainer $server */ - $server = $this->getContainer()->getServer(); - /** @var IEventDispatcher $dispatcher */ - $dispatcher = $server->query(IEventDispatcher::class); - - $dispatcher->addListener(TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS_LOGGEDIN, function() { - \OC_Util::addScript('firstrunwizard', 'about'); - }); - - // Display the first run wizard only on the files app, - $dispatcher->addListener(LoadAdditionalScriptsEvent::class, function() use ($server) { - /** @var IUserSession $userSession */ - $userSession = $this->getContainer()->query(IUserSession::class); - $user = $userSession->getUser(); - - if (!$user instanceof IUser) { - return; - } - - /** @var IConfig $config */ - $config = $this->getContainer()->query(IConfig::class); - $appHint = $this->getContainer()->query(AppHint::class); - - if ($config->getUserValue($user->getUID(), 'firstrunwizard', 'show', '1') !== '0') { - \OC_Util::addScript('firstrunwizard', 'activate'); - - $jobList = $this->getContainer()->getServer()->getJobList(); - $jobList->add('OCA\FirstRunWizard\Notification\BackgroundJob', ['uid' => $userSession->getUser()->getUID()]); - } - $appHint->sendAppHintNotifications(); - }); + public function register(IRegistrationContext $context): void { + // Display the first run wizard only on the files app + $context->registerEventListener(LoadAdditionalScriptsEvent::class, LoadAdditionalScriptsListener::class); + $context->registerEventListener(LoadAdditionalScriptsLoggedInEvent::class, LoadAdditionalScriptsLoggedInListener::class); } - protected function registerNotificationNotifier() { - $this->getContainer()->getServer()->getNotificationManager()->registerNotifierService(Notifier::class); + public function boot(IBootContext $context): void { + if (!$this->isCLI) { + $serverContainer = $context->getServerContainer(); + $serverContainer->getNotificationManager()->registerNotifierService(Notifier::class); - /** @var AppHint $appHint */ - $appHint = $this->getContainer()->query(AppHint::class); - $appHint->registerAppListener(); + $appContainer = $context->getAppContainer(); + /** @var AppHint $appHint */ + $appHint = $appContainer->query(AppHint::class); + $appHint->registerAppListener(); + } } } diff --git a/lib/Listener/LoadAdditionalScriptsListener.php b/lib/Listener/LoadAdditionalScriptsListener.php new file mode 100644 index 00000000..b9bccab3 --- /dev/null +++ b/lib/Listener/LoadAdditionalScriptsListener.php @@ -0,0 +1,77 @@ + + * + * @author Morris Jobke + * + * @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 . + * + */ + +namespace OCA\FirstRunWizard\Listener; + +use OCA\Files\Event\LoadAdditionalScriptsEvent; +use OCA\FirstRunWizard\Notification\AppHint; +use OCP\BackgroundJob\IJobList; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\IConfig; +use OCP\IUser; +use OCP\IUserSession; + +class LoadAdditionalScriptsListener implements IEventListener { + /** @var IUserSession */ + private $userSession; + /** @var IConfig */ + private $config; + /** @var AppHint */ + private $appHint; + /** @var IJobList */ + private $jobList; + + public function __construct( + IConfig $config, + IUserSession $userSession, + IJobList $jobList, + AppHint $appHint + ) { + $this->userSession = $userSession; + $this->config = $config; + $this->appHint = $appHint; + $this->jobList = $jobList; + } + + public function handle(Event $event): void { + if (!($event instanceof LoadAdditionalScriptsEvent)) { + return; + } + + $user = $this->userSession->getUser(); + if (!$user instanceof IUser) { + return; + } + + if ($this->config->getUserValue($user->getUID(), 'firstrunwizard', 'show', '1') !== '0') { + \OC_Util::addScript('firstrunwizard', 'activate'); + + $this->jobList->add('OCA\FirstRunWizard\Notification\BackgroundJob', ['uid' => $this->userSession->getUser()->getUID()]); + } + $this->appHint->sendAppHintNotifications(); + } +} diff --git a/lib/Listener/LoadAdditionalScriptsLoggedInListener.php b/lib/Listener/LoadAdditionalScriptsLoggedInListener.php new file mode 100644 index 00000000..ae39c66e --- /dev/null +++ b/lib/Listener/LoadAdditionalScriptsLoggedInListener.php @@ -0,0 +1,41 @@ + + * + * @author Morris Jobke + * + * @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 . + * + */ + +namespace OCA\FirstRunWizard\Listener; + +use OCP\AppFramework\Http\Events\LoadAdditionalScriptsLoggedInEvent; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; + +class LoadAdditionalScriptsLoggedInListener implements IEventListener { + public function handle(Event $event): void { + if (!($event instanceof LoadAdditionalScriptsLoggedInEvent)) { + return; + } + + \OC_Util::addScript('firstrunwizard', 'about'); + } +} -- cgit v1.2.3