diff options
-rw-r--r-- | lib/composer/composer/autoload_classmap.php | 1 | ||||
-rw-r--r-- | lib/composer/composer/autoload_static.php | 1 | ||||
-rw-r--r-- | lib/public/EventDispatcher/JsonSerializer.php | 42 | ||||
-rw-r--r-- | lib/public/Files/Events/Node/AbstractNodeEvent.php | 13 | ||||
-rw-r--r-- | lib/public/Files/Events/Node/AbstractNodesEvent.php | 12 |
5 files changed, 68 insertions, 1 deletions
diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 8e1408e121e..0fe1314644f 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -281,6 +281,7 @@ return array( 'OCP\\EventDispatcher\\GenericEvent' => $baseDir . '/lib/public/EventDispatcher/GenericEvent.php', 'OCP\\EventDispatcher\\IEventDispatcher' => $baseDir . '/lib/public/EventDispatcher/IEventDispatcher.php', 'OCP\\EventDispatcher\\IEventListener' => $baseDir . '/lib/public/EventDispatcher/IEventListener.php', + 'OCP\\EventDispatcher\\JsonSerializer' => $baseDir . '/lib/public/EventDispatcher/JsonSerializer.php', 'OCP\\Exceptions\\AbortedEventException' => $baseDir . '/lib/public/Exceptions/AbortedEventException.php', 'OCP\\Exceptions\\AppConfigException' => $baseDir . '/lib/public/Exceptions/AppConfigException.php', 'OCP\\Exceptions\\AppConfigIncorrectTypeException' => $baseDir . '/lib/public/Exceptions/AppConfigIncorrectTypeException.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index d6939ae36ce..cf52b9026f1 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -322,6 +322,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OCP\\EventDispatcher\\GenericEvent' => __DIR__ . '/../../..' . '/lib/public/EventDispatcher/GenericEvent.php', 'OCP\\EventDispatcher\\IEventDispatcher' => __DIR__ . '/../../..' . '/lib/public/EventDispatcher/IEventDispatcher.php', 'OCP\\EventDispatcher\\IEventListener' => __DIR__ . '/../../..' . '/lib/public/EventDispatcher/IEventListener.php', + 'OCP\\EventDispatcher\\JsonSerializer' => __DIR__ . '/../../..' . '/lib/public/EventDispatcher/JsonSerializer.php', 'OCP\\Exceptions\\AbortedEventException' => __DIR__ . '/../../..' . '/lib/public/Exceptions/AbortedEventException.php', 'OCP\\Exceptions\\AppConfigException' => __DIR__ . '/../../..' . '/lib/public/Exceptions/AppConfigException.php', 'OCP\\Exceptions\\AppConfigIncorrectTypeException' => __DIR__ . '/../../..' . '/lib/public/Exceptions/AppConfigIncorrectTypeException.php', diff --git a/lib/public/EventDispatcher/JsonSerializer.php b/lib/public/EventDispatcher/JsonSerializer.php new file mode 100644 index 00000000000..1eb75ed7527 --- /dev/null +++ b/lib/public/EventDispatcher/JsonSerializer.php @@ -0,0 +1,42 @@ +<?php + +declare(strict_types=1); + +/** + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OCP\EventDispatcher; + +use OCP\Files\FileInfo; +use OCP\IUser; + +/** + * Contains helper static methods to serialize OCP classes into arrays to pass json_encode. + * Useful for events implementing \JsonSerializable. + * + * @since 30.0.0 + */ + +final class JsonSerializer { + /** + * @since 30.0.0 + */ + public static function serializeFileInfo(FileInfo $node): array { + return [ + 'id' => $node->getId(), + 'path' => $node->getPath(), + ]; + } + + /** + * @since 30.0.0 + */ + public static function serializeUser(IUser $user): array { + return [ + 'uid' => $user->getUID(), + 'displayName' => $user->getDisplayName(), + ]; + } +} diff --git a/lib/public/Files/Events/Node/AbstractNodeEvent.php b/lib/public/Files/Events/Node/AbstractNodeEvent.php index 1b290578ab9..cc27bdd8cfd 100644 --- a/lib/public/Files/Events/Node/AbstractNodeEvent.php +++ b/lib/public/Files/Events/Node/AbstractNodeEvent.php @@ -9,12 +9,13 @@ declare(strict_types=1); namespace OCP\Files\Events\Node; use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\JsonSerializer; use OCP\Files\Node; /** * @since 20.0.0 */ -abstract class AbstractNodeEvent extends Event { +abstract class AbstractNodeEvent extends Event implements \JsonSerializable { /** * @since 20.0.0 */ @@ -29,4 +30,14 @@ abstract class AbstractNodeEvent extends Event { public function getNode(): Node { return $this->node; } + + /** + * @since 30.0.0 + */ + public function jsonSerialize(): array { + return [ + 'class' => static::class, + 'node' => JsonSerializer::serializeFileInfo($this->node), + ]; + } } diff --git a/lib/public/Files/Events/Node/AbstractNodesEvent.php b/lib/public/Files/Events/Node/AbstractNodesEvent.php index a5b058f18f5..b5fd1b75898 100644 --- a/lib/public/Files/Events/Node/AbstractNodesEvent.php +++ b/lib/public/Files/Events/Node/AbstractNodesEvent.php @@ -9,6 +9,7 @@ declare(strict_types=1); namespace OCP\Files\Events\Node; use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\JsonSerializer; use OCP\Files\Node; /** @@ -37,4 +38,15 @@ abstract class AbstractNodesEvent extends Event { public function getTarget(): Node { return $this->target; } + + /** + * @since 30.0.0 + */ + public function jsonSerialize(): array { + return [ + 'class' => static::class, + 'source' => JsonSerializer::serializeFileInfo($this->source), + 'target' => JsonSerializer::serializeFileInfo($this->target), + ]; + } } |