Skip to content

Commit 79521ab

Browse files
authored
Merge pull request #5717 from nextcloud/perf/lazy-template-creator
perf(bootstrap): Lazy register template creator through event
2 parents d8bafac + e0a2b99 commit 79521ab

File tree

4 files changed

+69
-17
lines changed

4 files changed

+69
-17
lines changed

composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
'OCA\\Text\\Listeners\\LoadViewerListener' => $baseDir . '/../lib/Listeners/LoadViewerListener.php',
4747
'OCA\\Text\\Listeners\\NodeCopiedListener' => $baseDir . '/../lib/Listeners/NodeCopiedListener.php',
4848
'OCA\\Text\\Listeners\\RegisterDirectEditorEventListener' => $baseDir . '/../lib/Listeners/RegisterDirectEditorEventListener.php',
49+
'OCA\\Text\\Listeners\\RegisterTemplateCreatorListener' => $baseDir . '/../lib/Listeners/RegisterTemplateCreatorListener.php',
4950
'OCA\\Text\\Middleware\\Attribute\\RequireDocumentBaseVersionEtag' => $baseDir . '/../lib/Middleware/Attribute/RequireDocumentBaseVersionEtag.php',
5051
'OCA\\Text\\Middleware\\Attribute\\RequireDocumentSession' => $baseDir . '/../lib/Middleware/Attribute/RequireDocumentSession.php',
5152
'OCA\\Text\\Middleware\\Attribute\\RequireDocumentSessionOrUserOrShareToken' => $baseDir . '/../lib/Middleware/Attribute/RequireDocumentSessionOrUserOrShareToken.php',

composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class ComposerStaticInitText
6161
'OCA\\Text\\Listeners\\LoadViewerListener' => __DIR__ . '/..' . '/../lib/Listeners/LoadViewerListener.php',
6262
'OCA\\Text\\Listeners\\NodeCopiedListener' => __DIR__ . '/..' . '/../lib/Listeners/NodeCopiedListener.php',
6363
'OCA\\Text\\Listeners\\RegisterDirectEditorEventListener' => __DIR__ . '/..' . '/../lib/Listeners/RegisterDirectEditorEventListener.php',
64+
'OCA\\Text\\Listeners\\RegisterTemplateCreatorListener' => __DIR__ . '/..' . '/../lib/Listeners/RegisterTemplateCreatorListener.php',
6465
'OCA\\Text\\Middleware\\Attribute\\RequireDocumentBaseVersionEtag' => __DIR__ . '/..' . '/../lib/Middleware/Attribute/RequireDocumentBaseVersionEtag.php',
6566
'OCA\\Text\\Middleware\\Attribute\\RequireDocumentSession' => __DIR__ . '/..' . '/../lib/Middleware/Attribute/RequireDocumentSession.php',
6667
'OCA\\Text\\Middleware\\Attribute\\RequireDocumentSessionOrUserOrShareToken' => __DIR__ . '/..' . '/../lib/Middleware/Attribute/RequireDocumentSessionOrUserOrShareToken.php',

lib/AppInfo/Application.php

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,11 @@
3939
use OCA\Text\Listeners\LoadViewerListener;
4040
use OCA\Text\Listeners\NodeCopiedListener;
4141
use OCA\Text\Listeners\RegisterDirectEditorEventListener;
42+
use OCA\Text\Listeners\RegisterTemplateCreatorListener;
4243
use OCA\Text\Middleware\SessionMiddleware;
4344
use OCA\Text\Notification\Notifier;
44-
use OCA\Text\Service\ConfigService;
4545
use OCA\TpAssistant\Event\BeforeAssistantNotificationEvent;
4646
use OCA\Viewer\Event\LoadViewer;
47-
use OCP\App\IAppManager;
4847
use OCP\AppFramework\App;
4948
use OCP\AppFramework\Bootstrap\IBootContext;
5049
use OCP\AppFramework\Bootstrap\IBootstrap;
@@ -55,9 +54,7 @@
5554
use OCP\Files\Events\Node\BeforeNodeRenamedEvent;
5655
use OCP\Files\Events\Node\BeforeNodeWrittenEvent;
5756
use OCP\Files\Events\Node\NodeCopiedEvent;
58-
use OCP\Files\Template\ITemplateManager;
59-
use OCP\Files\Template\TemplateFileCreator;
60-
use OCP\IL10N;
57+
use OCP\Files\Template\RegisterTemplateCreatorEvent;
6158

6259
class Application extends App implements IBootstrap {
6360
public const APP_NAME = 'text';
@@ -79,23 +76,12 @@ public function register(IRegistrationContext $context): void {
7976
$context->registerEventListener(BeforeNodeDeletedEvent::class, BeforeNodeDeletedListener::class);
8077
$context->registerEventListener(AddMissingIndicesEvent::class, AddMissingIndicesListener::class);
8178
$context->registerEventListener(BeforeAssistantNotificationEvent::class, BeforeAssistantNotificationListener::class);
79+
$context->registerEventListener(RegisterTemplateCreatorEvent::class, RegisterTemplateCreatorListener::class);
8280

8381
$context->registerNotifierService(Notifier::class);
8482
$context->registerMiddleware(SessionMiddleware::class);
8583
}
8684

8785
public function boot(IBootContext $context): void {
88-
$context->injectFn(function (ITemplateManager $templateManager, IL10N $l, ConfigService $configService, IAppManager $appManager) {
89-
$templateManager->registerTemplateFileCreator(function () use ($l, $configService, $appManager) {
90-
$markdownFile = new TemplateFileCreator(Application::APP_NAME, $l->t('New text file'), '.' . $configService->getDefaultFileExtension());
91-
$markdownFile->addMimetype('text/markdown');
92-
$markdownFile->addMimetype('text/plain');
93-
$markdownFile->setIconSvgInline(file_get_contents($appManager->getAppPath('text') . '/img/article.svg'));
94-
$markdownFile->setRatio(1);
95-
$markdownFile->setOrder(10);
96-
$markdownFile->setActionLabel($l->t('Create new text file'));
97-
return $markdownFile;
98-
});
99-
});
10086
}
10187
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* @copyright Copyright (c) 2024 Julius Härtl <jus@bitgrid.net>
6+
*
7+
* @author Julius Härtl <jus@bitgrid.net>
8+
*
9+
* @license GNU AGPL version 3 or any later version
10+
*
11+
* This program is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU Affero General Public License as
13+
* published by the Free Software Foundation, either version 3 of the
14+
* License, or (at your option) any later version.
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Affero General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Affero General Public License
22+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
*
24+
*/
25+
26+
namespace OCA\Text\Listeners;
27+
28+
use OCA\Text\AppInfo\Application;
29+
use OCA\Text\Service\ConfigService;
30+
use OCP\App\IAppManager;
31+
use OCP\EventDispatcher\Event;
32+
use OCP\EventDispatcher\IEventListener;
33+
use OCP\Files\Template\RegisterTemplateCreatorEvent;
34+
use OCP\Files\Template\TemplateFileCreator;
35+
use OCP\IL10N;
36+
37+
/** @implements IEventListener<Event|RegisterTemplateCreatorEvent> */
38+
class RegisterTemplateCreatorListener implements IEventListener {
39+
public function __construct(
40+
private IL10N $l10n,
41+
private ConfigService $configService,
42+
private IAppManager $appManager,
43+
) {
44+
}
45+
46+
public function handle(Event $event): void {
47+
if (!$event instanceof RegisterTemplateCreatorEvent) {
48+
return;
49+
}
50+
51+
$event->getTemplateManager()->registerTemplateFileCreator(function () {
52+
$markdownFile = new TemplateFileCreator(Application::APP_NAME, $this->l10n->t('New text file'), '.' . $this->configService->getDefaultFileExtension());
53+
$markdownFile->addMimetype('text/markdown');
54+
$markdownFile->addMimetype('text/plain');
55+
$markdownFile->setIconSvgInline(file_get_contents($this->appManager->getAppPath('text') . '/img/article.svg'));
56+
$markdownFile->setRatio(1);
57+
$markdownFile->setOrder(10);
58+
$markdownFile->setActionLabel($this->l10n->t('Create new text file'));
59+
return $markdownFile;
60+
});
61+
62+
63+
}
64+
}

0 commit comments

Comments
 (0)