Skip to content

Commit

Permalink
Resolve SettingsRepositoryInterface as late as possible (#5)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
clarkwinkelmann committed Apr 26, 2020
1 parent 2661164 commit cf35843
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
5 changes: 3 additions & 2 deletions extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand All @@ -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);
},
];
28 changes: 14 additions & 14 deletions src/Listeners/SaveSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
}

0 comments on commit cf35843

Please sign in to comment.