From a9f71315757a4c48f84cf5928022fa2fa105fad9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Thu, 16 May 2024 17:25:13 +0200 Subject: [PATCH 1/2] fix: Move OC_EventSource to OC namespace MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- lib/composer/composer/autoload_classmap.php | 2 +- lib/composer/composer/autoload_static.php | 2 +- .../OC_EventSource.php => EventSource.php} | 12 ++++++++---- lib/private/EventSourceFactory.php | 16 ++++------------ 4 files changed, 14 insertions(+), 18 deletions(-) rename lib/private/{legacy/OC_EventSource.php => EventSource.php} (97%) diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index cb57d44ea60..8e1408e121e 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -1382,6 +1382,7 @@ return array( 'OC\\Encryption\\Util' => $baseDir . '/lib/private/Encryption/Util.php', 'OC\\EventDispatcher\\EventDispatcher' => $baseDir . '/lib/private/EventDispatcher/EventDispatcher.php', 'OC\\EventDispatcher\\ServiceEventListener' => $baseDir . '/lib/private/EventDispatcher/ServiceEventListener.php', + 'OC\\EventSource' => $baseDir . '/lib/private/EventSource.php', 'OC\\EventSourceFactory' => $baseDir . '/lib/private/EventSourceFactory.php', 'OC\\Federation\\CloudFederationFactory' => $baseDir . '/lib/private/Federation/CloudFederationFactory.php', 'OC\\Federation\\CloudFederationNotification' => $baseDir . '/lib/private/Federation/CloudFederationNotification.php', @@ -1905,7 +1906,6 @@ return array( 'OC_API' => $baseDir . '/lib/private/legacy/OC_API.php', 'OC_App' => $baseDir . '/lib/private/legacy/OC_App.php', 'OC_Defaults' => $baseDir . '/lib/private/legacy/OC_Defaults.php', - 'OC_EventSource' => $baseDir . '/lib/private/legacy/OC_EventSource.php', 'OC_FileChunking' => $baseDir . '/lib/private/legacy/OC_FileChunking.php', 'OC_Files' => $baseDir . '/lib/private/legacy/OC_Files.php', 'OC_Helper' => $baseDir . '/lib/private/legacy/OC_Helper.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 0a4e9571049..d6939ae36ce 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -1423,6 +1423,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OC\\Encryption\\Util' => __DIR__ . '/../../..' . '/lib/private/Encryption/Util.php', 'OC\\EventDispatcher\\EventDispatcher' => __DIR__ . '/../../..' . '/lib/private/EventDispatcher/EventDispatcher.php', 'OC\\EventDispatcher\\ServiceEventListener' => __DIR__ . '/../../..' . '/lib/private/EventDispatcher/ServiceEventListener.php', + 'OC\\EventSource' => __DIR__ . '/../../..' . '/lib/private/EventSource.php', 'OC\\EventSourceFactory' => __DIR__ . '/../../..' . '/lib/private/EventSourceFactory.php', 'OC\\Federation\\CloudFederationFactory' => __DIR__ . '/../../..' . '/lib/private/Federation/CloudFederationFactory.php', 'OC\\Federation\\CloudFederationNotification' => __DIR__ . '/../../..' . '/lib/private/Federation/CloudFederationNotification.php', @@ -1946,7 +1947,6 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OC_API' => __DIR__ . '/../../..' . '/lib/private/legacy/OC_API.php', 'OC_App' => __DIR__ . '/../../..' . '/lib/private/legacy/OC_App.php', 'OC_Defaults' => __DIR__ . '/../../..' . '/lib/private/legacy/OC_Defaults.php', - 'OC_EventSource' => __DIR__ . '/../../..' . '/lib/private/legacy/OC_EventSource.php', 'OC_FileChunking' => __DIR__ . '/../../..' . '/lib/private/legacy/OC_FileChunking.php', 'OC_Files' => __DIR__ . '/../../..' . '/lib/private/legacy/OC_Files.php', 'OC_Helper' => __DIR__ . '/../../..' . '/lib/private/legacy/OC_Helper.php', diff --git a/lib/private/legacy/OC_EventSource.php b/lib/private/EventSource.php similarity index 97% rename from lib/private/legacy/OC_EventSource.php rename to lib/private/EventSource.php index 49fde4a214f..9a62d13a938 100644 --- a/lib/private/legacy/OC_EventSource.php +++ b/lib/private/EventSource.php @@ -1,7 +1,5 @@ * */ -class OC_EventSource implements \OCP\IEventSource { + +namespace OC; + +use OCP\IEventSource; +use OCP\IRequest; + +class EventSource implements IEventSource { /** * @var bool */ @@ -58,7 +62,7 @@ class OC_EventSource implements \OCP\IEventSource { $this->started = true; // prevent php output buffering, caching and nginx buffering - OC_Util::obEnd(); + \OC_Util::obEnd(); header('Cache-Control: no-cache'); header('X-Accel-Buffering: no'); $this->fallback = isset($_GET['fallback']) and $_GET['fallback'] == 'true'; diff --git a/lib/private/EventSourceFactory.php b/lib/private/EventSourceFactory.php index 197c8bf9e6c..2fff93f7176 100644 --- a/lib/private/EventSourceFactory.php +++ b/lib/private/EventSourceFactory.php @@ -27,20 +27,12 @@ use OCP\IEventSourceFactory; use OCP\IRequest; class EventSourceFactory implements IEventSourceFactory { - private IRequest $request; - - - public function __construct(IRequest $request) { - $this->request = $request; + public function __construct( + private IRequest $request, + ) { } - /** - * Create a new event source - * - * @return IEventSource - * @since 28.0.0 - */ public function create(): IEventSource { - return new \OC_EventSource($this->request); + return new EventSource($this->request); } } From d7ed056160a4f32e9e536bea0d934bbf97354ea4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Thu, 16 May 2024 17:33:30 +0200 Subject: [PATCH 2/2] fix: Improve code quality in EventSource MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- lib/private/EventSource.php | 30 ++++++++++-------------------- lib/private/EventSourceFactory.php | 3 +++ lib/public/IEventSource.php | 2 ++ 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/lib/private/EventSource.php b/lib/private/EventSource.php index 9a62d13a938..b5ab22d026e 100644 --- a/lib/private/EventSource.php +++ b/lib/private/EventSource.php @@ -1,5 +1,7 @@ request = $request; + public function __construct( + private IRequest $request, + ) { } - protected function init() { + protected function init(): void { if ($this->started) { return; } @@ -108,7 +98,7 @@ class EventSource implements IEventSource { */ public function send($type, $data = null) { if ($data and !preg_match('/^[A-Za-z0-9_]+$/', $type)) { - throw new BadMethodCallException('Type needs to be alphanumeric ('. $type .')'); + throw new \BadMethodCallException('Type needs to be alphanumeric ('. $type .')'); } $this->init(); if (is_null($data)) { diff --git a/lib/private/EventSourceFactory.php b/lib/private/EventSourceFactory.php index 2fff93f7176..52ed0749692 100644 --- a/lib/private/EventSourceFactory.php +++ b/lib/private/EventSourceFactory.php @@ -1,4 +1,7 @@ * diff --git a/lib/public/IEventSource.php b/lib/public/IEventSource.php index 879b365cc77..f24c0311f24 100644 --- a/lib/public/IEventSource.php +++ b/lib/public/IEventSource.php @@ -37,6 +37,7 @@ interface IEventSource { * * @param string $type One of success, notice, error, failure and done. Used in core/js/update.js * @param mixed $data + * @return void * * if only one parameter is given, a typeless message will be send with that parameter as data * @since 8.0.0 @@ -45,6 +46,7 @@ interface IEventSource { /** * close the connection of the event source + * @return void * @since 8.0.0 */ public function close();