Skip to content

Commit

Permalink
Migrate from app.php to IBootstrap
Browse files Browse the repository at this point in the history
Signed-off-by: Patrik Kernstock <patrik@kernstock.net>
  • Loading branch information
patschi committed Jul 21, 2023
1 parent 0d4e6d9 commit dd0f0a4
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 67 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ officeonline.zip
build/
vendor/
.php-cs-fixer.cache
.idea
52 changes: 0 additions & 52 deletions appinfo/app.php

This file was deleted.

60 changes: 45 additions & 15 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@
use OC\Files\Type\Detection;
use OC\Security\CSP\ContentSecurityPolicy;
use OCA\Federation\TrustedServers;
use OCA\Files\Event\LoadAdditionalScriptsEvent;
use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent;
use OCA\Officeonline\Capabilities;
use OCA\Officeonline\Hooks\WopiLockHooks;
use OCA\Officeonline\Listener\LoadOfficeOnlineFilesActions;
use OCA\Officeonline\Listener\LoadOfficeOnlineFilesSharingActions;
use OCA\Officeonline\Middleware\WOPIMiddleware;
use OCA\Officeonline\PermissionManager;
use OCA\Officeonline\Preview\MSExcel;
use OCA\Officeonline\Preview\MSWord;
use OCA\Officeonline\Preview\OOXML;
Expand All @@ -38,11 +43,14 @@
use OCA\Officeonline\Service\FederationService;
use OCA\Viewer\Event\LoadViewer;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\AppFramework\QueryException;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IPreview;

class Application extends App {
class Application extends App implements IBootstrap {
public const APP_ID = 'officeonline';

/**
Expand All @@ -59,24 +67,46 @@ private function domainOnly(string $url): string {
return "$scheme$host$port";
}

public function register(IRegistrationContext $context): void {
$context->registerEventListener(LoadAdditionalScriptsEvent::class, LoadOfficeOnlineFilesActions::class);
$context->registerEventListener(BeforeTemplateRenderedEvent::class, LoadOfficeOnlineFilesSharingActions::class);
}

public function __construct(array $urlParams = []) {
parent::__construct(self::APP_ID, $urlParams);

try {
/** @var IEventDispatcher $eventDispatcher */
$eventDispatcher = $this->getContainer()->getServer()->query(IEventDispatcher::class);
if (class_exists(LoadViewer::class)) {
$eventDispatcher->addListener(LoadViewer::class, function () {
\OCP\Util::addScript('officeonline', 'viewer');
});
}
} catch (QueryException $e) {
}

$this->getContainer()->registerCapability(Capabilities::class);
$this->getContainer()->registerMiddleWare(WOPIMiddleware::class);
}

public function boot(IBootContext $context): void {
// Check if the app is enabled for the current user
$currentUser = \OC::$server->getUserSession()->getUser();
if ($currentUser !== null) {
/** @var PermissionManager $permissionManager */
$permissionManager = \OC::$server->query(PermissionManager::class);
if (!$permissionManager->isEnabledForUser($currentUser)) {
return;
}
}

// Add to LoadViewer if loaded
try {
/** @var IEventDispatcher $eventDispatcher */
$eventDispatcher = $this->getContainer()->getServer()->query(IEventDispatcher::class);
if (class_exists(LoadViewer::class)) {
$eventDispatcher->addListener(LoadViewer::class, function () {
\OCP\Util::addScript('officeonline', 'viewer');
});
}
} catch (QueryException $e) {
}

// Register components
$this->getContainer()->registerCapability(Capabilities::class);
$this->getContainer()->registerMiddleWare(WOPIMiddleware::class);

$context->injectFn([$this, 'registerProvider']);
$context->injectFn([$this, 'updateCSP']);
}

public function registerProvider() {
$container = $this->getContainer();

Expand Down
43 changes: 43 additions & 0 deletions lib/Listener/LoadOfficeOnlineFilesActions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023, Patrik Kernstock <patrik@kernstock.net>
*
* @author Patrik Kernstock <patrik@kernstock.net>
*
* @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 OCA\Officeonline\Listener;

use OCA\Files\Event\LoadAdditionalScriptsEvent;
use OCA\Officeonline\AppInfo\Application;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Util;

class LoadOfficeOnlineFilesActions implements IEventListener {
public function handle(Event $event): void {
if (!($event instanceof LoadAdditionalScriptsEvent)) {
return;
}

Util::addScript(Application::APP_ID, 'files', 'viewer');
}
}
43 changes: 43 additions & 0 deletions lib/Listener/LoadOfficeOnlineFilesSharingActions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2023, Patrik Kernstock <patrik@kernstock.net>
*
* @author Patrik Kernstock <patrik@kernstock.net>
*
* @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 OCA\Officeonline\Listener;

use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent;
use OCA\Officeonline\AppInfo\Application;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Util;

class LoadOfficeOnlineFilesSharingActions implements IEventListener {
public function handle(Event $event): void {
if (!($event instanceof BeforeTemplateRenderedEvent)) {
return;
}

Util::addScript(Application::APP_ID, 'files', 'viewer');
}
}

0 comments on commit dd0f0a4

Please sign in to comment.