Use event extender where possible

filter extender will eliminate the last callback-style extender before stable.
This commit is contained in:
Alexander Skvortsov 2020-12-08 12:34:32 -05:00
parent 01549123c0
commit c7837f801e
3 changed files with 14 additions and 25 deletions

View File

@ -9,8 +9,12 @@
use Flarum\Api\Controller\ListDiscussionsController;
use Flarum\Api\Serializer\DiscussionSerializer;
use Flarum\Discussion\Event\Saving;
use Flarum\Extend;
use Flarum\Sticky\Event\DiscussionWasStickied;
use Flarum\Sticky\Event\DiscussionWasUnstickied;
use Flarum\Sticky\Listener;
use Flarum\Sticky\Listener\SaveStickyToDatabase;
use Flarum\Sticky\Post\DiscussionStickiedPost;
use Illuminate\Contracts\Events\Dispatcher;
@ -38,9 +42,13 @@ return [
new Extend\Locales(__DIR__.'/locale'),
(new Extend\Event())
->listen(Saving::class, SaveStickyToDatabase::class)
->listen(DiscussionWasStickied::class, [Listener\CreatePostWhenDiscussionIsStickied::class, 'whenDiscussionWasStickied'])
->listen(DiscussionWasUnstickied::class, [Listener\CreatePostWhenDiscussionIsStickied::class, 'whenDiscussionWasUnstickied']),
function (Dispatcher $events) {
$events->subscribe(Listener\CreatePostWhenDiscussionIsStickied::class);
// Replace with Filter extender before stable
$events->subscribe(Listener\PinStickiedDiscussionsToTop::class);
$events->subscribe(Listener\SaveStickyToDatabase::class);
},
];

View File

@ -14,25 +14,15 @@ use Flarum\Sticky\Event\DiscussionWasStickied;
use Flarum\Sticky\Event\DiscussionWasUnstickied;
use Flarum\Sticky\Post\DiscussionStickiedPost;
use Flarum\User\User;
use Illuminate\Contracts\Events\Dispatcher;
class CreatePostWhenDiscussionIsStickied
{
/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(DiscussionWasStickied::class, [$this, 'whenDiscussionWasStickied']);
$events->listen(DiscussionWasUnstickied::class, [$this, 'whenDiscussionWasUnstickied']);
}
/**
* @param DiscussionWasStickied $event
*/
public function whenDiscussionWasStickied(DiscussionWasStickied $event)
{
$this->stickyChanged($event->discussion, $event->user, true);
static::stickyChanged($event->discussion, $event->user, true);
}
/**
@ -40,7 +30,7 @@ class CreatePostWhenDiscussionIsStickied
*/
public function whenDiscussionWasUnstickied(DiscussionWasUnstickied $event)
{
$this->stickyChanged($event->discussion, $event->user, false);
static::stickyChanged($event->discussion, $event->user, false);
}
/**
@ -48,7 +38,7 @@ class CreatePostWhenDiscussionIsStickied
* @param User $user
* @param bool $isSticky
*/
protected function stickyChanged(Discussion $discussion, User $user, $isSticky)
protected static function stickyChanged(Discussion $discussion, User $user, $isSticky)
{
$post = DiscussionStickiedPost::reply(
$discussion->id,

View File

@ -12,22 +12,13 @@ namespace Flarum\Sticky\Listener;
use Flarum\Discussion\Event\Saving;
use Flarum\Sticky\Event\DiscussionWasStickied;
use Flarum\Sticky\Event\DiscussionWasUnstickied;
use Illuminate\Contracts\Events\Dispatcher;
class SaveStickyToDatabase
{
/**
* @param Dispatcher $events
*/
public function subscribe(Dispatcher $events)
{
$events->listen(Saving::class, [$this, 'whenSaving']);
}
/**
* @param Saving $event
*/
public function whenSaving(Saving $event)
public function handle(Saving $event)
{
if (isset($event->data['attributes']['isSticky'])) {
$isSticky = (bool) $event->data['attributes']['isSticky'];