Skip to content

Commit 43d7ab6

Browse files
committed
refactor: migrate OC_EventSource to dependency injection
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
1 parent 1a3bb23 commit 43d7ab6

File tree

10 files changed

+144
-28
lines changed

10 files changed

+144
-28
lines changed

core/ajax/update.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use OCP\EventDispatcher\Event;
3434
use OCP\EventDispatcher\IEventDispatcher;
3535
use OCP\IEventSource;
36+
use OCP\IEventSourceFactory;
3637
use OCP\IL10N;
3738
use OCP\ILogger;
3839
use OC\DB\MigratorExecuteSqlEvent;
@@ -43,16 +44,18 @@
4344
use OC\Repair\Events\RepairStartEvent;
4445
use OC\Repair\Events\RepairStepEvent;
4546
use OC\Repair\Events\RepairWarningEvent;
47+
use OCP\L10N\IFactory;
4648

4749
if (strpos(@ini_get('disable_functions'), 'set_time_limit') === false) {
4850
@set_time_limit(0);
4951
}
5052

5153
require_once '../../lib/base.php';
5254

53-
$l = \OC::$server->getL10N('core');
55+
/** @var \OCP\IL10N $l */
56+
$l = \OC::$server->get(IFactory::class)->get('core');
5457

55-
$eventSource = \OC::$server->createEventSource();
58+
$eventSource = \OC::$server->get(IEventSourceFactory::class)->create();
5659
// need to send an initial message to force-init the event source,
5760
// which will then trigger its own CSRF check and produces its own CSRF error
5861
// message

lib/composer/composer/autoload_classmap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,7 @@
453453
'OCP\\IDateTimeZone' => $baseDir . '/lib/public/IDateTimeZone.php',
454454
'OCP\\IEmojiHelper' => $baseDir . '/lib/public/IEmojiHelper.php',
455455
'OCP\\IEventSource' => $baseDir . '/lib/public/IEventSource.php',
456+
'OCP\\IEventSourceFactory' => $baseDir . '/lib/public/IEventSourceFactory.php',
456457
'OCP\\IGroup' => $baseDir . '/lib/public/IGroup.php',
457458
'OCP\\IGroupManager' => $baseDir . '/lib/public/IGroupManager.php',
458459
'OCP\\IImage' => $baseDir . '/lib/public/IImage.php',
@@ -1196,6 +1197,7 @@
11961197
'OC\\EventDispatcher\\GenericEventWrapper' => $baseDir . '/lib/private/EventDispatcher/GenericEventWrapper.php',
11971198
'OC\\EventDispatcher\\ServiceEventListener' => $baseDir . '/lib/private/EventDispatcher/ServiceEventListener.php',
11981199
'OC\\EventDispatcher\\SymfonyAdapter' => $baseDir . '/lib/private/EventDispatcher/SymfonyAdapter.php',
1200+
'OC\\EventSourceFactory' => $baseDir . '/lib/private/EventSourceFactory.php',
11991201
'OC\\Federation\\CloudFederationFactory' => $baseDir . '/lib/private/Federation/CloudFederationFactory.php',
12001202
'OC\\Federation\\CloudFederationNotification' => $baseDir . '/lib/private/Federation/CloudFederationNotification.php',
12011203
'OC\\Federation\\CloudFederationProviderManager' => $baseDir . '/lib/private/Federation/CloudFederationProviderManager.php',

lib/composer/composer/autoload_static.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
486486
'OCP\\IDateTimeZone' => __DIR__ . '/../../..' . '/lib/public/IDateTimeZone.php',
487487
'OCP\\IEmojiHelper' => __DIR__ . '/../../..' . '/lib/public/IEmojiHelper.php',
488488
'OCP\\IEventSource' => __DIR__ . '/../../..' . '/lib/public/IEventSource.php',
489+
'OCP\\IEventSourceFactory' => __DIR__ . '/../../..' . '/lib/public/IEventSourceFactory.php',
489490
'OCP\\IGroup' => __DIR__ . '/../../..' . '/lib/public/IGroup.php',
490491
'OCP\\IGroupManager' => __DIR__ . '/../../..' . '/lib/public/IGroupManager.php',
491492
'OCP\\IImage' => __DIR__ . '/../../..' . '/lib/public/IImage.php',
@@ -1229,6 +1230,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
12291230
'OC\\EventDispatcher\\GenericEventWrapper' => __DIR__ . '/../../..' . '/lib/private/EventDispatcher/GenericEventWrapper.php',
12301231
'OC\\EventDispatcher\\ServiceEventListener' => __DIR__ . '/../../..' . '/lib/private/EventDispatcher/ServiceEventListener.php',
12311232
'OC\\EventDispatcher\\SymfonyAdapter' => __DIR__ . '/../../..' . '/lib/private/EventDispatcher/SymfonyAdapter.php',
1233+
'OC\\EventSourceFactory' => __DIR__ . '/../../..' . '/lib/private/EventSourceFactory.php',
12321234
'OC\\Federation\\CloudFederationFactory' => __DIR__ . '/../../..' . '/lib/private/Federation/CloudFederationFactory.php',
12331235
'OC\\Federation\\CloudFederationNotification' => __DIR__ . '/../../..' . '/lib/private/Federation/CloudFederationNotification.php',
12341236
'OC\\Federation\\CloudFederationProviderManager' => __DIR__ . '/../../..' . '/lib/private/Federation/CloudFederationProviderManager.php',

lib/private/EventSourceFactory.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2023 Daniel Kesselberg <mail@danielkesselberg.de>
4+
*
5+
* @author Daniel Kesselberg <mail@danielkesselberg.de>
6+
*
7+
* @license AGPL-3.0-or-later
8+
*
9+
* This code is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License, version 3,
11+
* as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License, version 3,
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>
20+
*
21+
*/
22+
23+
namespace OC;
24+
25+
use OCP\IEventSource;
26+
use OCP\IEventSourceFactory;
27+
use OCP\IRequest;
28+
29+
class EventSourceFactory implements IEventSourceFactory {
30+
private IRequest $request;
31+
32+
33+
public function __construct(IRequest $request) {
34+
$this->request = $request;
35+
}
36+
37+
/**
38+
* Create a new event source
39+
*
40+
* @return IEventSource
41+
* @since 28.0.0
42+
*/
43+
public function create(): IEventSource {
44+
return new \OC_EventSource($this->request);
45+
}
46+
}

lib/private/Server.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@
211211
use OCP\IDateTimeFormatter;
212212
use OCP\IDateTimeZone;
213213
use OCP\IDBConnection;
214+
use OCP\IEventSourceFactory;
214215
use OCP\IGroupManager;
215216
use OCP\IInitialStateService;
216217
use OCP\IL10N;
@@ -1467,6 +1468,8 @@ public function __construct($webRoot, \OC\Config $config) {
14671468

14681469
$this->registerAlias(ISpeechToTextManager::class, SpeechToTextManager::class);
14691470

1471+
$this->registerAlias(IEventSourceFactory::class, EventSourceFactory::class);
1472+
14701473
$this->connectDispatcher();
14711474
}
14721475

@@ -1928,16 +1931,6 @@ public function getHTTPClientService() {
19281931
return $this->get(IClientService::class);
19291932
}
19301933

1931-
/**
1932-
* Create a new event source
1933-
*
1934-
* @return \OCP\IEventSource
1935-
* @deprecated 20.0.0
1936-
*/
1937-
public function createEventSource() {
1938-
return new \OC_EventSource();
1939-
}
1940-
19411934
/**
19421935
* Get the active event logger
19431936
*

lib/private/legacy/OC_EventSource.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
<?php
2+
3+
use OCP\IRequest;
4+
25
/**
36
* @copyright Copyright (c) 2016, ownCloud, Inc.
47
*
@@ -42,6 +45,12 @@ class OC_EventSource implements \OCP\IEventSource {
4245
*/
4346
private $started = false;
4447

48+
private IRequest $request;
49+
50+
public function __construct(IRequest $request) {
51+
$this->request = $request;
52+
}
53+
4554
protected function init() {
4655
if ($this->started) {
4756
return;
@@ -71,11 +80,11 @@ protected function init() {
7180
} else {
7281
header("Content-Type: text/event-stream");
7382
}
74-
if (!\OC::$server->getRequest()->passesStrictCookieCheck()) {
83+
if (!$this->request->passesStrictCookieCheck()) {
7584
header('Location: '.\OC::$WEBROOT);
7685
exit();
7786
}
78-
if (!\OC::$server->getRequest()->passesCSRFCheck()) {
87+
if (!$this->request->passesCSRFCheck()) {
7988
$this->send('error', 'Possible CSRF attack. Connection will be closed.');
8089
$this->close();
8190
exit();

lib/public/IEventSourceFactory.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* @copyright Copyright (c) 2023 Daniel Kesselberg <mail@danielkesselberg.de>
7+
*
8+
* @author Daniel Kesselberg <mail@danielkesselberg.de>
9+
*
10+
* @license AGPL-3.0-or-later
11+
*
12+
* This code is free software: you can redistribute it and/or modify
13+
* it under the terms of the GNU Affero General Public License, version 3,
14+
* as published by the Free Software Foundation.
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, version 3,
22+
* along with this program. If not, see <http://www.gnu.org/licenses/>
23+
*
24+
*/
25+
namespace OCP;
26+
27+
/**
28+
* @since 28.0.0
29+
*/
30+
interface IEventSourceFactory {
31+
/**
32+
* Create a new event source
33+
*
34+
* @return IEventSource
35+
* @since 28.0.0
36+
*/
37+
public function create(): IEventSource;
38+
}

lib/public/IServerContainer.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -395,15 +395,6 @@ public function getSearch();
395395
*/
396396
public function getCertificateManager();
397397

398-
/**
399-
* Create a new event source
400-
*
401-
* @return \OCP\IEventSource
402-
* @since 8.0.0
403-
* @deprecated 20.0.0 have it injected or fetch it through \Psr\Container\ContainerInterface::get
404-
*/
405-
public function createEventSource();
406-
407398
/**
408399
* Returns an instance of the HTTP client service
409400
*
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2023 Daniel Kesselberg <mail@danielkesselberg.de>
4+
*
5+
* @author Daniel Kesselberg <mail@danielkesselberg.de>
6+
*
7+
* @license AGPL-3.0-or-later
8+
*
9+
* This code is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License, version 3,
11+
* as published by the Free Software Foundation.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU Affero General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Affero General Public License, version 3,
19+
* along with this program. If not, see <http://www.gnu.org/licenses/>
20+
*
21+
*/
22+
23+
namespace Test;
24+
25+
use OC\EventSourceFactory;
26+
use OCP\IEventSource;
27+
use OCP\IRequest;
28+
29+
class EventSourceFactoryTest extends TestCase {
30+
public function testCreate(): void {
31+
$request = $this->createMock(IRequest::class);
32+
$factory = new EventSourceFactory($request);
33+
34+
$instance = $factory->create();
35+
$this->assertInstanceOf(IEventSource::class, $instance);
36+
}
37+
}

tests/lib/ServerTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,6 @@ public function testGetCertificateManager() {
179179
$this->assertInstanceOf('\OCP\ICertificateManager', $this->server->getCertificateManager(), 'service returned by "getCertificateManager" did not return the right class');
180180
}
181181

182-
public function testCreateEventSource() {
183-
$this->assertInstanceOf('\OC_EventSource', $this->server->createEventSource(), 'service returned by "createEventSource" did not return the right class');
184-
$this->assertInstanceOf('\OCP\IEventSource', $this->server->createEventSource(), 'service returned by "createEventSource" did not return the right class');
185-
}
186-
187182
public function testOverwriteDefaultCommentsManager() {
188183
$config = $this->server->getConfig();
189184
$defaultManagerFactory = $config->getSystemValue('comments.managerFactory', '\OC\Comments\ManagerFactory');

0 commit comments

Comments
 (0)