From 4dacbbd66dcee2f7d8684ba2929025139735893f Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Mon, 11 May 2020 09:27:17 +0200 Subject: [PATCH] Log usage of a deprecated event instead of emitting This way we can track down what is being used and migrate them over. And slowly kill the old way in a release or 2. Signed-off-by: Roeland Jago Douma --- lib/composer/composer/autoload_classmap.php | 1 + lib/composer/composer/autoload_static.php | 1 + .../EventDispatcher/GenericEventWrapper.php | 111 ++++++++++++++++++ .../EventDispatcher/SymfonyAdapter.php | 19 ++- 4 files changed, 126 insertions(+), 6 deletions(-) create mode 100644 lib/private/EventDispatcher/GenericEventWrapper.php diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index c7b966fd59..ba43ed5171 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -938,6 +938,7 @@ return array( 'OC\\Encryption\\Update' => $baseDir . '/lib/private/Encryption/Update.php', 'OC\\Encryption\\Util' => $baseDir . '/lib/private/Encryption/Util.php', 'OC\\EventDispatcher\\EventDispatcher' => $baseDir . '/lib/private/EventDispatcher/EventDispatcher.php', + 'OC\\EventDispatcher\\GenericEventWrapper' => $baseDir . '/lib/private/EventDispatcher/GenericEventWrapper.php', 'OC\\EventDispatcher\\ServiceEventListener' => $baseDir . '/lib/private/EventDispatcher/ServiceEventListener.php', 'OC\\EventDispatcher\\SymfonyAdapter' => $baseDir . '/lib/private/EventDispatcher/SymfonyAdapter.php', 'OC\\Federation\\CloudFederationFactory' => $baseDir . '/lib/private/Federation/CloudFederationFactory.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 8212d3dafc..0180ffb961 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -967,6 +967,7 @@ class ComposerStaticInit53792487c5a8370acc0b06b1a864ff4c 'OC\\Encryption\\Update' => __DIR__ . '/../../..' . '/lib/private/Encryption/Update.php', 'OC\\Encryption\\Util' => __DIR__ . '/../../..' . '/lib/private/Encryption/Util.php', 'OC\\EventDispatcher\\EventDispatcher' => __DIR__ . '/../../..' . '/lib/private/EventDispatcher/EventDispatcher.php', + 'OC\\EventDispatcher\\GenericEventWrapper' => __DIR__ . '/../../..' . '/lib/private/EventDispatcher/GenericEventWrapper.php', 'OC\\EventDispatcher\\ServiceEventListener' => __DIR__ . '/../../..' . '/lib/private/EventDispatcher/ServiceEventListener.php', 'OC\\EventDispatcher\\SymfonyAdapter' => __DIR__ . '/../../..' . '/lib/private/EventDispatcher/SymfonyAdapter.php', 'OC\\Federation\\CloudFederationFactory' => __DIR__ . '/../../..' . '/lib/private/Federation/CloudFederationFactory.php', diff --git a/lib/private/EventDispatcher/GenericEventWrapper.php b/lib/private/EventDispatcher/GenericEventWrapper.php new file mode 100644 index 0000000000..d1d136affc --- /dev/null +++ b/lib/private/EventDispatcher/GenericEventWrapper.php @@ -0,0 +1,111 @@ + + * + * @author Roeland Jago Douma + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OC\EventDispatcher; + +use OCP\ILogger; +use Symfony\Component\EventDispatcher\GenericEvent; + +class GenericEventWrapper extends GenericEvent { + + /** @var ILogger */ + private $logger; + + /** @var GenericEvent */ + private $event; + + /** @var string */ + private $eventName; + + public function __construct(ILogger $logger, string $eventName, ?GenericEvent $event) { + $this->logger = $logger; + $this->event = $event; + $this->eventName = $eventName; + } + + private function log() { + $this->logger->info( + 'Deprecated event type for {name}: {class} is used', + [ 'name' => $this->eventName, 'class' => is_object($this->event) ? get_class($this->event) : 'null' ] + ); + } + + public function isPropagationStopped() { + $this->log(); + return $this->event->isPropagationStopped(); + } + + public function stopPropagation() { + $this->log(); + $this->event->stopPropagation(); + } + + public function getSubject() { + $this->log(); + return $this->event->getSubject(); + } + + public function getArgument($key) { + $this->log(); + return $this->event->getArgument($key); + } + + public function setArgument($key, $value) { + $this->log(); + return $this->event->setArgument($key, $value); + } + + public function getArguments() { + return $this->event->getArguments(); + } + + public function setArguments(array $args = []) { + return $this->event->setArguments($args); + } + + public function hasArgument($key) { + return $this->event->hasArgument($key); + } + + public function offsetGet($key) { + return $this->event->offsetGet($key); + } + + public function offsetSet($key, $value) { + return $this->event->offsetSet($key, $value); + } + + public function offsetUnset($key) { + return $this->event->offsetUnset($key); + } + + public function offsetExists($key) { + return $this->event->offsetExists($key); + } + + public function getIterator() { + return$this->event->getIterator(); + } +} diff --git a/lib/private/EventDispatcher/SymfonyAdapter.php b/lib/private/EventDispatcher/SymfonyAdapter.php index b1a39a7913..10e953be84 100644 --- a/lib/private/EventDispatcher/SymfonyAdapter.php +++ b/lib/private/EventDispatcher/SymfonyAdapter.php @@ -28,6 +28,7 @@ declare(strict_types=1); namespace OC\EventDispatcher; +use Symfony\Component\EventDispatcher\GenericEvent; use function is_callable; use OCP\EventDispatcher\Event; use OCP\ILogger; @@ -62,12 +63,18 @@ class SymfonyAdapter implements EventDispatcherInterface { if ($event instanceof Event) { $this->eventDispatcher->dispatch($eventName, $event); } else { - // Legacy event - $this->logger->info( - 'Deprecated event type for {name}: {class}', - [ 'name' => $eventName, 'class' => is_object($event) ? get_class($event) : 'null' ] - ); - $this->eventDispatcher->getSymfonyDispatcher()->dispatch($eventName, $event); + if ($event instanceof GenericEvent) { + $newEvent = new GenericEventWrapper($this->logger, $eventName, $event); + } else { + $newEvent = $event; + + // Legacy event + $this->logger->info( + 'Deprecated event type for {name}: {class}', + ['name' => $eventName, 'class' => is_object($event) ? get_class($event) : 'null'] + ); + } + $this->eventDispatcher->getSymfonyDispatcher()->dispatch($eventName, $newEvent); } }