Skip to content

Commit

Permalink
perf(bootstrap): Lazy register template creator through event
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Härtl <jus@bitgrid.net>
  • Loading branch information
juliushaertl committed Apr 22, 2024
1 parent 502805e commit 1891ab6
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 12 deletions.
1 change: 1 addition & 0 deletions composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
'OCA\\Text\\Listeners\\LoadViewerListener' => $baseDir . '/../lib/Listeners/LoadViewerListener.php',
'OCA\\Text\\Listeners\\NodeCopiedListener' => $baseDir . '/../lib/Listeners/NodeCopiedListener.php',
'OCA\\Text\\Listeners\\RegisterDirectEditorEventListener' => $baseDir . '/../lib/Listeners/RegisterDirectEditorEventListener.php',
'OCA\\Text\\Listeners\\RegisterTemplateCreatorListener' => $baseDir . '/../lib/Listeners/RegisterTemplateCreatorListener.php',
'OCA\\Text\\Middleware\\Attribute\\RequireDocumentBaseVersionEtag' => $baseDir . '/../lib/Middleware/Attribute/RequireDocumentBaseVersionEtag.php',
'OCA\\Text\\Middleware\\Attribute\\RequireDocumentSession' => $baseDir . '/../lib/Middleware/Attribute/RequireDocumentSession.php',
'OCA\\Text\\Middleware\\Attribute\\RequireDocumentSessionOrUserOrShareToken' => $baseDir . '/../lib/Middleware/Attribute/RequireDocumentSessionOrUserOrShareToken.php',
Expand Down
1 change: 1 addition & 0 deletions composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class ComposerStaticInitText
'OCA\\Text\\Listeners\\LoadViewerListener' => __DIR__ . '/..' . '/../lib/Listeners/LoadViewerListener.php',
'OCA\\Text\\Listeners\\NodeCopiedListener' => __DIR__ . '/..' . '/../lib/Listeners/NodeCopiedListener.php',
'OCA\\Text\\Listeners\\RegisterDirectEditorEventListener' => __DIR__ . '/..' . '/../lib/Listeners/RegisterDirectEditorEventListener.php',
'OCA\\Text\\Listeners\\RegisterTemplateCreatorListener' => __DIR__ . '/..' . '/../lib/Listeners/RegisterTemplateCreatorListener.php',
'OCA\\Text\\Middleware\\Attribute\\RequireDocumentBaseVersionEtag' => __DIR__ . '/..' . '/../lib/Middleware/Attribute/RequireDocumentBaseVersionEtag.php',
'OCA\\Text\\Middleware\\Attribute\\RequireDocumentSession' => __DIR__ . '/..' . '/../lib/Middleware/Attribute/RequireDocumentSession.php',
'OCA\\Text\\Middleware\\Attribute\\RequireDocumentSessionOrUserOrShareToken' => __DIR__ . '/..' . '/../lib/Middleware/Attribute/RequireDocumentSessionOrUserOrShareToken.php',
Expand Down
15 changes: 3 additions & 12 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use OCA\Text\Listeners\LoadViewerListener;
use OCA\Text\Listeners\NodeCopiedListener;
use OCA\Text\Listeners\RegisterDirectEditorEventListener;
use OCA\Text\Listeners\RegisterTemplateCreatorListener;
use OCA\Text\Middleware\SessionMiddleware;
use OCA\Text\Notification\Notifier;
use OCA\Text\Service\ConfigService;
Expand All @@ -56,6 +57,7 @@
use OCP\Files\Events\Node\BeforeNodeWrittenEvent;
use OCP\Files\Events\Node\NodeCopiedEvent;
use OCP\Files\Template\ITemplateManager;
use OCP\Files\Template\RegisterTemplateCreatorEvent;
use OCP\Files\Template\TemplateFileCreator;
use OCP\IL10N;

Expand All @@ -79,23 +81,12 @@ public function register(IRegistrationContext $context): void {
$context->registerEventListener(BeforeNodeDeletedEvent::class, BeforeNodeDeletedListener::class);
$context->registerEventListener(AddMissingIndicesEvent::class, AddMissingIndicesListener::class);
$context->registerEventListener(BeforeAssistantNotificationEvent::class, BeforeAssistantNotificationListener::class);
$context->registerEventListener(RegisterTemplateCreatorEvent::class, RegisterTemplateCreatorListener::class);

Check failure on line 84 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

UndefinedClass

lib/AppInfo/Application.php:84:35: UndefinedClass: Class, interface or enum named OCP\Files\Template\RegisterTemplateCreatorEvent does not exist (see https://psalm.dev/019)

Check failure on line 84 in lib/AppInfo/Application.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

InvalidArgument

lib/AppInfo/Application.php:84:72: InvalidArgument: Argument 2 of OCP\AppFramework\Bootstrap\IRegistrationContext::registerEventListener expects class-string<OCP\EventDispatcher\IEventListener<OCP\EventDispatcher\Event>>, but OCA\Text\Listeners\RegisterTemplateCreatorListener::class provided (see https://psalm.dev/004)

$context->registerNotifierService(Notifier::class);
$context->registerMiddleware(SessionMiddleware::class);
}

public function boot(IBootContext $context): void {
$context->injectFn(function (ITemplateManager $templateManager, IL10N $l, ConfigService $configService, IAppManager $appManager) {
$templateManager->registerTemplateFileCreator(function () use ($l, $configService, $appManager) {
$markdownFile = new TemplateFileCreator(Application::APP_NAME, $l->t('New text file'), '.' . $configService->getDefaultFileExtension());
$markdownFile->addMimetype('text/markdown');
$markdownFile->addMimetype('text/plain');
$markdownFile->setIconSvgInline(file_get_contents($appManager->getAppPath('text') . '/img/article.svg'));
$markdownFile->setRatio(1);
$markdownFile->setOrder(10);
$markdownFile->setActionLabel($l->t('Create new text file'));
return $markdownFile;
});
});
}
}
69 changes: 69 additions & 0 deletions lib/Listeners/RegisterTemplateCreatorListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);
/**
* @copyright Copyright (c) 2020 Morris Jobke <hey@morrisjobke.de>
*
* @author Morris Jobke <hey@morrisjobke.de>
*
* @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\Text\Listeners;

use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent;
use OCA\Richdocuments\Listener\RegisterTemplateFileCreatorListener;
use OCA\Text\AppInfo\Application;
use OCA\Text\Service\ConfigService;
use OCA\Text\Service\InitialStateProvider;
use OCP\App\IAppManager;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\Files\Template\RegisterTemplateCreatorEvent;
use OCP\Files\Template\TemplateFileCreator;
use OCP\IConfig;
use OCP\IL10N;
use OCP\Util;

/** @implements IEventListener<Event|RegisterTemplateCreatorEvent> */
class RegisterTemplateCreatorListener implements IEventListener {

Check failure on line 43 in lib/Listeners/RegisterTemplateCreatorListener.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

UndefinedDocblockClass

lib/Listeners/RegisterTemplateCreatorListener.php:43:7: UndefinedDocblockClass: Docblock-defined class, interface or enum named OCP\Files\Template\RegisterTemplateCreatorEvent does not exist (see https://psalm.dev/200)

Check failure on line 43 in lib/Listeners/RegisterTemplateCreatorListener.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

InvalidTemplateParam

lib/Listeners/RegisterTemplateCreatorListener.php:43:50: InvalidTemplateParam: Extended template param T expects type OCP\EventDispatcher\Event, type OCP\EventDispatcher\Event|OCP\Files\Template\RegisterTemplateCreatorEvent given (see https://psalm.dev/183)
public function __construct(
private IL10N $l10n,
private ConfigService $configService,
private IAppManager $appManager,
) {
}

public function handle(Event $event): void {

Check failure on line 51 in lib/Listeners/RegisterTemplateCreatorListener.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

MoreSpecificImplementedParamType

lib/Listeners/RegisterTemplateCreatorListener.php:51:31: MoreSpecificImplementedParamType: Argument 1 of OCA\Text\Listeners\RegisterTemplateCreatorListener::handle has the more specific type 'OCP\EventDispatcher\Event', expecting 'OCP\EventDispatcher\Event|OCP\Files\Template\RegisterTemplateCreatorEvent' as defined by OCP\EventDispatcher\IEventListener::handle (see https://psalm.dev/140)
if (!$event instanceof RegisterTemplateCreatorEvent) {

Check failure on line 52 in lib/Listeners/RegisterTemplateCreatorListener.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

UndefinedClass

lib/Listeners/RegisterTemplateCreatorListener.php:52:26: UndefinedClass: Class, interface or enum named OCP\Files\Template\RegisterTemplateCreatorEvent does not exist (see https://psalm.dev/019)
return;
}

$event->getTemplateManager()->registerTemplateFileCreator(function () {
$markdownFile = new TemplateFileCreator(Application::APP_NAME, $this->l10n->t('New text file'), '.' . $this->configService->getDefaultFileExtension());
$markdownFile->addMimetype('text/markdown');
$markdownFile->addMimetype('text/plain');
$markdownFile->setIconSvgInline(file_get_contents($this->appManager->getAppPath('text') . '/img/article.svg'));
$markdownFile->setRatio(1);
$markdownFile->setOrder(10);
$markdownFile->setActionLabel($this->l10n->t('Create new text file'));
return $markdownFile;
});


}
}

0 comments on commit 1891ab6

Please sign in to comment.