From cf35843ad39838b4aa8263fced6e8f2944231d1e Mon Sep 17 00:00:00 2001 From: Clark Winkelmann Date: Sun, 26 Apr 2020 07:40:52 +0200 Subject: [PATCH] Resolve SettingsRepositoryInterface as late as possible (#5) This gives the opportunity to other extensions to change its implementation Also added the missing check for ForumSerializer so that the value only gets added to the forum payload and not every other serialized payload --- extend.php | 5 +++-- src/Listeners/SaveSettings.php | 28 ++++++++++++++-------------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/extend.php b/extend.php index fdaa829..773fdcc 100644 --- a/extend.php +++ b/extend.php @@ -10,9 +10,10 @@ */ namespace Therealsujitk\GIFs; -use Illuminate\Contracts\Events\Dispatcher; +use Flarum\Api\Event\Serializing; use Flarum\Extend; +use Illuminate\Contracts\Events\Dispatcher; return [ (new Extend\Frontend('forum')) @@ -21,6 +22,6 @@ ->js(__DIR__.'/js/dist/admin.js'), new Extend\Locales(__DIR__ . '/resources/locale'), function (Dispatcher $dispatcher) { - $dispatcher->subscribe(Listeners\SaveSettings::class); + $dispatcher->listen(Serializing::class, Listeners\SaveSettings::class); }, ]; diff --git a/src/Listeners/SaveSettings.php b/src/Listeners/SaveSettings.php index 999d8ca..0db6df7 100644 --- a/src/Listeners/SaveSettings.php +++ b/src/Listeners/SaveSettings.php @@ -2,23 +2,23 @@ namespace Therealsujitk\GIFs\Listeners; -use Flarum\Api\Serializer\UserSerializer; use Flarum\Api\Event\Serializing; +use Flarum\Api\Serializer\ForumSerializer; use Flarum\Settings\SettingsRepositoryInterface; -use Illuminate\Contracts\Events\Dispatcher; -class SaveSettings { - protected $settings; +class SaveSettings +{ + protected $settings; - public function __construct(SettingsRepositoryInterface $settings) { - $this->settings = $settings; - } + public function __construct(SettingsRepositoryInterface $settings) + { + $this->settings = $settings; + } - public function subscribe(Dispatcher $events) { - $events->listen(Serializing::class, [$this, 'addAttributes']); - } - - public function addAttributes(Serializing $event) { - $event->attributes['therealsujitk-gifs.giphy_api_key'] = $this->settings->get('therealsujitk-gifs.giphy_api_key'); - } + public function handle(Serializing $event) + { + if ($event->isSerializer(ForumSerializer::class)) { + $event->attributes['therealsujitk-gifs.giphy_api_key'] = $this->settings->get('therealsujitk-gifs.giphy_api_key'); + } + } }