Skip to content

Commit 17bf174

Browse files
committed
Add new LoadAdditionalScriptsEvent
This adds a new event that is in the new style of event dispatching. This should allow more lazy loading and better sepeartion of concerns Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
1 parent bc40916 commit 17bf174

File tree

6 files changed

+116
-10
lines changed

6 files changed

+116
-10
lines changed

apps/files/composer/composer/autoload_classmap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
'OCA\\Files\\Controller\\AjaxController' => $baseDir . '/../lib/Controller/AjaxController.php',
3333
'OCA\\Files\\Controller\\ApiController' => $baseDir . '/../lib/Controller/ApiController.php',
3434
'OCA\\Files\\Controller\\ViewController' => $baseDir . '/../lib/Controller/ViewController.php',
35+
'OCA\\Files\\Event\\LoadAdditionalScriptsEvent' => $baseDir . '/../lib/Event/LoadAdditionalScriptsEvent.php',
3536
'OCA\\Files\\Helper' => $baseDir . '/../lib/Helper.php',
37+
'OCA\\Files\\Listener\\LegacyLoadAdditionalScripts' => $baseDir . '/../lib/Listener/LegacyLoadAdditionalScripts.php',
3638
'OCA\\Files\\Service\\TagService' => $baseDir . '/../lib/Service/TagService.php',
3739
);

apps/files/composer/composer/autoload_static.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ class ComposerStaticInitFiles
4747
'OCA\\Files\\Controller\\AjaxController' => __DIR__ . '/..' . '/../lib/Controller/AjaxController.php',
4848
'OCA\\Files\\Controller\\ApiController' => __DIR__ . '/..' . '/../lib/Controller/ApiController.php',
4949
'OCA\\Files\\Controller\\ViewController' => __DIR__ . '/..' . '/../lib/Controller/ViewController.php',
50+
'OCA\\Files\\Event\\LoadAdditionalScriptsEvent' => __DIR__ . '/..' . '/../lib/Event/LoadAdditionalScriptsEvent.php',
5051
'OCA\\Files\\Helper' => __DIR__ . '/..' . '/../lib/Helper.php',
52+
'OCA\\Files\\Listener\\LegacyLoadAdditionalScripts' => __DIR__ . '/..' . '/../lib/Listener/LegacyLoadAdditionalScripts.php',
5153
'OCA\\Files\\Service\\TagService' => __DIR__ . '/..' . '/../lib/Service/TagService.php',
5254
);
5355

apps/files/lib/AppInfo/Application.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@
3030
use OCA\Files\Collaboration\Resources\Listener;
3131
use OCA\Files\Collaboration\Resources\ResourceProvider;
3232
use OCA\Files\Controller\ApiController;
33+
use OCA\Files\Event\LoadAdditionalScriptsEvent;
34+
use OCA\Files\Listener\LegacyLoadAdditionalScripts;
3335
use OCP\AppFramework\App;
3436
use \OCA\Files\Service\TagService;
3537
use OCP\Collaboration\Resources\IManager;
38+
use OCP\EventDispatcher\IEventDispatcher;
3639
use \OCP\IContainer;
3740
use OCA\Files\Controller\ViewController;
3841
use OCA\Files\Capabilities;
@@ -66,7 +69,7 @@ public function __construct(array $urlParams=array()) {
6669
$server->getURLGenerator(),
6770
$c->query('L10N'),
6871
$server->getConfig(),
69-
$server->getEventDispatcher(),
72+
$server->query(IEventDispatcher::class),
7073
$server->getUserSession(),
7174
$server->getAppManager(),
7275
$server->getRootFolder(),
@@ -110,5 +113,9 @@ public function __construct(array $urlParams=array()) {
110113
$resourceManager = $container->query(IManager::class);
111114
$resourceManager->registerResourceProvider(ResourceProvider::class);
112115
Listener::register($server->getEventDispatcher());
116+
117+
/** @var IEventDispatcher $dispatcher */
118+
$dispatcher = $container->query(IEventDispatcher::class);
119+
$dispatcher->addServiceListener(LoadAdditionalScriptsEvent::class, LegacyLoadAdditionalScripts::class);
113120
}
114121
}

apps/files/lib/Controller/ViewController.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,14 @@
2929
namespace OCA\Files\Controller;
3030

3131
use OCA\Files\Activity\Helper;
32+
use OCA\Files\Event\LoadAdditionalScriptsEvent;
3233
use OCP\AppFramework\Controller;
3334
use OCP\AppFramework\Http\ContentSecurityPolicy;
34-
use OCP\AppFramework\Http\NotFoundResponse;
3535
use OCP\AppFramework\Http\RedirectResponse;
3636
use OCP\AppFramework\Http\Response;
3737
use OCP\AppFramework\Http\TemplateResponse;
3838
use OCP\App\IAppManager;
39+
use OCP\EventDispatcher\IEventDispatcher;
3940
use OCP\Files\Folder;
4041
use OCP\Files\IRootFolder;
4142
use OCP\Files\NotFoundException;
@@ -44,8 +45,6 @@
4445
use OCP\IRequest;
4546
use OCP\IURLGenerator;
4647
use OCP\IUserSession;
47-
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
48-
use Symfony\Component\EventDispatcher\GenericEvent;
4948

5049
/**
5150
* Class ViewController
@@ -63,7 +62,7 @@ class ViewController extends Controller {
6362
protected $l10n;
6463
/** @var IConfig */
6564
protected $config;
66-
/** @var EventDispatcherInterface */
65+
/** @var IEventDispatcher */
6766
protected $eventDispatcher;
6867
/** @var IUserSession */
6968
protected $userSession;
@@ -79,7 +78,7 @@ public function __construct(string $appName,
7978
IURLGenerator $urlGenerator,
8079
IL10N $l10n,
8180
IConfig $config,
82-
EventDispatcherInterface $eventDispatcherInterface,
81+
IEventDispatcher $eventDispatcher,
8382
IUserSession $userSession,
8483
IAppManager $appManager,
8584
IRootFolder $rootFolder,
@@ -91,7 +90,7 @@ public function __construct(string $appName,
9190
$this->urlGenerator = $urlGenerator;
9291
$this->l10n = $l10n;
9392
$this->config = $config;
94-
$this->eventDispatcher = $eventDispatcherInterface;
93+
$this->eventDispatcher = $eventDispatcher;
9594
$this->userSession = $userSession;
9695
$this->appManager = $appManager;
9796
$this->rootFolder = $rootFolder;
@@ -267,8 +266,8 @@ public function index($dir = '', $view = '', $fileid = null, $fileNotFound = fal
267266
];
268267
}
269268

270-
$event = new GenericEvent(null, ['hiddenFields' => []]);
271-
$this->eventDispatcher->dispatch('OCA\Files::loadAdditionalScripts', $event);
269+
$event = new LoadAdditionalScriptsEvent();
270+
$this->eventDispatcher->dispatch(LoadAdditionalScriptsEvent::class, $event);
272271

273272
$params = [];
274273
$params['usedSpacePercent'] = (int) $storageInfo['relative'];
@@ -285,7 +284,7 @@ public function index($dir = '', $view = '', $fileid = null, $fileNotFound = fal
285284
$params['fileNotFound'] = $fileNotFound ? 1 : 0;
286285
$params['appNavigation'] = $nav;
287286
$params['appContents'] = $contentItems;
288-
$params['hiddenFields'] = $event->getArgument('hiddenFields');
287+
$params['hiddenFields'] = $event->getHiddenFields();
289288

290289
$response = new TemplateResponse(
291290
$this->appName,
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
declare(strict_types=1);
3+
/**
4+
* @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
5+
*
6+
* @author Roeland Jago Douma <roeland@famdouma.nl>
7+
*
8+
* @license GNU AGPL version 3 or any later version
9+
*
10+
* This program is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU Affero General Public License as
12+
* published by the Free Software Foundation, either version 3 of the
13+
* License, or (at your option) any later version.
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU Affero General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU Affero General Public License
21+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
*
23+
*/
24+
25+
namespace OCA\Files\Event;
26+
27+
use OCP\EventDispatcher\Event;
28+
29+
class LoadAdditionalScriptsEvent extends Event {
30+
31+
private $hiddenFields = [];
32+
33+
public function addHiddenField(string $name, string $value): void {
34+
$this->hiddenFields[$name] = $value;
35+
}
36+
37+
public function getHiddenFields(): array {
38+
return $this->hiddenFields;
39+
}
40+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
declare(strict_types=1);
3+
/**
4+
* @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
5+
*
6+
* @author Roeland Jago Douma <roeland@famdouma.nl>
7+
*
8+
* @license GNU AGPL version 3 or any later version
9+
*
10+
* This program is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU Affero General Public License as
12+
* published by the Free Software Foundation, either version 3 of the
13+
* License, or (at your option) any later version.
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU Affero General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU Affero General Public License
21+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
*
23+
*/
24+
25+
namespace OCA\Files\Listener;
26+
27+
use OCA\Files\Event\LoadAdditionalScriptsEvent;
28+
use OCP\EventDispatcher\Event;
29+
use OCP\EventDispatcher\IEventListener;
30+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
31+
use Symfony\Component\EventDispatcher\GenericEvent;
32+
33+
class LegacyLoadAdditionalScripts implements IEventListener {
34+
35+
/** @var EventDispatcherInterface */
36+
private $legacyDispatcher;
37+
38+
public function __construct(EventDispatcherInterface $legacyDispatcher) {
39+
$this->legacyDispatcher = $legacyDispatcher;
40+
}
41+
42+
public function handle(Event $event): void {
43+
if (!($event instanceof LoadAdditionalScriptsEvent)) {
44+
return;
45+
}
46+
47+
$legacyEvent = new GenericEvent(null, ['hiddenFields' => []]);
48+
$this->legacyDispatcher->dispatch('OCA\Files::loadAdditionalScripts', $legacyEvent);
49+
50+
$hiddenFields = $legacyEvent->getArgument('hiddenFields');
51+
foreach ($hiddenFields as $name => $value) {
52+
$event->addHiddenField($name, $value);
53+
}
54+
}
55+
56+
}

0 commit comments

Comments
 (0)