Skip to content

Commit 08fd49b

Browse files
committed
refactor: migrate OC_EventSource to dependency injection
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
1 parent 3e293b7 commit 08fd49b

File tree

5 files changed

+14
-27
lines changed

5 files changed

+14
-27
lines changed

core/ajax/update.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252

5353
$l = \OC::$server->getL10N('core');
5454

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

lib/private/Server.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,6 +1467,8 @@ public function __construct($webRoot, \OC\Config $config) {
14671467

14681468
$this->registerAlias(ISpeechToTextManager::class, SpeechToTextManager::class);
14691469

1470+
$this->registerAlias(\OCP\IEventSource::class, \OC_EventSource::class);
1471+
14701472
$this->connectDispatcher();
14711473
}
14721474

@@ -1928,16 +1930,6 @@ public function getHTTPClientService() {
19281930
return $this->get(IClientService::class);
19291931
}
19301932

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-
19411933
/**
19421934
* Get the active event logger
19431935
*

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/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
*

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)