mirror of
https://github.com/flarum/framework.git
synced 2025-03-10 04:05:30 +08:00
Use event extender where possible
filter extender will eliminate the last callback-style extender before stable.
This commit is contained in:
parent
01549123c0
commit
c7837f801e
@ -9,8 +9,12 @@
|
|||||||
|
|
||||||
use Flarum\Api\Controller\ListDiscussionsController;
|
use Flarum\Api\Controller\ListDiscussionsController;
|
||||||
use Flarum\Api\Serializer\DiscussionSerializer;
|
use Flarum\Api\Serializer\DiscussionSerializer;
|
||||||
|
use Flarum\Discussion\Event\Saving;
|
||||||
use Flarum\Extend;
|
use Flarum\Extend;
|
||||||
|
use Flarum\Sticky\Event\DiscussionWasStickied;
|
||||||
|
use Flarum\Sticky\Event\DiscussionWasUnstickied;
|
||||||
use Flarum\Sticky\Listener;
|
use Flarum\Sticky\Listener;
|
||||||
|
use Flarum\Sticky\Listener\SaveStickyToDatabase;
|
||||||
use Flarum\Sticky\Post\DiscussionStickiedPost;
|
use Flarum\Sticky\Post\DiscussionStickiedPost;
|
||||||
use Illuminate\Contracts\Events\Dispatcher;
|
use Illuminate\Contracts\Events\Dispatcher;
|
||||||
|
|
||||||
@ -38,9 +42,13 @@ return [
|
|||||||
|
|
||||||
new Extend\Locales(__DIR__.'/locale'),
|
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) {
|
function (Dispatcher $events) {
|
||||||
$events->subscribe(Listener\CreatePostWhenDiscussionIsStickied::class);
|
// Replace with Filter extender before stable
|
||||||
$events->subscribe(Listener\PinStickiedDiscussionsToTop::class);
|
$events->subscribe(Listener\PinStickiedDiscussionsToTop::class);
|
||||||
$events->subscribe(Listener\SaveStickyToDatabase::class);
|
|
||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
@ -14,25 +14,15 @@ use Flarum\Sticky\Event\DiscussionWasStickied;
|
|||||||
use Flarum\Sticky\Event\DiscussionWasUnstickied;
|
use Flarum\Sticky\Event\DiscussionWasUnstickied;
|
||||||
use Flarum\Sticky\Post\DiscussionStickiedPost;
|
use Flarum\Sticky\Post\DiscussionStickiedPost;
|
||||||
use Flarum\User\User;
|
use Flarum\User\User;
|
||||||
use Illuminate\Contracts\Events\Dispatcher;
|
|
||||||
|
|
||||||
class CreatePostWhenDiscussionIsStickied
|
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
|
* @param DiscussionWasStickied $event
|
||||||
*/
|
*/
|
||||||
public function whenDiscussionWasStickied(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)
|
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 User $user
|
||||||
* @param bool $isSticky
|
* @param bool $isSticky
|
||||||
*/
|
*/
|
||||||
protected function stickyChanged(Discussion $discussion, User $user, $isSticky)
|
protected static function stickyChanged(Discussion $discussion, User $user, $isSticky)
|
||||||
{
|
{
|
||||||
$post = DiscussionStickiedPost::reply(
|
$post = DiscussionStickiedPost::reply(
|
||||||
$discussion->id,
|
$discussion->id,
|
||||||
|
@ -12,22 +12,13 @@ namespace Flarum\Sticky\Listener;
|
|||||||
use Flarum\Discussion\Event\Saving;
|
use Flarum\Discussion\Event\Saving;
|
||||||
use Flarum\Sticky\Event\DiscussionWasStickied;
|
use Flarum\Sticky\Event\DiscussionWasStickied;
|
||||||
use Flarum\Sticky\Event\DiscussionWasUnstickied;
|
use Flarum\Sticky\Event\DiscussionWasUnstickied;
|
||||||
use Illuminate\Contracts\Events\Dispatcher;
|
|
||||||
|
|
||||||
class SaveStickyToDatabase
|
class SaveStickyToDatabase
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* @param Dispatcher $events
|
|
||||||
*/
|
|
||||||
public function subscribe(Dispatcher $events)
|
|
||||||
{
|
|
||||||
$events->listen(Saving::class, [$this, 'whenSaving']);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Saving $event
|
* @param Saving $event
|
||||||
*/
|
*/
|
||||||
public function whenSaving(Saving $event)
|
public function handle(Saving $event)
|
||||||
{
|
{
|
||||||
if (isset($event->data['attributes']['isSticky'])) {
|
if (isset($event->data['attributes']['isSticky'])) {
|
||||||
$isSticky = (bool) $event->data['attributes']['isSticky'];
|
$isSticky = (bool) $event->data['attributes']['isSticky'];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user