From e4e0bc240a3425bddaeba6e788c957f84806ea14 Mon Sep 17 00:00:00 2001 From: Hugo Renard Date: Tue, 13 Jun 2023 11:03:37 +0200 Subject: [PATCH] feat: inject style & script in a modern way --- lib/AppInfo/Application.php | 27 +++++++++++++++++++-------- lib/Listener.php | 22 ++++++++++++++++++++++ 2 files changed, 41 insertions(+), 8 deletions(-) create mode 100644 lib/Listener.php diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 9cae642..6c79665 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -4,17 +4,28 @@ declare(strict_types=1); namespace OCA\MultiOffice\AppInfo; +use OCA\MultiOffice\Listener; use OCP\AppFramework\App; +use OCP\AppFramework\Bootstrap\IBootContext; +use OCP\AppFramework\Bootstrap\IBootstrap; +use OCP\AppFramework\Bootstrap\IRegistrationContext; +use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent; -class Application extends App { +class Application extends App implements IBootstrap +{ + const APP_ID = "multioffice"; - public function __construct() { - parent::__construct('multioffice'); - $eventDispatcher = \OC::$server->getEventDispatcher(); - $eventDispatcher->addListener('OCA\Files::loadAdditionalScripts', function() { - script('multioffice', 'script'); - style('multioffice', 'style'); - }); + public function __construct() + { + parent::__construct(self::APP_ID); } + public function register(IRegistrationContext $context): void + { + $context->registerEventListener(BeforeTemplateRenderedEvent::class, Listener::class); + } + + public function boot(IBootContext $context): void + { + } } diff --git a/lib/Listener.php b/lib/Listener.php new file mode 100644 index 0000000..041c9c6 --- /dev/null +++ b/lib/Listener.php @@ -0,0 +1,22 @@ + +// SPDX-License-Identifier: AGPL-3.0-or-later + +namespace OCA\MultiOffice; + +use OCA\MultiOffice\AppInfo\Application; +use OCP\AppFramework\Http\Events\BeforeTemplateRenderedEvent; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; +use OCP\Util; + +class Listener implements IEventListener +{ + public function handle(Event $event): void + { + if ($event instanceof BeforeTemplateRenderedEvent) { + Util::addStyle(Application::APP_ID, "style"); + Util::addScript(Application::APP_ID, "script"); + } + } +} -- GitLab