Skip to content

Commit d182037

Browse files
committed
Emit to load additionalscripts
Fixes #13662 This will fire of an event after a Template Response has been returned. There is an event for the generic loading and one when logged in. So apps can chose to load only on loged in pages. This is a more generic approach than the files app event. As some things we might want to load on other pages as well besides the files app. Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
1 parent 87c8a71 commit d182037

6 files changed

Lines changed: 172 additions & 0 deletions

File tree

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@
419419
'OC\\AppFramework\\Http\\Output' => $baseDir . '/lib/private/AppFramework/Http/Output.php',
420420
'OC\\AppFramework\\Http\\Request' => $baseDir . '/lib/private/AppFramework/Http/Request.php',
421421
'OC\\AppFramework\\Logger' => $baseDir . '/lib/private/AppFramework/Logger.php',
422+
'OC\\AppFramework\\Middleware\\AdditionalScriptsMiddleware' => $baseDir . '/lib/private/AppFramework/Middleware/AdditionalScriptsMiddleware.php',
422423
'OC\\AppFramework\\Middleware\\MiddlewareDispatcher' => $baseDir . '/lib/private/AppFramework/Middleware/MiddlewareDispatcher.php',
423424
'OC\\AppFramework\\Middleware\\OCSMiddleware' => $baseDir . '/lib/private/AppFramework/Middleware/OCSMiddleware.php',
424425
'OC\\AppFramework\\Middleware\\PublicShare\\Exceptions\\NeedAuthenticationException' => $baseDir . '/lib/private/AppFramework/Middleware/PublicShare/Exceptions/NeedAuthenticationException.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c
449449
'OC\\AppFramework\\Http\\Output' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Http/Output.php',
450450
'OC\\AppFramework\\Http\\Request' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Http/Request.php',
451451
'OC\\AppFramework\\Logger' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Logger.php',
452+
'OC\\AppFramework\\Middleware\\AdditionalScriptsMiddleware' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Middleware/AdditionalScriptsMiddleware.php',
452453
'OC\\AppFramework\\Middleware\\MiddlewareDispatcher' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Middleware/MiddlewareDispatcher.php',
453454
'OC\\AppFramework\\Middleware\\OCSMiddleware' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Middleware/OCSMiddleware.php',
454455
'OC\\AppFramework\\Middleware\\PublicShare\\Exceptions\\NeedAuthenticationException' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Middleware/PublicShare/Exceptions/NeedAuthenticationException.php',

lib/private/AppFramework/DependencyInjection/DIContainer.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,9 @@ public function __construct($appName, $urlParams = array(), ServerContainer $ser
261261
$c->query(\OCP\IConfig::class)
262262
)
263263
);
264+
$dispatcher->registerMiddleware(
265+
$c->query(\OC\AppFramework\Middleware\AdditionalScriptsMiddleware::class)
266+
);
264267

265268
foreach($this->middleWares as $middleWare) {
266269
$dispatcher->registerMiddleware($c[$middleWare]);
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 OC\AppFramework\Middleware;
26+
27+
use OCP\AppFramework\Http\Response;
28+
use OCP\AppFramework\Http\TemplateResponse;
29+
use OCP\AppFramework\Middleware;
30+
use OCP\IUserSession;
31+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
32+
33+
class AdditionalScriptsMiddleware extends Middleware {
34+
/** @var EventDispatcherInterface */
35+
private $dispatcher;
36+
/** @var IUserSession */
37+
private $userSession;
38+
39+
public function __construct(EventDispatcherInterface $dispatcher, IUserSession $userSession) {
40+
$this->dispatcher = $dispatcher;
41+
$this->userSession = $userSession;
42+
}
43+
44+
public function afterController($controller, $methodName, Response $response): Response {
45+
if ($response instanceof TemplateResponse) {
46+
$this->dispatcher->dispatch(TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS);
47+
48+
if ($this->userSession->isLoggedIn()) {
49+
$this->dispatcher->dispatch(TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS_LOGGEDIN);
50+
}
51+
}
52+
53+
return $response;
54+
}
55+
56+
}

lib/public/AppFramework/Http/TemplateResponse.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
*/
3838
class TemplateResponse extends Response {
3939

40+
const EVENT_LOAD_ADDITIONAL_SCRIPTS = self::class . '::loadAdditionalScripts';
41+
const EVENT_LOAD_ADDITIONAL_SCRIPTS_LOGGEDIN = self::class . '::loadAdditionalScriptsLoggedIn';
42+
4043
/**
4144
* name of the template
4245
* @var string
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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 Test\AppFramework\Middleware;
26+
27+
use OC\AppFramework\Middleware\AdditionalScriptsMiddleware;
28+
use OCP\AppFramework\Controller;
29+
use OCP\AppFramework\Http\Response;
30+
use OCP\AppFramework\Http\TemplateResponse;
31+
use OCP\IUserSession;
32+
use PHPUnit\Framework\MockObject\MockObject;
33+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
34+
35+
class AdditionalScriptsMiddlewareTest extends \Test\TestCase {
36+
37+
/** @var EventDispatcherInterface|MockObject */
38+
private $dispatcher;
39+
/** @var IUserSession|MockObject */
40+
private $userSession;
41+
42+
/** @var Controller */
43+
private $controller;
44+
45+
/** @var AdditionalScriptsMiddleware */
46+
private $middleWare;
47+
48+
public function setUp() {
49+
parent::setUp();
50+
51+
$this->dispatcher = $this->createMock(EventDispatcherInterface::class);
52+
$this->userSession = $this->createMock(IUserSession::class);
53+
$this->middleWare = new AdditionalScriptsMiddleware(
54+
$this->dispatcher,
55+
$this->userSession
56+
);
57+
58+
$this->controller = $this->createMock(Controller::class);
59+
}
60+
61+
public function testNoTemplateResponse() {
62+
$this->dispatcher->expects($this->never())
63+
->method($this->anything());
64+
$this->userSession->expects($this->never())
65+
->method($this->anything());
66+
67+
$this->middleWare->afterController($this->controller, 'myMethod', $this->createMock(Response::class));
68+
}
69+
70+
public function testTemplateResponseNotLoggedIn() {
71+
$this->dispatcher->expects($this->once())
72+
->method('dispatch')
73+
->willReturnCallback(function($eventName) {
74+
if ($eventName === TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS) {
75+
return;
76+
}
77+
78+
$this->fail('Wrong event dispatched');
79+
});
80+
$this->userSession->method('isLoggedIn')
81+
->willReturn(false);
82+
83+
$this->middleWare->afterController($this->controller, 'myMethod', $this->createMock(TemplateResponse::class));
84+
}
85+
86+
public function testTemplateResponseLoggedIn() {
87+
$events = [];
88+
89+
$this->dispatcher->expects($this->exactly(2))
90+
->method('dispatch')
91+
->willReturnCallback(function($eventName) use (&$events) {
92+
if ($eventName === TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS ||
93+
$eventName === TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS_LOGGEDIN) {
94+
$events[] = $eventName;
95+
return;
96+
}
97+
98+
$this->fail('Wrong event dispatched');
99+
});
100+
$this->userSession->method('isLoggedIn')
101+
->willReturn(true);
102+
103+
$this->middleWare->afterController($this->controller, 'myMethod', $this->createMock(TemplateResponse::class));
104+
105+
$this->assertContains(TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS, $events);
106+
$this->assertContains(TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS_LOGGEDIN, $events);
107+
}
108+
}

0 commit comments

Comments
 (0)