Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate from app.php to IBootstrap #462

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)) {
juliushaertl marked this conversation as resolved.
Show resolved Hide resolved
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');
}
}
Loading